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",
28 my $email = $query->param('email') // q{};
29 my $password = $query->param('newPassword');
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 $errResetForbidden;
46 #new password form error
49 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
51 #try with the main email
54 # Find the borrower by userid, card number, or email
56 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username }, login_attempts => { '!=', Koha::Patron::ADMINISTRATIVE_LOCKOUT } } );
59 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email }, login_attempts => { '!=', Koha::Patron::ADMINISTRATIVE_LOCKOUT } } );
62 if ( !defined $search_results || $search_results->count < 1) {
64 $errNoBorrowerFound = 1;
66 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
68 $errNoBorrowerFound = 1;
70 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
72 $errMultipleAccountsForEmail = 1;
74 elsif ( $borrower = $search_results->next() ) { # One matching borrower
76 if ( $borrower->category->effective_reset_password ) {
78 my @emails = grep { $_ } ( $borrower->email, $borrower->emailpro, $borrower->B_email );
80 my $firstNonEmptyEmail;
81 $firstNonEmptyEmail = $emails[0] if @emails;
83 # Is the given email one of the borrower's ?
84 if ( $email && !( any { lc($_) eq lc($email) } @emails ) ) {
86 $errNoBorrowerFound = 1;
89 # If there is no given email, and there is no email on record
90 elsif ( !$email && !$firstNonEmptyEmail ) {
92 $errNoBorrowerEmail = 1;
95 # Check if a password reset already issued for this
96 # borrower AND we are not asking for a new email
97 elsif ( not $query->param('resendEmail') ) {
98 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
100 $errAlreadyStartRecovery = 1;
103 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
106 # Set the $email, if we don't have one.
107 if ( !$hasError && !$email ) {
108 $email = $firstNonEmptyEmail;
113 $errResetForbidden = 1;
116 else { # 0 matching borrower
118 $errNoBorrowerFound = 1;
123 errNoBorrowerFound => $errNoBorrowerFound,
124 errAlreadyStartRecovery => $errAlreadyStartRecovery,
125 errNoBorrowerEmail => $errNoBorrowerEmail,
126 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
127 errResetForbidden => $errResetForbidden,
128 password_recovery => 1,
129 email => HTML::Entities::encode($email),
130 username => $username
133 elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) { # generate uuid and send recovery email
139 else { # if it doesn't work....
142 password_recovery => 1,
147 elsif ( $query->param('passwordReset') ) {
148 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
151 my $min_password_length = C4::Context->preference('minPasswordPreference');
152 my $require_strong_password = C4::Context->preference('RequireStrongPassword');
153 if ( not $borrower_number ) {
154 $error = 'errLinkNotValid';
155 } elsif ( $password ne $repeatPassword ) {
156 $error = 'errPassNotMatch';
158 my $borrower = Koha::Patrons->find($borrower_number);
159 $min_password_length = $borrower->category->effective_min_password_length;
160 $require_strong_password = $borrower->category->effective_require_strong_password;
162 $borrower->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,
189 minPasswordLength => $min_password_length,
190 RequireStrongPassword => $require_strong_password
194 elsif ($uniqueKey) { #reset password form
195 #check if the link is valid
196 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
198 if ( !$borrower_number ) {
199 $errLinkNotValid = 1;
202 my $borrower = Koha::Patrons->find($borrower_number);
207 uniqueKey => $uniqueKey,
208 username => $username,
209 errLinkNotValid => $errLinkNotValid,
210 hasError => ( $errLinkNotValid ? 1 : 0 ),
211 minPasswordLength => $borrower->category->effective_min_password_length,
212 RequireStrongPassword => $borrower->category->effective_require_strong_password
215 else { #password recovery form (to send email)
216 $template->param( password_recovery => 1 );
219 output_html_with_http_headers $query, $cookie, $template->output;