10 use Koha::Patron::Password::Recovery
11 qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery DeleteExpiredPasswordRecovery);
17 my ( $template, $dummy, $cookie ) = get_template_and_user(
19 template_name => "opac-password-recovery.tt",
27 my $email = $query->param('email') // q{};
28 my $password = $query->param('password');
29 my $repeatPassword = $query->param('repeatPassword');
30 my $id = $query->param('id');
31 my $uniqueKey = $query->param('uniqueKey');
32 my $username = $query->param('username') // q{};
39 my $errNoBorrowerFound;
40 my $errNoBorrowerEmail;
41 my $errMultipleAccountsForEmail;
42 my $errAlreadyStartRecovery;
43 my $errTooManyEmailFound;
46 #new password form error
49 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
51 #try with the main email
55 # Find the borrower by userid, card number, or email
57 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username } } );
60 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email } } );
63 if ( !defined $search_results || $search_results->count < 1) {
65 $errNoBorrowerFound = 1;
67 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
69 $errNoBorrowerFound = 1;
71 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
73 $errMultipleAccountsForEmail = 1;
75 elsif ( $borrower = $search_results->next() ) { # One matching borrower
76 my @emails = grep { $_ } ( $borrower->email, $borrower->emailpro, $borrower->B_email );
78 my $firstNonEmptyEmail = shift @emails;
80 # Is the given email one of the borrower's ?
81 if ( $email && !( grep /^$email$/i, @emails ) ) {
83 $errNoBorrowerFound = 1;
86 # If there is no given email, and there is no email on record
87 elsif ( !$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 );
102 # Set the $email, if we don't have one.
103 if ( !$hasError && !$email ) {
104 $email = $firstNonEmptyEmail;
107 else { # 0 matching borrower
109 $errNoBorrowerFound = 1;
114 errNoBorrowerFound => $errNoBorrowerFound,
115 errTooManyEmailFound => $errTooManyEmailFound,
116 errAlreadyStartRecovery => $errAlreadyStartRecovery,
117 errBadEmail => $errBadEmail,
118 errNoBorrowerEmail => $errNoBorrowerEmail,
119 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
120 password_recovery => 1,
121 email => HTML::Entities::encode($email),
122 username => $username
125 elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) { # generate uuid and send recovery email
131 else { # if it doesn't work....
134 password_recovery => 1,
139 elsif ( $query->param('passwordReset') ) {
140 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
143 if ( not $borrower_number ) {
144 $error = 'errLinkNotValid';
145 } elsif ( $password ne $repeatPassword ) {
146 $error = 'errPassNotMatch';
148 my ( $is_valid, $err) = Koha::AuthUtils::is_password_valid( $password );
149 unless ( $is_valid ) {
150 $error = 'password_too_short' if $err eq 'too_short';
151 $error = 'password_too_weak' if $err eq 'too_weak';
152 $error = 'password_has_whitespaces' if $err eq 'has_whitespaces';
154 Koha::Patrons->find($borrower_number)->update_password( $username, $password );
155 CompletePasswordRecovery($uniqueKey);
157 password_reset_done => 1,
158 username => $username
166 uniqueKey => $uniqueKey,
172 elsif ($uniqueKey) { #reset password form
173 #check if the link is valid
174 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
176 if ( !$borrower_number ) {
177 $errLinkNotValid = 1;
183 uniqueKey => $uniqueKey,
184 username => $username,
185 errLinkNotValid => $errLinkNotValid,
186 hasError => ( $errLinkNotValid ? 1 : 0 ),
189 else { #password recovery form (to send email)
190 $template->param( password_recovery => 1 );
193 output_html_with_http_headers $query, $cookie, $template->output;