Bug 21772: (QA follow-up) Fix setting variables to PROCESS in holds
[koha.git] / opac / opac-password-recovery.pl
blobe4b5a9195344cd09bb6434edb4f4c66d54bc20d8
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::Patrons;
14 my $query = new CGI;
15 use HTML::Entities;
16 use Try::Tiny;
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 = grep { $_ } ( $borrower->email, $borrower->emailpro, $borrower->B_email );
79 my $firstNonEmptyEmail;
80 $firstNonEmptyEmail = $emails[0] if @emails;
82 # Is the given email one of the borrower's ?
83 if ( $email && !( grep /^$email$/i, @emails ) ) {
84 $hasError = 1;
85 $errNoBorrowerFound = 1;
88 # If there is no given email, and there is no email on record
89 elsif ( !$email && !$firstNonEmptyEmail ) {
90 $hasError = 1;
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 ( not $query->param('resendEmail') ) {
96 if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
97 $hasError = 1;
98 $errAlreadyStartRecovery = 1;
100 else {
101 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
104 # Set the $email, if we don't have one.
105 if ( !$hasError && !$email ) {
106 $email = $firstNonEmptyEmail;
109 else { # 0 matching borrower
110 $hasError = 1;
111 $errNoBorrowerFound = 1;
113 if ($hasError) {
114 $template->param(
115 hasError => 1,
116 errNoBorrowerFound => $errNoBorrowerFound,
117 errTooManyEmailFound => $errTooManyEmailFound,
118 errAlreadyStartRecovery => $errAlreadyStartRecovery,
119 errBadEmail => $errBadEmail,
120 errNoBorrowerEmail => $errNoBorrowerEmail,
121 errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
122 password_recovery => 1,
123 email => HTML::Entities::encode($email),
124 username => $username
127 elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) { # generate uuid and send recovery email
128 $template->param(
129 mail_sent => 1,
130 email => $email
133 else { # if it doesn't work....
134 $template->param(
135 hasError => 1,
136 password_recovery => 1,
137 sendmailError => 1
141 elsif ( $query->param('passwordReset') ) {
142 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
144 my $error;
145 if ( not $borrower_number ) {
146 $error = 'errLinkNotValid';
147 } elsif ( $password ne $repeatPassword ) {
148 $error = 'errPassNotMatch';
149 } else {
150 try {
151 Koha::Patrons->find($borrower_number)->set_password({ password => $password });
153 CompletePasswordRecovery($uniqueKey);
154 $template->param(
155 password_reset_done => 1,
156 username => $username
159 catch {
160 if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
161 $error = 'password_too_short';
163 elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
164 $error = 'password_has_whitespaces';
166 elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
167 $error = 'password_too_weak';
171 if ( $error ) {
172 $template->param(
173 new_password => 1,
174 email => $email,
175 uniqueKey => $uniqueKey,
176 hasError => 1,
177 $error => 1,
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;
189 $template->param(
190 new_password => 1,
191 email => $email,
192 uniqueKey => $uniqueKey,
193 username => $username,
194 errLinkNotValid => $errLinkNotValid,
195 hasError => ( $errLinkNotValid ? 1 : 0 ),
198 else { #password recovery form (to send email)
199 $template->param( password_recovery => 1 );
202 output_html_with_http_headers $query, $cookie, $template->output;