0f1882bf8400b5a101a597f7582d8241186eb6f8
[koha.git] / Koha / Patron / Password / Recovery.pm
1 package Koha::Patron::Password::Recovery;
2
3 # Copyright 2014 Solutions InLibro inc.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use C4::Context;
22 use C4::Letters;
23 use Crypt::Eksblowfish::Bcrypt qw( en_base64 );
24 use Koha::DateUtils qw( dt_from_string );
25
26 use constant PATRON => 2;
27 use constant STAFF  => 5;
28
29 our (@ISA, @EXPORT_OK);
30 BEGIN {
31     require Exporter;
32     @ISA = qw(Exporter);
33     @EXPORT_OK = qw(
34       &ValidateBorrowernumber
35       &SendPasswordRecoveryEmail
36       &GetValidLinkInfo
37       &CompletePasswordRecovery
38       &DeleteExpiredPasswordRecovery
39     );
40 }
41
42 =head1 NAME
43
44 Koha::Patron::Password::Recovery - Koha password recovery module
45
46 =head1 SYNOPSIS
47
48 use Koha::Patron::Password::Recovery;
49
50 =head1 FUNCTIONS
51
52 =head2 ValidateBorrowernumber
53
54 $alread = ValidateBorrowernumber( $borrower_number );
55
56 Check if the system already start recovery
57
58 Returns true false
59
60 =cut
61
62 sub ValidateBorrowernumber {
63     my ($borrower_number) = @_;
64     my $schema = Koha::Database->new->schema;
65
66     my $rs = $schema->resultset('BorrowerPasswordRecovery')->search(
67         {
68             borrowernumber => $borrower_number,
69             valid_until    => \'> NOW()'
70         },
71         { columns => 'borrowernumber' }
72     );
73     if ( $rs->next ) {
74         return 1;
75     }
76     return 0;
77 }
78
79 =head2 GetValidLinkInfo
80
81     Check if the link is still valid and return some info.
82
83 =cut
84
85 sub GetValidLinkInfo {
86     my ($uniqueKey) = @_;
87     my $dbh         = C4::Context->dbh;
88     my $query       = '
89     SELECT borrower_password_recovery.borrowernumber, userid
90     FROM borrower_password_recovery, borrowers
91     WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber
92     AND NOW() < valid_until
93     AND uuid = ?
94     ';
95     my $sth = $dbh->prepare($query);
96     $sth->execute($uniqueKey);
97     return $sth->fetchrow;
98 }
99
100 =head2 SendPasswordRecoveryEmail
101
102  It creates an email using the templates and sends it to the user, using the specified email
103
104 =cut
105
106 sub SendPasswordRecoveryEmail {
107     my $borrower  = shift;    # Koha::Patron
108     my $userEmail = shift;    #to_address (the one specified in the request)
109     my $update    = shift;
110     my $staff     = shift // 0;
111
112     my $schema = Koha::Database->new->schema;
113
114     # generate UUID
115     my $uuid_str;
116     do {
117         $uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16));
118     } while ( substr ( $uuid_str, -1, 1 ) eq '.' );
119
120     # insert into database
121     my $days = $staff ? STAFF : PATRON;
122     my $expirydate =
123       dt_from_string()->add( days => $days );
124     if ($update) {
125         my $rs =
126           $schema->resultset('BorrowerPasswordRecovery')
127           ->search( { borrowernumber => $borrower->borrowernumber, } );
128         $rs->update(
129             { uuid => $uuid_str, valid_until => $expirydate->datetime() } );
130     }
131     else {
132         my $rs = $schema->resultset('BorrowerPasswordRecovery')->create(
133             {
134                 borrowernumber => $borrower->borrowernumber,
135                 uuid           => $uuid_str,
136                 valid_until    => $expirydate->datetime()
137             }
138         );
139     }
140
141     # create link
142     my $opacbase = C4::Context->preference('OPACBaseURL') || '';
143     my $uuidLink = $opacbase
144       . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
145
146     # prepare the email
147     my $letter = C4::Letters::GetPreparedLetter(
148         module      => 'members',
149         letter_code => $staff ? 'STAFF_PASSWORD_RESET' : 'PASSWORD_RESET',
150         branchcode  => $borrower->branchcode,
151         lang        => $borrower->lang,
152         substitute =>
153           { passwordreseturl => $uuidLink, user => $borrower->userid },
154     );
155
156     # define from emails
157     my $library = $borrower->library;
158     my $kohaEmail = $library->from_email_address; # send from patron's branch or Koha Admin
159
160     my $message_id = C4::Letters::EnqueueLetter(
161         {
162             letter                 => $letter,
163             borrowernumber         => $borrower->borrowernumber,
164             to_address             => $userEmail,
165             from_address           => $kohaEmail,
166             message_transport_type => 'email',
167         }
168     );
169
170     my $num_letters_attempted =
171       C4::Letters::SendQueuedMessages( { message_id => $message_id } );
172
173     return ($num_letters_attempted > 0);
174 }
175
176 =head2 CompletePasswordRecovery
177
178     $bool = CompletePasswordRecovery($uuid);
179
180     Deletes a password recovery entry.
181
182 =cut
183
184 sub CompletePasswordRecovery {
185     my $uniqueKey = shift;
186     my $model =
187       Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
188     my $entry = $model->search(
189         { -or => [ uuid => $uniqueKey, valid_until => \'< NOW()' ] } );
190     return $entry->delete();
191 }
192
193 =head2 DeleteExpiredPasswordRecovery
194
195     $bool = DeleteExpiredPasswordRecovery($borrowernumber)
196
197     Deletes an expired password recovery entry.
198
199 =cut
200
201 sub DeleteExpiredPasswordRecovery {
202     my $borrower_number = shift;
203     my $model =
204       Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
205     my $entry = $model->search(
206         { borrowernumber => $borrower_number } );
207     return $entry->delete();
208 }
209
210
211 END { }    # module clean-up code here (global destructor)
212
213 1;