Bug 19439: (follow-up) Remove four spaces from lines 133-149
[koha.git] / opac / opac-password-recovery.pl
blob456aa97ff56dcaf3739fc76c4f53e048578c21c7
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use CGI;
6 use C4::Auth;
7 use C4::Koha;
8 use C4::Output;
9 use C4::Context;
10 use Koha::Patron::Password::Recovery
11 qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery DeleteExpiredPasswordRecovery);
12 use Koha::Patrons;
13 use Koha::AuthUtils qw(hash_password);
14 use Koha::Patrons;
15 my $query = new CGI;
16 use HTML::Entities;
18 my ( $template, $dummy, $cookie ) = get_template_and_user(
20 template_name => "opac-password-recovery.tt",
21 query => $query,
22 type => "opac",
23 authnotrequired => 1,
24 debug => 1,
28 my $email = $query->param('email') // q{};
29 my $password = $query->param('password');
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{};
34 my $borrower_number;
36 #errors
37 my $hasError;
39 #email form error
40 my $errNoBorrowerFound;
41 my $errNoBorrowerEmail;
42 my $errMultipleAccountsForEmail;
43 my $errAlreadyStartRecovery;
44 my $errTooManyEmailFound;
45 my $errBadEmail;
47 #new password form error
48 my $errLinkNotValid;
50 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
52 #try with the main email
53 my $borrower;
54 my $search_results;
56 # Find the borrower by userid, card number, or email
57 if ($username) {
58 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username } } );
60 elsif ($email) {
61 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email } } );
64 if ( !defined $search_results || $search_results->count < 1) {
65 $hasError = 1;
66 $errNoBorrowerFound = 1;
68 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
69 $hasError = 1;
70 $errNoBorrowerFound = 1;
72 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
73 $hasError = 1;
74 $errMultipleAccountsForEmail = 1;
76 elsif ( $borrower = $search_results->next() ) { # One matching borrower
77 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
79 my $firstNonEmptyEmail = '';
80 foreach my $address ( @emails ) {
81 $firstNonEmptyEmail = $address if length $address;
82 last if $firstNonEmptyEmail;
85 # Is the given email one of the borrower's ?
86 if ( $email && !( grep { $_ eq $email } @emails ) ) {
87 $hasError = 1;
88 $errNoBorrowerFound = 1;
91 # If there is no given email, and there is no email on record
92 elsif ( !$email && !$firstNonEmptyEmail ) {
93 $hasError = 1;
94 $errNoBorrowerEmail = 1;
97 # Check if a password reset already issued for this borrower AND we are not asking for a new email
98 elsif ( not $query->param('resendEmail') ) {
99 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
100 $hasError = 1;
101 $errAlreadyStartRecovery = 1;
103 else {
104 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
107 # Set the $email, if we don't have one.
108 if ( !$hasError && !$email ) {
109 $email = $firstNonEmptyEmail;
112 else { # 0 matching borrower
113 $hasError = 1;
114 $errNoBorrowerFound = 1;
116 if ($hasError) {
117 $template->param(
118 hasError => 1,
119 errNoBorrowerFound => $errNoBorrowerFound,
120 errTooManyEmailFound => $errTooManyEmailFound,
121 errAlreadyStartRecovery => $errAlreadyStartRecovery,
122 errBadEmail => $errBadEmail,
123 errNoBorrowerEmail => $errNoBorrowerEmail,
124 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
125 password_recovery => 1,
126 email => HTML::Entities::encode($email),
127 username => $username
130 elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) { # generate uuid and send recovery email
131 $template->param(
132 mail_sent => 1,
133 email => $email
136 else { # if it doesn't work....
137 $template->param(
138 hasError => 1,
139 password_recovery => 1,
140 sendmailError => 1
144 elsif ( $query->param('passwordReset') ) {
145 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
147 my $error;
148 if ( not $borrower_number ) {
149 $error = 'errLinkNotValid';
150 } elsif ( $password ne $repeatPassword ) {
151 $error = 'errPassNotMatch';
152 } else {
153 my ( $is_valid, $err) = Koha::AuthUtils::is_password_valid( $password );
154 unless ( $is_valid ) {
155 $error = 'password_too_short' if $err eq 'too_short';
156 $error = 'password_too_weak' if $err eq 'too_weak';
157 $error = 'password_has_whitespaces' if $err eq 'has_whitespaces';
158 } else {
159 Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
160 CompletePasswordRecovery($uniqueKey);
161 $template->param(
162 password_reset_done => 1,
163 username => $username
167 if ( $error ) {
168 $template->param(
169 new_password => 1,
170 email => $email,
171 uniqueKey => $uniqueKey,
172 hasError => 1,
173 $error => 1,
177 elsif ($uniqueKey) { #reset password form
178 #check if the link is valid
179 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
181 if ( !$borrower_number ) {
182 $errLinkNotValid = 1;
185 $template->param(
186 new_password => 1,
187 email => $email,
188 uniqueKey => $uniqueKey,
189 username => $username,
190 errLinkNotValid => $errLinkNotValid,
191 hasError => ( $errLinkNotValid ? 1 : 0 ),
194 else { #password recovery form (to send email)
195 $template->param( password_recovery => 1 );
198 output_html_with_http_headers $query, $cookie, $template->output;