Bug 18309: UNIMARC update from IFLA - authority (fr) (STU)
[koha.git] / Koha / Patron / Password / Recovery.pm
blob0d09923bf1754dcbcbc6d4f3394d1dcd724cfe6d
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>.
20 use Modern::Perl;
21 use C4::Context;
22 use C4::Letters;
23 use Crypt::Eksblowfish::Bcrypt qw(en_base64);
25 use vars qw(@ISA @EXPORT);
27 BEGIN {
28 require Exporter;
29 @ISA = qw(Exporter);
30 push @EXPORT, qw(
31 &ValidateBorrowernumber
32 &SendPasswordRecoveryEmail
33 &GetValidLinkInfo
34 &CompletePasswordRecovery
35 &DeleteExpiredPasswordRecovery
39 =head1 NAME
41 Koha::Patron::Password::Recovery - Koha password recovery module
43 =head1 SYNOPSIS
45 use Koha::Patron::Password::Recovery;
47 =head1 FUNCTIONS
49 =head2 ValidateBorrowernumber
51 $alread = ValidateBorrowernumber( $borrower_number );
53 Check if the system already start recovery
55 Returns true false
57 =cut
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' }
70 if ( $rs->next ) {
71 return 1;
73 return 0;
76 =head2 GetValidLinkInfo
78 Check if the link is still valid and return some info.
80 =cut
82 sub GetValidLinkInfo {
83 my ($uniqueKey) = @_;
84 my $dbh = C4::Context->dbh;
85 my $query = '
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
90 AND uuid = ?
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
101 =cut
103 sub SendPasswordRecoveryEmail {
104 my $borrower = shift; # Koha::Patron
105 my $userEmail = shift; #to_address (the one specified in the request)
106 my $update = shift;
108 my $schema = Koha::Database->new->schema;
110 # generate UUID
111 my $uuid_str;
112 do {
113 $uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16));
114 } while ( substr ( $uuid_str, -1, 1 ) eq '.' );
116 # insert into database
117 my $expirydate =
118 DateTime->now( time_zone => C4::Context->tz() )->add( days => 2 );
119 if ($update) {
120 my $rs =
121 $schema->resultset('BorrowerPasswordRecovery')
122 ->search( { borrowernumber => $borrower->borrowernumber, } );
123 $rs->update(
124 { uuid => $uuid_str, valid_until => $expirydate->datetime() } );
126 else {
127 my $rs = $schema->resultset('BorrowerPasswordRecovery')->create(
129 borrowernumber => $borrower->borrowernumber,
130 uuid => $uuid_str,
131 valid_until => $expirydate->datetime()
136 # create link
137 my $opacbase = C4::Context->preference('OPACBaseURL') || '';
138 my $uuidLink = $opacbase
139 . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
141 # prepare the email
142 my $letter = C4::Letters::GetPreparedLetter(
143 module => 'members',
144 letter_code => 'PASSWORD_RESET',
145 branchcode => $borrower->branchcode,
146 lang => $borrower->lang,
147 substitute =>
148 { passwordreseturl => $uuidLink, user => $borrower->userid },
151 # define from emails
152 my $library = $borrower->library;
153 my $kohaEmail = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress'); # send from patron's branch or Koha Admin
155 C4::Letters::EnqueueLetter(
157 letter => $letter,
158 borrowernumber => $borrower->borrowernumber,
159 to_address => $userEmail,
160 from_address => $kohaEmail,
161 message_transport_type => 'email',
164 my $num_letters_attempted = C4::Letters::SendQueuedMessages( {
165 borrowernumber => $borrower->borrowernumber,
166 letter_code => 'PASSWORD_RESET'
167 } );
168 return ($num_letters_attempted > 0);
171 =head2 CompletePasswordRecovery
173 $bool = CompletePasswordRecovery($uuid);
175 Deletes a password recovery entry.
177 =cut
179 sub CompletePasswordRecovery {
180 my $uniqueKey = shift;
181 my $model =
182 Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
183 my $entry = $model->search(
184 { -or => [ uuid => $uniqueKey, valid_until => \'< NOW()' ] } );
185 return $entry->delete();
188 =head2 DeleteExpiredPasswordRecovery
190 $bool = DeleteExpiredPasswordRecovery($borrowernumber)
192 Deletes an expired password recovery entry.
194 =cut
196 sub DeleteExpiredPasswordRecovery {
197 my $borrower_number = shift;
198 my $model =
199 Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
200 my $entry = $model->search(
201 { borrowernumber => $borrower_number } );
202 return $entry->delete();
206 END { } # module clean-up code here (global destructor)