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');
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
53 $email ||= ''; # avoid undef
57 # Find the borrower by userid, card number, or email
59 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username } } );
62 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email } } );
65 if ( not $search_results || $search_results->count < 1) {
67 $errNoBorrowerFound = 1;
69 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
71 $errNoBorrowerFound = 1;
73 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
75 $errMultipleAccountsForEmail = 1;
77 elsif ( $borrower = $search_results->next() ) { # One matching borrower
78 $username ||= $borrower->userid;
79 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
81 my $firstNonEmptyEmail = '';
82 foreach my $address ( @emails ) {
83 $firstNonEmptyEmail = $address if length $address;
84 last if $firstNonEmptyEmail;
87 # Is the given email one of the borrower's ?
88 if ( $email && !( grep { $_ eq $email } @emails ) ) {
90 $errNoBorrowerFound = 1;
93 # If we dont have an email yet. Get one of the borrower's email or raise an error.
94 elsif ( !$email && !( $email = $firstNonEmptyEmail ) ) {
96 $errNoBorrowerEmail = 1;
99 # Check if a password reset already issued for this borrower AND we are not asking for a new email
100 elsif ( not $query->param('resendEmail') ) {
101 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
103 $errAlreadyStartRecovery = 1;
106 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
110 else { # 0 matching borrower
112 $errNoBorrowerFound = 1;
117 errNoBorrowerFound => $errNoBorrowerFound,
118 errTooManyEmailFound => $errTooManyEmailFound,
119 errAlreadyStartRecovery => $errAlreadyStartRecovery,
120 errBadEmail => $errBadEmail,
121 errNoBorrowerEmail => $errNoBorrowerEmail,
122 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
123 password_recovery => 1,
124 email => HTML::Entities::encode($email),
125 username => $username
128 elsif ( SendPasswordRecoveryEmail( $borrower, $email, $query->param('resendEmail') ) ) { # generate uuid and send recovery email
134 else { # if it doesn't work....
137 password_recovery => 1,
142 elsif ( $query->param('passwordReset') ) {
143 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
146 if ( not $borrower_number ) {
147 $error = 'errLinkNotValid';
148 } elsif ( $password ne $repeatPassword ) {
149 $error = 'errPassNotMatch';
151 my ( $is_valid, $err) = Koha::AuthUtils::is_password_valid( $password );
152 unless ( $is_valid ) {
153 $error = 'password_too_short' if $err eq 'too_short';
154 $error = 'password_too_weak' if $err eq 'too_weak';
155 $error = 'password_has_whitespaces' if $err eq 'has_whitespaces';
157 Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
158 CompletePasswordRecovery($uniqueKey);
160 password_reset_done => 1,
161 username => $username
169 uniqueKey => $uniqueKey,
175 elsif ($uniqueKey) { #reset password form
176 #check if the link is valid
177 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
179 if ( !$borrower_number ) {
180 $errLinkNotValid = 1;
186 uniqueKey => $uniqueKey,
187 username => $username,
188 errLinkNotValid => $errLinkNotValid,
189 hasError => ( $errLinkNotValid ? 1 : 0 ),
192 else { #password recovery form (to send email)
193 $template->param( password_recovery => 1 );
196 output_html_with_http_headers $query, $cookie, $template->output;