Bug 19971: Typo in the comments of parseQuery routine
[koha.git] / opac / opac-password-recovery.pl
blob159f7f46be5a4e0734b92f566e8ac7e010e035a0
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 $minPassLength = C4::Context->preference('minPasswordLength');
32 my $id = $query->param('id');
33 my $uniqueKey = $query->param('uniqueKey');
34 my $username = $query->param('username') // q{};
35 my $borrower_number;
37 #errors
38 my $hasError;
40 #email form error
41 my $errNoBorrowerFound;
42 my $errNoBorrowerEmail;
43 my $errMultipleAccountsForEmail;
44 my $errAlreadyStartRecovery;
45 my $errTooManyEmailFound;
46 my $errBadEmail;
48 #new password form error
49 my $errLinkNotValid;
50 my $errPassNotMatch;
51 my $errPassTooShort;
53 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
55 #try with the main email
56 my $borrower;
57 my $search_results;
59 # Find the borrower by userid, card number, or email
60 if ($username) {
61 $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username } } );
63 elsif ($email) {
64 $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email } } );
67 if ( !defined $search_results || $search_results->count < 1) {
68 $hasError = 1;
69 $errNoBorrowerFound = 1;
71 elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
72 $hasError = 1;
73 $errNoBorrowerFound = 1;
75 elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
76 $hasError = 1;
77 $errMultipleAccountsForEmail = 1;
79 elsif ( $borrower = $search_results->next() ) { # One matching borrower
80 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
82 my $firstNonEmptyEmail = '';
83 foreach my $address ( @emails ) {
84 $firstNonEmptyEmail = $address if length $address;
85 last if $firstNonEmptyEmail;
88 # Is the given email one of the borrower's ?
89 if ( $email && !( grep { $_ eq $email } @emails ) ) {
90 $hasError = 1;
91 $errNoBorrowerFound = 1;
94 # If there is no given email, and there is no email on record
95 elsif ( !$email && !$firstNonEmptyEmail ) {
96 $hasError = 1;
97 $errNoBorrowerEmail = 1;
100 # Check if a password reset already issued for this borrower AND we are not asking for a new email
101 elsif ( not $query->param('resendEmail') ) {
102 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
103 $hasError = 1;
104 $errAlreadyStartRecovery = 1;
106 else {
107 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
110 # Set the $email, if we don't have one.
111 if ( !$hasError && !$email ) {
112 $email = $firstNonEmptyEmail;
115 else { # 0 matching borrower
116 $hasError = 1;
117 $errNoBorrowerFound = 1;
119 if ($hasError) {
120 $template->param(
121 hasError => 1,
122 errNoBorrowerFound => $errNoBorrowerFound,
123 errTooManyEmailFound => $errTooManyEmailFound,
124 errAlreadyStartRecovery => $errAlreadyStartRecovery,
125 errBadEmail => $errBadEmail,
126 errNoBorrowerEmail => $errNoBorrowerEmail,
127 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
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
134 $template->param(
135 mail_sent => 1,
136 email => $email
139 else { # if it doesn't work....
140 $template->param(
141 hasError => 1,
142 password_recovery => 1,
143 sendmailError => 1
147 elsif ( $query->param('passwordReset') ) {
148 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
150 #validate password length & match
151 if ( ($borrower_number)
152 && ( $password eq $repeatPassword )
153 && ( length($password) >= $minPassLength ) )
154 { #apply changes
155 Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
156 CompletePasswordRecovery($uniqueKey);
157 $template->param(
158 password_reset_done => 1,
159 username => $username
162 else { #errors
163 if ( !$borrower_number ) { #parameters not valid
164 $errLinkNotValid = 1;
166 elsif ( $password ne $repeatPassword ) { #passwords does not match
167 $errPassNotMatch = 1;
169 elsif ( length($password) < $minPassLength ) { #password too short
170 $errPassTooShort = 1;
172 $template->param(
173 new_password => 1,
174 minPassLength => $minPassLength,
175 email => $email,
176 uniqueKey => $uniqueKey,
177 errLinkNotValid => $errLinkNotValid,
178 errPassNotMatch => $errPassNotMatch,
179 errPassTooShort => $errPassTooShort,
180 hasError => 1
184 elsif ($uniqueKey) { #reset password form
185 #check if the link is valid
186 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
188 if ( !$borrower_number ) {
189 $errLinkNotValid = 1;
192 $template->param(
193 new_password => 1,
194 minPassLength => $minPassLength,
195 email => $email,
196 uniqueKey => $uniqueKey,
197 username => $username,
198 errLinkNotValid => $errLinkNotValid,
199 hasError => ( $errLinkNotValid ? 1 : 0 ),
202 else { #password recovery form (to send email)
203 $template->param( password_recovery => 1 );
206 output_html_with_http_headers $query, $cookie, $template->output;