8 use C4
::Members
qw(changepassword);
11 use C4
::Passwordrecovery
12 qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
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 $errAlreadyStartRecovery;
44 my $errTooManyEmailFound;
47 #new password form error
52 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
54 #try with the main email
55 $email ||= ''; # avoid undef
59 # Find the borrower by his userid or email
61 $search_results = [ Koha
::Borrowers
->search( { userid
=> $username } ) ];
64 $search_results = [ Koha
::Borrowers
->search( { -or => { email
=> $email, emailpro
=> $email, B_email
=> $email } } ) ];
66 if ( not $search_results || scalar @
$search_results > 1 ) {
68 $errNoBorrowerFound = 1;
70 elsif ( $borrower = shift @
$search_results ) { # One matching borrower
71 $username ||= $borrower->userid;
72 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
74 # Is the given email one of the borrower's ?
75 if ( $email && !( grep { $_ eq $email } @emails ) ) {
77 $errNoBorrowerFound = 1;
80 # If we dont have an email yet. Get one of the borrower's email or raise an error.
81 # FIXME: That ugly shift-grep contraption.
82 # $email = shift [ grep { length() } @emails ]
83 # It's supposed to get a non-empty string from the @emails array. There's surely a simpler way
84 elsif ( !$email && !( $email = shift [ grep { length() } @emails ] ) ) {
86 $errNoBorrowerFound = 1;
89 # Check if a password reset already issued for this borrower AND we are not asking for a new email
90 elsif ( ValidateBorrowernumber
( $borrower->borrowernumber )
91 && !$query->param('resendEmail') )
94 $errAlreadyStartRecovery = 1;
97 else { # 0 matching borrower
99 $errNoBorrowerFound = 1;
104 errNoBorrowerFound
=> $errNoBorrowerFound,
105 errTooManyEmailFound
=> $errTooManyEmailFound,
106 errAlreadyStartRecovery
=> $errAlreadyStartRecovery,
107 errBadEmail
=> $errBadEmail,
108 errNoBorrowerEmail
=> $errNoBorrowerEmail,
109 password_recovery
=> 1,
110 email
=> HTML
::Entities
::encode
($email),
111 username
=> $username
114 elsif ( SendPasswordRecoveryEmail
( $borrower, $email, $query->param('resendEmail') ) ) { # generate uuid and send recovery email
120 else { # if it doesn't work....
122 password_recovery
=> 1,
127 elsif ( $query->param('passwordReset') ) {
128 ( $borrower_number, $username ) = GetValidLinkInfo
($uniqueKey);
130 #validate password length & match
131 if ( ($borrower_number)
132 && ( $password eq $repeatPassword )
133 && ( length($password) >= $minPassLength ) )
135 changepassword
( $username, $borrower_number, hash_password
($password) );
136 CompletePasswordRecovery
($uniqueKey);
138 password_reset_done
=> 1,
139 username
=> $username
143 if ( !$borrower_number ) { #parameters not valid
144 $errLinkNotValid = 1;
146 elsif ( $password ne $repeatPassword ) { #passwords does not match
147 $errPassNotMatch = 1;
149 elsif ( length($password) < $minPassLength ) { #password too short
150 $errPassTooShort = 1;
154 minPassLength
=> $minPassLength,
156 uniqueKey
=> $uniqueKey,
157 errLinkNotValid
=> $errLinkNotValid,
158 errPassNotMatch
=> $errPassNotMatch,
159 errPassTooShort
=> $errPassTooShort,
164 elsif ($uniqueKey) { #reset password form
165 #check if the link is valid
166 ( $borrower_number, $username ) = GetValidLinkInfo
($uniqueKey);
168 if ( !$borrower_number ) {
169 $errLinkNotValid = 1;
174 minPassLength
=> $minPassLength,
176 uniqueKey
=> $uniqueKey,
177 username
=> $username,
178 errLinkNotValid
=> $errLinkNotValid,
179 hasError
=> ( $errLinkNotValid ?
1 : 0 ),
182 else { #password recovery form (to send email)
183 $template->param( password_recovery
=> 1 );
186 output_html_with_http_headers
$query, $cookie, $template->output;