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>.
23 use vars qw($VERSION @ISA @EXPORT);
26 # set the version for version checking
27 $VERSION = 3.07.00.049;
31 &ValidateBorrowernumber
32 &SendPasswordRecoveryEmail
34 &CompletePasswordRecovery
40 C4::Passwordrecovery - Koha password recovery module
44 use C4::Passwordrecovery;
48 =head2 ValidateBorrowernumber
50 $alread = ValidateBorrowernumber( $borrower_number );
52 Check if the system already start recovery
58 sub ValidateBorrowernumber {
59 my ($borrower_number) = @_;
60 my $schema = Koha::Database->new->schema;
62 my $rs = $schema->resultset('BorrowerPasswordRecovery')->search(
64 borrowernumber => $borrower_number,
65 valid_until => \'> NOW()'
67 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 send it to the user, using the specified email
104 sub SendPasswordRecoveryEmail {
105 my $borrower = shift; # Koha::Borrower
106 my $userEmail = shift; #to_address (the one specified in the request)
109 my $schema = Koha::Database->new->schema;
112 my @chars = ("A".."Z", "a".."z", "0".."9");
114 $uuid_str .= $chars[rand @chars] for 1..32;
116 # insert into database
117 my $expirydate = DateTime->now(time_zone => C4::Context->tz())->add( days => 2 );
119 my $rs = $schema->resultset('BorrowerPasswordRecovery')->search(
121 borrowernumber => $borrower->borrowernumber,
123 $rs->update({uuid => $uuid_str, valid_until => $expirydate->datetime()});
125 my $rs = $schema->resultset('BorrowerPasswordRecovery')->create({
126 borrowernumber=>$borrower->borrowernumber,
128 valid_until=> $expirydate->datetime()
133 my $uuidLink = C4::Context->preference( 'OPACBaseURL' ) . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
136 my $letter = C4::Letters::GetPreparedLetter (
138 letter_code => 'PASSWORD_RESET',
139 branchcode => $borrower->branchcode,
140 substitute => {passwordreseturl => $uuidLink, user => $borrower->userid },
143 # define to/from emails
144 my $kohaEmail = C4::Context->preference( 'KohaAdminEmailAddress' ); # from
146 C4::Letters::EnqueueLetter( {
148 borrowernumber => $borrower->borrowernumber,
149 to_address => $userEmail,
150 from_address => $kohaEmail,
151 message_transport_type => 'email',
156 =head2 CompletePasswordRecovery
158 $bool = CompletePasswordRevovery($uuid);
160 Deletes a password recovery entry.
163 sub CompletePasswordRecovery{
164 my $uniqueKey = shift;
165 my $model = Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
166 my $entry = $model->search({-or => [uuid => $uniqueKey, valid_until => \'< NOW()']});
167 return $entry->delete();
170 END { } # module clean-up code here (global destructor)