10 use Koha::Patron::Password::Recovery
11 qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery DeleteExpiredPasswordRecovery);
16 use List::Util qw/any/;
18 my ( $template, $dummy, $cookie ) = get_template_and_user(
20 template_name => "opac-password-recovery.tt",
27 my $email = $query->param('email') // q{};
28 my $password = $query->param('newPassword');
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 $errResetForbidden;
45 #new password form error
48 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
50 #try with the main email
53 # Find the borrower by userid, card number, or email
55 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username }, login_attempts => { '!=', Koha::Patron::ADMINISTRATIVE_LOCKOUT } } );
58 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email }, login_attempts => { '!=', Koha::Patron::ADMINISTRATIVE_LOCKOUT } } );
61 if ( !defined $search_results || $search_results->count < 1) {
63 $errNoBorrowerFound = 1;
65 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
67 $errNoBorrowerFound = 1;
69 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
71 $errMultipleAccountsForEmail = 1;
73 elsif ( $borrower = $search_results->next() ) { # One matching borrower
75 if ( $borrower->category->effective_reset_password ) {
77 my @emails = grep { $_ } ( $borrower->email, $borrower->emailpro, $borrower->B_email );
79 my $firstNonEmptyEmail;
80 $firstNonEmptyEmail = $emails[0] if @emails;
82 # Is the given email one of the borrower's ?
83 if ( $email && !( any { lc($_) eq lc($email) } @emails ) ) {
85 $errNoBorrowerFound = 1;
88 # If there is no given email, and there is no email on record
89 elsif ( !$email && !$firstNonEmptyEmail ) {
91 $errNoBorrowerEmail = 1;
94 # Check if a password reset already issued for this
95 # borrower AND we are not asking for a new email
96 elsif ( not $query->param('resendEmail') ) {
97 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
99 $errAlreadyStartRecovery = 1;
102 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
105 # Set the $email, if we don't have one.
106 if ( !$hasError && !$email ) {
107 $email = $firstNonEmptyEmail;
112 $errResetForbidden = 1;
115 else { # 0 matching borrower
117 $errNoBorrowerFound = 1;
122 errNoBorrowerFound => $errNoBorrowerFound,
123 errAlreadyStartRecovery => $errAlreadyStartRecovery,
124 errNoBorrowerEmail => $errNoBorrowerEmail,
125 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
126 errResetForbidden => $errResetForbidden,
127 password_recovery => 1,
128 email => HTML::Entities::encode($email),
129 username => $username
132 elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) { # generate uuid and send recovery email
138 else { # if it doesn't work....
141 password_recovery => 1,
146 elsif ( $query->param('passwordReset') ) {
147 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
150 my $min_password_length = C4::Context->preference('minPasswordPreference');
151 my $require_strong_password = C4::Context->preference('RequireStrongPassword');
152 if ( not $borrower_number ) {
153 $error = 'errLinkNotValid';
154 } elsif ( $password ne $repeatPassword ) {
155 $error = 'errPassNotMatch';
157 my $borrower = Koha::Patrons->find($borrower_number);
158 $min_password_length = $borrower->category->effective_min_password_length;
159 $require_strong_password = $borrower->category->effective_require_strong_password;
161 $borrower->set_password({ password => $password });
163 CompletePasswordRecovery($uniqueKey);
165 password_reset_done => 1,
166 username => $username
170 if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
171 $error = 'password_too_short';
173 elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
174 $error = 'password_has_whitespaces';
176 elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
177 $error = 'password_too_weak';
185 uniqueKey => $uniqueKey,
188 minPasswordLength => $min_password_length,
189 RequireStrongPassword => $require_strong_password
193 elsif ($uniqueKey) { #reset password form
194 #check if the link is valid
195 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
197 if ( !$borrower_number ) {
198 $errLinkNotValid = 1;
201 my $borrower = Koha::Patrons->find($borrower_number);
206 uniqueKey => $uniqueKey,
207 username => $username,
208 errLinkNotValid => $errLinkNotValid,
209 hasError => ( $errLinkNotValid ? 1 : 0 ),
210 minPasswordLength => $borrower->category->effective_min_password_length,
211 RequireStrongPassword => $borrower->category->effective_require_strong_password
214 else { #password recovery form (to send email)
215 $template->param( password_recovery => 1 );
218 output_html_with_http_headers $query, $cookie, $template->output;