1 package Koha::Patron::Password::Recovery;
3 # Copyright 2014 Solutions InLibro inc.
5 # This file is part of Koha.
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.
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.
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>.
23 use Crypt::Eksblowfish::Bcrypt qw( en_base64 );
24 use Koha::DateUtils qw( dt_from_string );
26 our (@ISA, @EXPORT_OK);
31 &ValidateBorrowernumber
32 &SendPasswordRecoveryEmail
34 &CompletePasswordRecovery
35 &DeleteExpiredPasswordRecovery
41 Koha::Patron::Password::Recovery - Koha password recovery module
45 use Koha::Patron::Password::Recovery;
49 =head2 ValidateBorrowernumber
51 $alread = ValidateBorrowernumber( $borrower_number );
53 Check if the system already start recovery
59 sub ValidateBorrowernumber {
60 my ($borrower_number) = @_;
61 my $schema = Koha::Database->new->schema;
63 my $rs = $schema->resultset('BorrowerPasswordRecovery')->search(
65 borrowernumber => $borrower_number,
66 valid_until => \'> NOW()'
68 { columns => 'borrowernumber' }
76 =head2 GetValidLinkInfo
78 Check if the link is still valid and return some info.
82 sub GetValidLinkInfo {
84 my $dbh = C4::Context->dbh;
86 SELECT borrower_password_recovery.borrowernumber, userid
87 FROM borrower_password_recovery, borrowers
88 WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber
89 AND NOW() < valid_until
92 my $sth = $dbh->prepare($query);
93 $sth->execute($uniqueKey);
94 return $sth->fetchrow;
97 =head2 SendPasswordRecoveryEmail
99 It creates an email using the templates and sends it to the user, using the specified email
103 sub SendPasswordRecoveryEmail {
104 my $borrower = shift; # Koha::Patron
105 my $userEmail = shift; #to_address (the one specified in the request)
107 my $staff = shift // 0;
109 my $schema = Koha::Database->new->schema;
114 $uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16));
115 } while ( substr ( $uuid_str, -1, 1 ) eq '.' );
117 # insert into database
118 my $days = $staff ? 5 : 2;
120 dt_from_string()->add( days => $days );
123 $schema->resultset('BorrowerPasswordRecovery')
124 ->search( { borrowernumber => $borrower->borrowernumber, } );
126 { uuid => $uuid_str, valid_until => $expirydate->datetime() } );
129 my $rs = $schema->resultset('BorrowerPasswordRecovery')->create(
131 borrowernumber => $borrower->borrowernumber,
133 valid_until => $expirydate->datetime()
139 my $opacbase = C4::Context->preference('OPACBaseURL') || '';
140 my $uuidLink = $opacbase
141 . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
144 my $letter = C4::Letters::GetPreparedLetter(
146 letter_code => $staff ? 'STAFF_PASSWORD_RESET' : 'PASSWORD_RESET',
147 branchcode => $borrower->branchcode,
148 lang => $borrower->lang,
150 { passwordreseturl => $uuidLink, user => $borrower->userid },
154 my $library = $borrower->library;
155 my $kohaEmail = $library->from_email_address; # send from patron's branch or Koha Admin
157 my $message_id = C4::Letters::EnqueueLetter(
160 borrowernumber => $borrower->borrowernumber,
161 to_address => $userEmail,
162 from_address => $kohaEmail,
163 message_transport_type => 'email',
167 my $num_letters_attempted =
168 C4::Letters::SendQueuedMessages( { message_id => $message_id } );
170 return ($num_letters_attempted > 0);
173 =head2 CompletePasswordRecovery
175 $bool = CompletePasswordRecovery($uuid);
177 Deletes a password recovery entry.
181 sub CompletePasswordRecovery {
182 my $uniqueKey = shift;
184 Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
185 my $entry = $model->search(
186 { -or => [ uuid => $uniqueKey, valid_until => \'< NOW()' ] } );
187 return $entry->delete();
190 =head2 DeleteExpiredPasswordRecovery
192 $bool = DeleteExpiredPasswordRecovery($borrowernumber)
194 Deletes an expired password recovery entry.
198 sub DeleteExpiredPasswordRecovery {
199 my $borrower_number = shift;
201 Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
202 my $entry = $model->search(
203 { borrowernumber => $borrower_number } );
204 return $entry->delete();
208 END { } # module clean-up code here (global destructor)