9 use C4::Members qw(changepassword);
12 use C4::Passwordrecovery
13 qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
14 use Koha::AuthUtils qw(hash_password);
19 my ( $template, $dummy, $cookie ) = get_template_and_user(
21 template_name => "opac-password-recovery.tt",
29 my $email = $query->param('email') // q{};
30 my $password = $query->param('password');
31 my $repeatPassword = $query->param('repeatPassword');
32 my $minPassLength = C4::Context->preference('minPasswordLength');
33 my $id = $query->param('id');
34 my $uniqueKey = $query->param('uniqueKey');
35 my $username = $query->param('username');
42 my $errNoBorrowerFound;
43 my $errNoBorrowerEmail;
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::Borrowers->search( { userid => $username } ) ];
65 $search_results = [ Koha::Borrowers->search( { -or => { email => $email, emailpro => $email, B_email => $email } } ) ];
67 if ( not $search_results ) {
69 $errNoBorrowerFound = 1;
71 elsif ( scalar @$search_results > 1 ) { # Many matching borrowers
73 $errTooManyEmailFound = 1;
75 elsif ( $borrower = shift @$search_results ) { # One matching borrower
76 $username ||= $borrower->userid;
77 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
79 # Is the given email one of the borrower's ?
80 if ( $email && !( grep { $_ eq $email } @emails ) ) {
85 # If we dont have an email yet. Get one of the borrower's email or raise an error.
86 # FIXME: That ugly shift-grep contraption.
87 # $email = shift [ grep { length() } @emails ]
88 # It's supposed to get a non-empty string from the @emails array. There's surely a simpler way
89 elsif ( !$email && !( $email = shift [ grep { length() } @emails ] ) ) {
91 $errNoBorrowerEmail = 1;
94 # Check if a password reset already issued for this borrower AND we are not asking for a new email
95 elsif ( ValidateBorrowernumber( $borrower->borrowernumber )
96 && !$query->param('resendEmail') )
99 $errAlreadyStartRecovery = 1;
102 else { # 0 matching borrower
104 $errNoBorrowerFound = 1;
109 errNoBorrowerFound => $errNoBorrowerFound,
110 errTooManyEmailFound => $errTooManyEmailFound,
111 errAlreadyStartRecovery => $errAlreadyStartRecovery,
112 errBadEmail => $errBadEmail,
113 errNoBorrowerEmail => $errNoBorrowerEmail,
114 password_recovery => 1,
115 email => HTML::Entities::encode($email),
116 username => $username
119 elsif ( SendPasswordRecoveryEmail( $borrower, $email, $query->param('resendEmail') ) ) { # generate uuid and send recovery email
125 else { # if it doesn't work....
127 password_recovery => 1,
132 elsif ( $query->param('passwordReset') ) {
133 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
135 #validate password length & match
136 if ( ($borrower_number)
137 && ( $password eq $repeatPassword )
138 && ( length($password) >= $minPassLength ) )
140 changepassword( $username, $borrower_number, hash_password($password) );
141 CompletePasswordRecovery($uniqueKey);
143 password_reset_done => 1,
144 username => $username
148 if ( !$borrower_number ) { #parameters not valid
149 $errLinkNotValid = 1;
151 elsif ( $password ne $repeatPassword ) { #passwords does not match
152 $errPassNotMatch = 1;
154 elsif ( length($password) < $minPassLength ) { #password too short
155 $errPassTooShort = 1;
159 minPassLength => $minPassLength,
161 uniqueKey => $uniqueKey,
162 errLinkNotValid => $errLinkNotValid,
163 errPassNotMatch => $errPassNotMatch,
164 errPassTooShort => $errPassTooShort,
169 elsif ($uniqueKey) { #reset password form
170 #check if the link is valid
171 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
173 if ( !$borrower_number ) {
174 $errLinkNotValid = 1;
179 minPassLength => $minPassLength,
181 uniqueKey => $uniqueKey,
182 username => $username,
183 errLinkNotValid => $errLinkNotValid,
184 hasError => ( $errLinkNotValid ? 1 : 0 ),
187 else { #password recovery form (to send email)
188 $template->param( password_recovery => 1 );
191 output_html_with_http_headers $query, $cookie, $template->output;