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 $id = $query->param('id');
32 my $uniqueKey = $query->param('uniqueKey');
33 my $username = $query->param('username') // q{};
40 my $errNoBorrowerFound;
41 my $errNoBorrowerEmail;
42 my $errMultipleAccountsForEmail;
43 my $errAlreadyStartRecovery;
44 my $errTooManyEmailFound;
47 #new password form error
50 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
52 #try with the main email
56 # Find the borrower by userid, card number, or email
58 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username } } );
61 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email } } );
64 if ( !defined $search_results || $search_results->count < 1) {
66 $errNoBorrowerFound = 1;
68 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
70 $errNoBorrowerFound = 1;
72 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
74 $errMultipleAccountsForEmail = 1;
76 elsif ( $borrower = $search_results->next() ) { # One matching borrower
77 my @emails = grep { $_ } ( $borrower->email, $borrower->emailpro, $borrower->B_email );
79 my $firstNonEmptyEmail;
80 $firstNonEmptyEmail = $emails[0] if @emails;
82 # Is the given email one of the borrower's ?
83 if ( $email && !( grep /^$email$/i, @emails ) ) {
85 $errNoBorrowerFound = 1;
88 # If there is no given email, and there is no email on record
89 elsif ( !$email && !$firstNonEmptyEmail ) {
91 $errNoBorrowerEmail = 1;
94 # Check if a password reset already issued for this borrower AND we are not asking for a new email
95 elsif ( not $query->param('resendEmail') ) {
96 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
98 $errAlreadyStartRecovery = 1;
101 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
104 # Set the $email, if we don't have one.
105 if ( !$hasError && !$email ) {
106 $email = $firstNonEmptyEmail;
109 else { # 0 matching borrower
111 $errNoBorrowerFound = 1;
116 errNoBorrowerFound => $errNoBorrowerFound,
117 errTooManyEmailFound => $errTooManyEmailFound,
118 errAlreadyStartRecovery => $errAlreadyStartRecovery,
119 errBadEmail => $errBadEmail,
120 errNoBorrowerEmail => $errNoBorrowerEmail,
121 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
122 password_recovery => 1,
123 email => HTML::Entities::encode($email),
124 username => $username
127 elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) { # generate uuid and send recovery email
133 else { # if it doesn't work....
136 password_recovery => 1,
141 elsif ( $query->param('passwordReset') ) {
142 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
145 if ( not $borrower_number ) {
146 $error = 'errLinkNotValid';
147 } elsif ( $password ne $repeatPassword ) {
148 $error = 'errPassNotMatch';
150 my ( $is_valid, $err) = Koha::AuthUtils::is_password_valid( $password );
151 unless ( $is_valid ) {
152 $error = 'password_too_short' if $err eq 'too_short';
153 $error = 'password_too_weak' if $err eq 'too_weak';
154 $error = 'password_has_whitespaces' if $err eq 'has_whitespaces';
156 Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
157 CompletePasswordRecovery($uniqueKey);
159 password_reset_done => 1,
160 username => $username
168 uniqueKey => $uniqueKey,
174 elsif ($uniqueKey) { #reset password form
175 #check if the link is valid
176 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
178 if ( !$borrower_number ) {
179 $errLinkNotValid = 1;
185 uniqueKey => $uniqueKey,
186 username => $username,
187 errLinkNotValid => $errLinkNotValid,
188 hasError => ( $errLinkNotValid ? 1 : 0 ),
191 else { #password recovery form (to send email)
192 $template->param( password_recovery => 1 );
195 output_html_with_http_headers $query, $cookie, $template->output;