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
52 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
54 #try with the main email
55 $email ||= ''; # avoid undef
59 # Find the borrower by userid, card number, or email
61 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username } } );
64 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email } } );
67 if ( not $search_results || $search_results->count < 1) {
69 $errNoBorrowerFound = 1;
71 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
73 $errNoBorrowerFound = 1;
75 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
77 $errMultipleAccountsForEmail = 1;
79 elsif ( $borrower = $search_results->next() ) { # One matching borrower
80 $username ||= $borrower->userid;
81 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
83 my $firstNonEmptyEmail = '';
84 foreach my $address ( @emails ) {
85 $firstNonEmptyEmail = $address if length $address;
86 last if $firstNonEmptyEmail;
89 # Is the given email one of the borrower's ?
90 if ( $email && !( grep { $_ eq $email } @emails ) ) {
92 $errNoBorrowerFound = 1;
95 # If we dont have an email yet. Get one of the borrower's email or raise an error.
96 elsif ( !$email && !( $email = $firstNonEmptyEmail ) ) {
98 $errNoBorrowerEmail = 1;
101 # Check if a password reset already issued for this borrower AND we are not asking for a new email
102 elsif ( not $query->param('resendEmail') ) {
103 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
105 $errAlreadyStartRecovery = 1;
108 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
112 else { # 0 matching borrower
114 $errNoBorrowerFound = 1;
119 errNoBorrowerFound => $errNoBorrowerFound,
120 errTooManyEmailFound => $errTooManyEmailFound,
121 errAlreadyStartRecovery => $errAlreadyStartRecovery,
122 errBadEmail => $errBadEmail,
123 errNoBorrowerEmail => $errNoBorrowerEmail,
124 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
125 password_recovery => 1,
126 email => HTML::Entities::encode($email),
127 username => $username
130 elsif ( SendPasswordRecoveryEmail( $borrower, $email, $query->param('resendEmail') ) ) { # generate uuid and send recovery email
136 else { # if it doesn't work....
139 password_recovery => 1,
144 elsif ( $query->param('passwordReset') ) {
145 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
147 my $minPassLength = C4::Context->preference('minPasswordLength');
148 $minPassLength = 3 if not $minPassLength or $minPassLength < 3;
149 #validate password length & match
150 if ( ($borrower_number)
151 && ( $password eq $repeatPassword )
152 && ( length($password) >= $minPassLength ) )
154 Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
155 CompletePasswordRecovery($uniqueKey);
157 password_reset_done => 1,
158 username => $username
162 if ( !$borrower_number ) { #parameters not valid
163 $errLinkNotValid = 1;
165 elsif ( $password ne $repeatPassword ) { #passwords does not match
166 $errPassNotMatch = 1;
168 elsif ( length($password) < $minPassLength ) { #password too short
169 $errPassTooShort = 1;
174 uniqueKey => $uniqueKey,
175 errLinkNotValid => $errLinkNotValid,
176 errPassNotMatch => $errPassNotMatch,
177 errPassTooShort => $errPassTooShort,
182 elsif ($uniqueKey) { #reset password form
183 #check if the link is valid
184 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
186 if ( !$borrower_number ) {
187 $errLinkNotValid = 1;
193 uniqueKey => $uniqueKey,
194 username => $username,
195 errLinkNotValid => $errLinkNotValid,
196 hasError => ( $errLinkNotValid ? 1 : 0 ),
199 else { #password recovery form (to send email)
200 $template->param( password_recovery => 1 );
203 output_html_with_http_headers $query, $cookie, $template->output;