1 package C4
::Passwordrecovery
;
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>.
22 use Crypt
::Eksblowfish
::Bcrypt
qw(en_base64);
24 use vars
qw($VERSION @ISA @EXPORT);
27 # set the version for version checking
28 $VERSION = 3.07.00.049;
32 &ValidateBorrowernumber
33 &SendPasswordRecoveryEmail
35 &CompletePasswordRecovery
41 C4::Passwordrecovery - Koha password recovery module
45 use C4::Passwordrecovery;
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
' }
78 =head2 GetValidLinkInfo
80 Check if the link is still valid and return some info.
84 sub GetValidLinkInfo {
86 my $dbh = C4::Context->dbh;
88 SELECT borrower_password_recovery
.borrowernumber
, userid
89 FROM borrower_password_recovery
, borrowers
90 WHERE borrowers
.borrowernumber
= borrower_password_recovery
.borrowernumber
91 AND NOW
() < valid_until
94 my $sth = $dbh->prepare($query);
95 $sth->execute($uniqueKey);
96 return $sth->fetchrow;
99 =head2 SendPasswordRecoveryEmail
101 It creates an email using the templates and sends it to the user, using the specified email
105 sub SendPasswordRecoveryEmail {
106 my $borrower = shift; # Koha::Borrower
107 my $userEmail = shift; #to_address (the one specified in the request)
110 my $schema = Koha::Database->new->schema;
113 my @chars = ( "A" .. "Z", "a" .. "z", "0" .. "9" );
114 my $uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak
', 16));
116 # insert into database
118 DateTime->now( time_zone => C4::Context->tz() )->add( days => 2 );
121 $schema->resultset('BorrowerPasswordRecovery
')
122 ->search( { borrowernumber => $borrower->borrowernumber, } );
124 { uuid => $uuid_str, valid_until => $expirydate->datetime() } );
127 my $rs = $schema->resultset('BorrowerPasswordRecovery
')->create(
129 borrowernumber => $borrower->borrowernumber,
131 valid_until => $expirydate->datetime()
137 my $uuidLink = C4::Context->preference('OPACBaseURL
')
138 . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
141 my $letter = C4::Letters::GetPreparedLetter(
143 letter_code => 'PASSWORD_RESET
',
144 branchcode => $borrower->branchcode,
146 { passwordreseturl => $uuidLink, user => $borrower->userid },
149 # define to/from emails
150 my $kohaEmail = C4::Context->preference('KohaAdminEmailAddress
'); # from
152 C4::Letters::EnqueueLetter(
155 borrowernumber => $borrower->borrowernumber,
156 to_address => $userEmail,
157 from_address => $kohaEmail,
158 message_transport_type => 'email
',
165 =head2 CompletePasswordRecovery
167 $bool = CompletePasswordRecovery($uuid);
169 Deletes a password recovery entry.
173 sub CompletePasswordRecovery {
174 my $uniqueKey = shift;
176 Koha::Database->new->schema->resultset('BorrowerPasswordRecovery
');
177 my $entry = $model->search(
178 { -or => [ uuid => $uniqueKey, valid_until => \'< NOW()' ] } );
179 return $entry->delete();
182 END { } # module clean-up code here (global destructor)