6 use C4::Auth qw( get_template_and_user );
7 use C4::Output qw( output_html_with_http_headers );
9 use Koha::Patron::Password::Recovery qw(
10 CompletePasswordRecovery
11 DeleteExpiredPasswordRecovery
13 SendPasswordRecoveryEmail
14 ValidateBorrowernumber
19 use Try::Tiny qw( catch try );
20 use List::Util qw( any );
22 my ( $template, $dummy, $cookie ) = get_template_and_user(
24 template_name => "opac-password-recovery.tt",
31 my $op = $query->param('op') // q{};
32 my $email = $query->param('email') // q{};
33 my $password = $query->param('newPassword');
34 my $repeatPassword = $query->param('repeatPassword');
35 my $id = $query->param('id');
36 my $uniqueKey = $query->param('uniqueKey');
37 my $username = $query->param('username') // q{};
44 my $errNoBorrowerFound;
45 my $errNoBorrowerEmail;
46 my $errMultipleAccountsForEmail;
47 my $errAlreadyStartRecovery;
48 my $errResetForbidden;
50 #new password form error
53 if ( $op eq 'cud-sendEmail' || $op eq 'cud-resendEmail' ) {
55 #try with the main email
58 # Find the borrower by userid, card number, or email
60 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username }, login_attempts => { '!=', Koha::Patron::ADMINISTRATIVE_LOCKOUT } } );
63 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email }, login_attempts => { '!=', Koha::Patron::ADMINISTRATIVE_LOCKOUT } } );
66 if ( !defined $search_results || $search_results->count < 1) {
68 $errNoBorrowerFound = 1;
70 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
72 $errNoBorrowerFound = 1;
74 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
76 $errMultipleAccountsForEmail = 1;
78 elsif ( $borrower = $search_results->next() ) { # One matching borrower
80 if ( $borrower->category->effective_reset_password ) {
82 my @emails = grep { $_ } ( $borrower->email, $borrower->emailpro, $borrower->B_email );
84 my $firstNonEmptyEmail;
85 $firstNonEmptyEmail = $emails[0] if @emails;
87 # Is the given email one of the borrower's ?
88 if ( $email && !( any { lc($_) eq lc($email) } @emails ) ) {
90 $errNoBorrowerFound = 1;
93 # If there is no given email, and there is no email on record
94 elsif ( !$email && !$firstNonEmptyEmail ) {
96 $errNoBorrowerEmail = 1;
99 # Check if a password reset already issued for this
100 # borrower AND we are not asking for a new email
101 elsif ( $op ne 'cud-resendEmail' ) {
102 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
104 $errAlreadyStartRecovery = 1;
107 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
110 # Set the $email, if we don't have one.
111 if ( !$hasError && !$email ) {
112 $email = $firstNonEmptyEmail;
117 $errResetForbidden = 1;
120 else { # 0 matching borrower
122 $errNoBorrowerFound = 1;
127 errNoBorrowerFound => $errNoBorrowerFound,
128 errAlreadyStartRecovery => $errAlreadyStartRecovery,
129 errNoBorrowerEmail => $errNoBorrowerEmail,
130 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
131 errResetForbidden => $errResetForbidden,
132 password_recovery => 1,
133 email => HTML::Entities::encode($email),
134 username => $username
137 elsif ( SendPasswordRecoveryEmail( $borrower, $email ) ) { # generate uuid and send recovery email
143 else { # if it doesn't work....
146 password_recovery => 1,
151 elsif ( $query->param('passwordReset') ) {
152 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
155 my $min_password_length = C4::Context->preference('minPasswordPreference');
156 my $require_strong_password = C4::Context->preference('RequireStrongPassword');
157 if ( not $borrower_number ) {
158 $error = 'errLinkNotValid';
159 } elsif ( $password ne $repeatPassword ) {
160 $error = 'errPassNotMatch';
162 my $borrower = Koha::Patrons->find($borrower_number);
163 $min_password_length = $borrower->category->effective_min_password_length;
164 $require_strong_password = $borrower->category->effective_require_strong_password;
166 $borrower->set_password({ password => $password, action => 'RESET PASS' });
168 CompletePasswordRecovery($uniqueKey);
170 password_reset_done => 1,
171 username => $username
175 if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
176 $error = 'password_too_short';
178 elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
179 $error = 'password_has_whitespaces';
181 elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
182 $error = 'password_too_weak';
190 uniqueKey => $uniqueKey,
193 minPasswordLength => $min_password_length,
194 RequireStrongPassword => $require_strong_password
198 elsif ($uniqueKey) { #reset password form
199 #check if the link is valid
200 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
202 if ( !$borrower_number ) {
203 $errLinkNotValid = 1;
206 my $borrower = Koha::Patrons->find($borrower_number);
211 uniqueKey => $uniqueKey,
212 username => $username,
213 errLinkNotValid => $errLinkNotValid,
214 hasError => ( $errLinkNotValid ? 1 : 0 ),
215 minPasswordLength => $borrower ? $borrower->category->effective_min_password_length : undef,
216 RequireStrongPassword => $borrower ? $borrower->category->effective_require_strong_password : undef,
219 else { #password recovery form (to send email)
220 $template->param( password_recovery => 1 );
223 output_html_with_http_headers $query, $cookie, $template->output;