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);
26 use vars qw(@ISA @EXPORT);
32 &ValidateBorrowernumber
33 &SendPasswordRecoveryEmail
35 &CompletePasswordRecovery
36 &DeleteExpiredPasswordRecovery
42 Koha::Patron::Password::Recovery - Koha password recovery module
46 use Koha::Patron::Password::Recovery;
50 =head2 ValidateBorrowernumber
52 $alread = ValidateBorrowernumber( $borrower_number );
54 Check if the system already start recovery
60 sub ValidateBorrowernumber {
61 my ($borrower_number) = @_;
62 my $schema = Koha::Database->new->schema;
64 my $rs = $schema->resultset('BorrowerPasswordRecovery')->search(
66 borrowernumber => $borrower_number,
67 valid_until => \'> NOW()'
69 { columns => 'borrowernumber' }
77 =head2 GetValidLinkInfo
79 Check if the link is still valid and return some info.
83 sub GetValidLinkInfo {
85 my $dbh = C4::Context->dbh;
87 SELECT borrower_password_recovery.borrowernumber, userid
88 FROM borrower_password_recovery, borrowers
89 WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber
90 AND NOW() < valid_until
93 my $sth = $dbh->prepare($query);
94 $sth->execute($uniqueKey);
95 return $sth->fetchrow;
98 =head2 SendPasswordRecoveryEmail
100 It creates an email using the templates and sends it to the user, using the specified email
104 sub SendPasswordRecoveryEmail {
105 my $borrower = shift; # Koha::Patron
106 my $userEmail = shift; #to_address (the one specified in the request)
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
119 dt_from_string()->add( days => 2 );
122 $schema->resultset('BorrowerPasswordRecovery')
123 ->search( { borrowernumber => $borrower->borrowernumber, } );
125 { uuid => $uuid_str, valid_until => $expirydate->datetime() } );
128 my $rs = $schema->resultset('BorrowerPasswordRecovery')->create(
130 borrowernumber => $borrower->borrowernumber,
132 valid_until => $expirydate->datetime()
138 my $opacbase = C4::Context->preference('OPACBaseURL') || '';
139 my $uuidLink = $opacbase
140 . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
143 my $letter = C4::Letters::GetPreparedLetter(
145 letter_code => 'PASSWORD_RESET',
146 branchcode => $borrower->branchcode,
147 lang => $borrower->lang,
149 { passwordreseturl => $uuidLink, user => $borrower->userid },
153 my $library = $borrower->library;
154 my $kohaEmail = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress'); # send from patron's branch or Koha Admin
156 my $message_id = C4::Letters::EnqueueLetter(
159 borrowernumber => $borrower->borrowernumber,
160 to_address => $userEmail,
161 from_address => $kohaEmail,
162 message_transport_type => 'email',
166 my $num_letters_attempted =
167 C4::Letters::SendQueuedMessages( { message_id => $message_id } );
169 return ($num_letters_attempted > 0);
172 =head2 CompletePasswordRecovery
174 $bool = CompletePasswordRecovery($uuid);
176 Deletes a password recovery entry.
180 sub CompletePasswordRecovery {
181 my $uniqueKey = shift;
183 Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
184 my $entry = $model->search(
185 { -or => [ uuid => $uniqueKey, valid_until => \'< NOW()' ] } );
186 return $entry->delete();
189 =head2 DeleteExpiredPasswordRecovery
191 $bool = DeleteExpiredPasswordRecovery($borrowernumber)
193 Deletes an expired password recovery entry.
197 sub DeleteExpiredPasswordRecovery {
198 my $borrower_number = shift;
200 Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
201 my $entry = $model->search(
202 { borrowernumber => $borrower_number } );
203 return $entry->delete();
207 END { } # module clean-up code here (global destructor)