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;
79 $firstNonEmptyEmail = $emails[0] if @emails;
81 # Is the given email one of the borrower's ?
82 if ( $email && !( grep /^$email$/i, @emails ) ) {
84 $errNoBorrowerFound = 1;
87 # If there is no given email, and there is no email on record
88 elsif ( !$email && !$firstNonEmptyEmail ) {
90 $errNoBorrowerEmail = 1;
93 # Check if a password reset already issued for this borrower AND we are not asking for a new email
94 elsif ( not $query->param('resendEmail') ) {
95 if ( ValidateBorrowernumber
( $borrower->borrowernumber ) ) {
97 $errAlreadyStartRecovery = 1;
100 DeleteExpiredPasswordRecovery
( $borrower->borrowernumber );
103 # Set the $email, if we don't have one.
104 if ( !$hasError && !$email ) {
105 $email = $firstNonEmptyEmail;
108 else { # 0 matching borrower
110 $errNoBorrowerFound = 1;
115 errNoBorrowerFound
=> $errNoBorrowerFound,
116 errTooManyEmailFound
=> $errTooManyEmailFound,
117 errAlreadyStartRecovery
=> $errAlreadyStartRecovery,
118 errBadEmail
=> $errBadEmail,
119 errNoBorrowerEmail
=> $errNoBorrowerEmail,
120 errMultipleAccountsForEmail
=> $errMultipleAccountsForEmail,
121 password_recovery
=> 1,
122 email
=> HTML
::Entities
::encode
($email),
123 username
=> $username
126 elsif ( SendPasswordRecoveryEmail
( $borrower, $email, scalar $query->param('resendEmail') ) ) { # generate uuid and send recovery email
132 else { # if it doesn't work....
135 password_recovery
=> 1,
140 elsif ( $query->param('passwordReset') ) {
141 ( $borrower_number, $username ) = GetValidLinkInfo
($uniqueKey);
144 if ( not $borrower_number ) {
145 $error = 'errLinkNotValid';
146 } elsif ( $password ne $repeatPassword ) {
147 $error = 'errPassNotMatch';
149 my ( $is_valid, $err) = Koha
::AuthUtils
::is_password_valid
( $password );
150 unless ( $is_valid ) {
151 $error = 'password_too_short' if $err eq 'too_short';
152 $error = 'password_too_weak' if $err eq 'too_weak';
153 $error = 'password_has_whitespaces' if $err eq 'has_whitespaces';
155 Koha
::Patrons
->find($borrower_number)->update_password( $username, $password );
156 CompletePasswordRecovery
($uniqueKey);
158 password_reset_done
=> 1,
159 username
=> $username
167 uniqueKey
=> $uniqueKey,
173 elsif ($uniqueKey) { #reset password form
174 #check if the link is valid
175 ( $borrower_number, $username ) = GetValidLinkInfo
($uniqueKey);
177 if ( !$borrower_number ) {
178 $errLinkNotValid = 1;
184 uniqueKey
=> $uniqueKey,
185 username
=> $username,
186 errLinkNotValid
=> $errLinkNotValid,
187 hasError
=> ( $errLinkNotValid ?
1 : 0 ),
190 else { #password recovery form (to send email)
191 $template->param( password_recovery
=> 1 );
194 output_html_with_http_headers
$query, $cookie, $template->output;