10 use Koha
::Patron
::Password
::Recovery
11 qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery DeleteExpiredPasswordRecovery);
13 use Koha
::AuthUtils
qw(hash_password);
18 my ( $template, $dummy, $cookie ) = get_template_and_user
(
20 template_name
=> "opac-password-recovery.tt",
28 my $email = $query->param('email') // q{};
29 my $password = $query->param('password');
30 my $repeatPassword = $query->param('repeatPassword');
31 my $minPassLength = C4
::Context
->preference('minPasswordLength');
32 my $id = $query->param('id');
33 my $uniqueKey = $query->param('uniqueKey');
34 my $username = $query->param('username');
41 my $errNoBorrowerFound;
42 my $errNoBorrowerEmail;
43 my $errAlreadyStartRecovery;
44 my $errTooManyEmailFound;
47 #new password form error
52 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
54 #try with the main email
55 $email ||= ''; # avoid undef
57 my $search_results = [];
59 # Find the borrower by his userid or email
61 $search_results = [ Koha
::Patrons
->search( { userid
=> $username } ) ];
64 $search_results = [ Koha
::Patrons
->search( { -or => { email
=> $email, emailpro
=> $email, B_email
=> $email } } ) ];
66 if ( not $search_results || scalar @
$search_results > 1 ) {
68 $errNoBorrowerFound = 1;
70 elsif ( $borrower = shift @
$search_results ) { # One matching borrower
71 $username ||= $borrower->userid;
72 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
74 my $firstNonEmptyEmail = '';
75 foreach my $address ( @emails ) {
76 $firstNonEmptyEmail = $address if length $address;
77 last if $firstNonEmptyEmail;
80 # Is the given email one of the borrower's ?
81 if ( $email && !( grep { $_ eq $email } @emails ) ) {
83 $errNoBorrowerFound = 1;
86 # If we dont have an email yet. Get one of the borrower's email or raise an error.
87 elsif ( !$email && !( $email = $firstNonEmptyEmail ) ) {
89 $errNoBorrowerEmail = 1;
92 # Check if a password reset already issued for this borrower AND we are not asking for a new email
93 elsif ( not $query->param('resendEmail') ) {
94 if ( ValidateBorrowernumber
( $borrower->borrowernumber ) ) {
96 $errAlreadyStartRecovery = 1;
99 DeleteExpiredPasswordRecovery
( $borrower->borrowernumber );
103 else { # 0 matching borrower
105 $errNoBorrowerFound = 1;
110 errNoBorrowerFound
=> $errNoBorrowerFound,
111 errTooManyEmailFound
=> $errTooManyEmailFound,
112 errAlreadyStartRecovery
=> $errAlreadyStartRecovery,
113 errBadEmail
=> $errBadEmail,
114 errNoBorrowerEmail
=> $errNoBorrowerEmail,
115 password_recovery
=> 1,
116 email
=> HTML
::Entities
::encode
($email),
117 username
=> $username
120 elsif ( SendPasswordRecoveryEmail
( $borrower, $email, $query->param('resendEmail') ) ) { # generate uuid and send recovery email
126 else { # if it doesn't work....
128 password_recovery
=> 1,
133 elsif ( $query->param('passwordReset') ) {
134 ( $borrower_number, $username ) = GetValidLinkInfo
($uniqueKey);
136 #validate password length & match
137 if ( ($borrower_number)
138 && ( $password eq $repeatPassword )
139 && ( length($password) >= $minPassLength ) )
141 Koha
::Patrons
->find($borrower_number)->update_password( $username, hash_password
($password) );
142 CompletePasswordRecovery
($uniqueKey);
144 password_reset_done
=> 1,
145 username
=> $username
149 if ( !$borrower_number ) { #parameters not valid
150 $errLinkNotValid = 1;
152 elsif ( $password ne $repeatPassword ) { #passwords does not match
153 $errPassNotMatch = 1;
155 elsif ( length($password) < $minPassLength ) { #password too short
156 $errPassTooShort = 1;
160 minPassLength
=> $minPassLength,
162 uniqueKey
=> $uniqueKey,
163 errLinkNotValid
=> $errLinkNotValid,
164 errPassNotMatch
=> $errPassNotMatch,
165 errPassTooShort
=> $errPassTooShort,
170 elsif ($uniqueKey) { #reset password form
171 #check if the link is valid
172 ( $borrower_number, $username ) = GetValidLinkInfo
($uniqueKey);
174 if ( !$borrower_number ) {
175 $errLinkNotValid = 1;
180 minPassLength
=> $minPassLength,
182 uniqueKey
=> $uniqueKey,
183 username
=> $username,
184 errLinkNotValid
=> $errLinkNotValid,
185 hasError
=> ( $errLinkNotValid ?
1 : 0 ),
188 else { #password recovery form (to send email)
189 $template->param( password_recovery
=> 1 );
192 output_html_with_http_headers
$query, $cookie, $template->output;