Lines 9-14
use C4::Koha;
Link Here
|
9 |
use C4::Members qw(changepassword GetMember GetMemberDetails ); |
9 |
use C4::Members qw(changepassword GetMember GetMemberDetails ); |
10 |
use C4::Output; |
10 |
use C4::Output; |
11 |
use C4::Context; |
11 |
use C4::Context; |
|
|
12 |
use C4::Passwordrecovery qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo); |
12 |
use Koha::AuthUtils qw(hash_password); |
13 |
use Koha::AuthUtils qw(hash_password); |
13 |
my $query = new CGI; |
14 |
my $query = new CGI; |
14 |
use HTML::Entities; |
15 |
use HTML::Entities; |
Lines 44-54
my $errLinkNotValid;
Link Here
|
44 |
my $errPassNotMatch; |
45 |
my $errPassNotMatch; |
45 |
my $errPassTooShort; |
46 |
my $errPassTooShort; |
46 |
|
47 |
|
47 |
my $dbh = C4::Context->dbh; |
|
|
48 |
|
49 |
if ( $query->param('sendEmail') || $query->param('resendEmail') ) { |
48 |
if ( $query->param('sendEmail') || $query->param('resendEmail') ) { |
50 |
#send mail + confirmation |
49 |
my $protocol = $query->https() ? "https://" : "http://"; |
51 |
|
|
|
52 |
#try with the main email |
50 |
#try with the main email |
53 |
$email ||= ''; # avoid undef |
51 |
$email ||= ''; # avoid undef |
54 |
my $borrower_infos = GetMember( email => $email ); |
52 |
my $borrower_infos = GetMember( email => $email ); |
Lines 63-73
if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
Link Here
|
63 |
$errNoEmailFound = 1; |
61 |
$errNoEmailFound = 1; |
64 |
} |
62 |
} |
65 |
elsif ( !$query->param('resendEmail') ) { |
63 |
elsif ( !$query->param('resendEmail') ) { |
66 |
my $sth = $dbh->prepare( |
64 |
my $already = ValidateBorrowernumber( $borrower_number ); |
67 |
"SELECT borrowernumber FROM borrower_password_recovery WHERE NOW() < valid_until AND borrowernumber = ?" |
65 |
|
68 |
); |
66 |
if ( $already ) { |
69 |
$sth->execute($borrower_number); |
|
|
70 |
if ( my $already = $sth->fetchrow ) { |
71 |
$hasError = 1; |
67 |
$hasError = 1; |
72 |
$errAlreadyStartRecovery = 1; |
68 |
$errAlreadyStartRecovery = 1; |
73 |
} |
69 |
} |
Lines 82-88
if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
Link Here
|
82 |
email => HTML::Entities::encode($email), |
78 |
email => HTML::Entities::encode($email), |
83 |
); |
79 |
); |
84 |
} |
80 |
} |
85 |
elsif ( SendPasswordRecoveryEmail( $borrower_infos, $email, $query, $query->param('resendEmail') ) ) {#generate uuid and send recovery email |
81 |
elsif ( SendPasswordRecoveryEmail( $borrower_infos, $email, $protocol, $query->param('resendEmail') ) ) {#generate uuid and send recovery email |
86 |
$template->param( |
82 |
$template->param( |
87 |
mail_sent => 1, |
83 |
mail_sent => 1, |
88 |
email => $email |
84 |
email => $email |
Lines 96-113
if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
Link Here
|
96 |
} |
92 |
} |
97 |
} |
93 |
} |
98 |
elsif ( $query->param('passwordReset') ) { |
94 |
elsif ( $query->param('passwordReset') ) { |
99 |
#new password form |
95 |
( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey); |
100 |
#check if the link is still valid |
|
|
101 |
my $sth = $dbh->prepare( |
102 |
"SELECT borrower_password_recovery.borrowernumber, userid |
103 |
FROM borrower_password_recovery, borrowers |
104 |
WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber |
105 |
AND NOW() < valid_until |
106 |
AND uuid = ?" |
107 |
); |
108 |
$sth->execute($uniqueKey); |
109 |
( $borrower_number, $username ) = $sth->fetchrow; |
110 |
|
111 |
#validate password length & match |
96 |
#validate password length & match |
112 |
if ( ($borrower_number) |
97 |
if ( ($borrower_number) |
113 |
&& ( $password eq $repeatPassword ) |
98 |
&& ( $password eq $repeatPassword ) |
Lines 116-123
elsif ( $query->param('passwordReset') ) {
Link Here
|
116 |
changepassword( $username, $borrower_number, hash_password($password) ); |
101 |
changepassword( $username, $borrower_number, hash_password($password) ); |
117 |
|
102 |
|
118 |
#remove entry |
103 |
#remove entry |
119 |
my $sth = $dbh->prepare("DELETE FROM borrower_password_recovery WHERE uuid = ? or NOW() > valid_until"); |
104 |
my $schema = Koha::Database->new->schema; |
120 |
$sth->execute($uniqueKey); |
105 |
my $rs = $schema->resultset('BorrowerPasswordRecovery')->search({-or => [uuid => $uniqueKey, valid_until => \'< NOW()']}); |
|
|
106 |
$rs->delete; |
121 |
|
107 |
|
122 |
$template->param( |
108 |
$template->param( |
123 |
password_reset_done => 1, |
109 |
password_reset_done => 1, |
Lines 148-166
elsif ( $query->param('passwordReset') ) {
Link Here
|
148 |
} |
134 |
} |
149 |
elsif ($uniqueKey) { #reset password form |
135 |
elsif ($uniqueKey) { #reset password form |
150 |
#check if the link is valid |
136 |
#check if the link is valid |
151 |
my $sth = $dbh->prepare( |
137 |
( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey); |
152 |
"SELECT borrower_password_recovery.borrowernumber, userid |
138 |
|
153 |
FROM borrower_password_recovery, borrowers |
|
|
154 |
WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber |
155 |
AND NOW() < valid_until |
156 |
AND uuid = ?" |
157 |
); |
158 |
$sth->execute($uniqueKey); |
159 |
( $borrower_number, $username ) = $sth->fetchrow; |
160 |
if ( !$borrower_number ) { |
139 |
if ( !$borrower_number ) { |
161 |
$errLinkNotValid = 1; |
140 |
$errLinkNotValid = 1; |
162 |
} |
141 |
} |
163 |
warn "INLIBRO username $username"; |
142 |
|
164 |
$template->param( |
143 |
$template->param( |
165 |
new_password => 1, |
144 |
new_password => 1, |
166 |
minPassLength => $minPassLength, |
145 |
minPassLength => $minPassLength, |
Lines 175-230
else { #password recovery form (to send email)
Link Here
|
175 |
} |
154 |
} |
176 |
|
155 |
|
177 |
output_html_with_http_headers $query, $cookie, $template->output; |
156 |
output_html_with_http_headers $query, $cookie, $template->output; |
178 |
|
|
|
179 |
# |
180 |
# It creates an email using the templates and send it to the user, using the specified email |
181 |
# |
182 |
sub SendPasswordRecoveryEmail { |
183 |
my $borrower = shift; # from GetMember |
184 |
my $userEmail = shift; #to_address (the one specified in the request) |
185 |
my $query = shift; #only required to determine if 'http' or 'https' |
186 |
my $update = shift; |
187 |
|
188 |
my $dbh = C4::Context->dbh; |
189 |
|
190 |
# generate UUID |
191 |
my @chars = ("A".."Z", "a".."z", "0".."9"); |
192 |
my $uuid_str; |
193 |
$uuid_str .= $chars[rand @chars] for 1..32; |
194 |
|
195 |
# insert into database |
196 |
my $expirydate = DateTime->now(time_zone => C4::Context->tz())->add( days => 2 ); |
197 |
if($update){ |
198 |
my $sth = $dbh->prepare( 'UPDATE borrower_password_recovery set uuid=?, valid_until=? where borrowernumber=? '); |
199 |
$sth->execute($uuid_str, $expirydate->datetime(), $borrower->{'borrowernumber'}); |
200 |
} else { |
201 |
my $sth = $dbh->prepare( 'INSERT INTO borrower_password_recovery VALUES (?, ?, ?)'); |
202 |
$sth->execute($borrower->{'borrowernumber'}, $uuid_str, $expirydate->datetime()); |
203 |
} |
204 |
|
205 |
# create link |
206 |
my $protocol = $query->https() ? "https://" : "http://"; |
207 |
my $uuidLink = $protocol . C4::Context->preference( 'OPACBaseURL' ) . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str"; |
208 |
|
209 |
# prepare the email |
210 |
my $letter = C4::Letters::GetPreparedLetter ( |
211 |
module => 'members', |
212 |
letter_code => 'PASSWORD_RESET', |
213 |
branchcode => $borrower->{branchcode}, |
214 |
tables => {borrowers => $borrower}, |
215 |
substitute => {passwordreseturl => $uuidLink}, |
216 |
); |
217 |
|
218 |
# define to/from emails |
219 |
my $kohaEmail = C4::Context->preference( 'KohaAdminEmailAddress' ); # from |
220 |
|
221 |
C4::Letters::EnqueueLetter( { |
222 |
letter => $letter, |
223 |
borrowernumber => $borrower->{'borrowernumber'}, |
224 |
to_address => $userEmail, |
225 |
from_address => $kohaEmail, |
226 |
message_transport_type => 'email', |
227 |
} ); |
228 |
|
229 |
return 1; |
230 |
} |
231 |
- |