10 use Koha::Patron::Password::Recovery
11 qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery DeleteExpiredPasswordRecovery);
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;
46 my $errResetForbidden;
48 #new password form error
51 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
53 #try with the main email
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 ( !defined $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
79 if ( $borrower->category->effective_reset_password ) {
81 my @emails = grep { $_ } ( $borrower->email, $borrower->emailpro, $borrower->B_email );
83 my $firstNonEmptyEmail;
84 $firstNonEmptyEmail = $emails[0] if @emails;
86 # Is the given email one of the borrower's ?
87 if ( $email && !( grep /^$email$/i, @emails ) ) {
89 $errNoBorrowerFound = 1;
92 # If there is no given email, and there is no email on record
93 elsif ( !$email && !$firstNonEmptyEmail ) {
95 $errNoBorrowerEmail = 1;
98 # Check if a password reset already issued for this
99 # 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 );
109 # Set the $email, if we don't have one.
110 if ( !$hasError && !$email ) {
111 $email = $firstNonEmptyEmail;
116 $errResetForbidden = 1;
119 else { # 0 matching borrower
121 $errNoBorrowerFound = 1;
126 errNoBorrowerFound => $errNoBorrowerFound,
127 errTooManyEmailFound => $errTooManyEmailFound,
128 errAlreadyStartRecovery => $errAlreadyStartRecovery,
129 errBadEmail => $errBadEmail,
130 errNoBorrowerEmail => $errNoBorrowerEmail,
131 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
132 errResetForbidden => $errResetForbidden,
133 password_recovery => 1,
134 email => HTML::Entities::encode($email),
135 username => $username
138 elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) { # generate uuid and send recovery email
144 else { # if it doesn't work....
147 password_recovery => 1,
152 elsif ( $query->param('passwordReset') ) {
153 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
156 if ( not $borrower_number ) {
157 $error = 'errLinkNotValid';
158 } elsif ( $password ne $repeatPassword ) {
159 $error = 'errPassNotMatch';
162 Koha::Patrons->find($borrower_number)->set_password({ password => $password });
164 CompletePasswordRecovery($uniqueKey);
166 password_reset_done => 1,
167 username => $username
171 if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
172 $error = 'password_too_short';
174 elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
175 $error = 'password_has_whitespaces';
177 elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
178 $error = 'password_too_weak';
186 uniqueKey => $uniqueKey,
192 elsif ($uniqueKey) { #reset password form
193 #check if the link is valid
194 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
196 if ( !$borrower_number ) {
197 $errLinkNotValid = 1;
203 uniqueKey => $uniqueKey,
204 username => $username,
205 errLinkNotValid => $errLinkNotValid,
206 hasError => ( $errLinkNotValid ? 1 : 0 ),
209 else { #password recovery form (to send email)
210 $template->param( password_recovery => 1 );
213 output_html_with_http_headers $query, $cookie, $template->output;