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 $minPassLength = C4
::Context
->preference('minPasswordLength');
32 my $id = $query->param('id');
33 my $uniqueKey = $query->param('uniqueKey');
34 my $username = $query->param('username');
41 my $errNoBorrowerFound;
42 my $errNoBorrowerEmail;
43 my $errMultipleAccountsForEmail;
44 my $errAlreadyStartRecovery;
45 my $errTooManyEmailFound;
48 #new password form error
53 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
55 #try with the main email
56 $email ||= ''; # avoid undef
60 # Find the borrower by his userid or email
62 $search_results = Koha
::Patrons
->search( { userid
=> $username } );
65 $search_results = Koha
::Patrons
->search( { -or => { email
=> $email, emailpro
=> $email, B_email
=> $email } } );
68 if ( not $search_results || $search_results->count < 1) {
70 $errNoBorrowerFound = 1;
72 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
74 $errNoBorrowerFound = 1;
76 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
78 $errMultipleAccountsForEmail = 1;
80 elsif ( $borrower = $search_results->next() ) { # One matching borrower
81 $username ||= $borrower->userid;
82 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
84 my $firstNonEmptyEmail = '';
85 foreach my $address ( @emails ) {
86 $firstNonEmptyEmail = $address if length $address;
87 last if $firstNonEmptyEmail;
90 # Is the given email one of the borrower's ?
91 if ( $email && !( grep { $_ eq $email } @emails ) ) {
93 $errNoBorrowerFound = 1;
96 # If we dont have an email yet. Get one of the borrower's email or raise an error.
97 elsif ( !$email && !( $email = $firstNonEmptyEmail ) ) {
99 $errNoBorrowerEmail = 1;
102 # Check if a password reset already issued for this borrower AND we are not asking for a new email
103 elsif ( not $query->param('resendEmail') ) {
104 if ( ValidateBorrowernumber
( $borrower->borrowernumber ) ) {
106 $errAlreadyStartRecovery = 1;
109 DeleteExpiredPasswordRecovery
( $borrower->borrowernumber );
113 else { # 0 matching borrower
115 $errNoBorrowerFound = 1;
120 errNoBorrowerFound
=> $errNoBorrowerFound,
121 errTooManyEmailFound
=> $errTooManyEmailFound,
122 errAlreadyStartRecovery
=> $errAlreadyStartRecovery,
123 errBadEmail
=> $errBadEmail,
124 errNoBorrowerEmail
=> $errNoBorrowerEmail,
125 errMultipleAccountsForEmail
=> $errMultipleAccountsForEmail,
126 password_recovery
=> 1,
127 email
=> HTML
::Entities
::encode
($email),
128 username
=> $username
131 elsif ( SendPasswordRecoveryEmail
( $borrower, $email, $query->param('resendEmail') ) ) { # generate uuid and send recovery email
137 else { # if it doesn't work....
139 password_recovery
=> 1,
144 elsif ( $query->param('passwordReset') ) {
145 ( $borrower_number, $username ) = GetValidLinkInfo
($uniqueKey);
147 #validate password length & match
148 if ( ($borrower_number)
149 && ( $password eq $repeatPassword )
150 && ( length($password) >= $minPassLength ) )
152 Koha
::Patrons
->find($borrower_number)->update_password( $username, hash_password
($password) );
153 CompletePasswordRecovery
($uniqueKey);
155 password_reset_done
=> 1,
156 username
=> $username
160 if ( !$borrower_number ) { #parameters not valid
161 $errLinkNotValid = 1;
163 elsif ( $password ne $repeatPassword ) { #passwords does not match
164 $errPassNotMatch = 1;
166 elsif ( length($password) < $minPassLength ) { #password too short
167 $errPassTooShort = 1;
171 minPassLength
=> $minPassLength,
173 uniqueKey
=> $uniqueKey,
174 errLinkNotValid
=> $errLinkNotValid,
175 errPassNotMatch
=> $errPassNotMatch,
176 errPassTooShort
=> $errPassTooShort,
181 elsif ($uniqueKey) { #reset password form
182 #check if the link is valid
183 ( $borrower_number, $username ) = GetValidLinkInfo
($uniqueKey);
185 if ( !$borrower_number ) {
186 $errLinkNotValid = 1;
191 minPassLength
=> $minPassLength,
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;