648c40758ea7cf7a9974bcc92863ac58fa1519c7
[koha.git] / opac / opac-password-recovery.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use CGI;
5
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;
17
18 my ( $template, $dummy, $cookie ) = get_template_and_user(
19     {
20         template_name   => "opac-password-recovery.tt",
21         query           => $query,
22         type            => "opac",
23         authnotrequired => 1,
24         debug           => 1,
25     }
26 );
27
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;
35
36 #errors
37 my $hasError;
38
39 #email form error
40 my $errNoBorrowerFound;
41 my $errNoBorrowerEmail;
42 my $errMultipleAccountsForEmail;
43 my $errAlreadyStartRecovery;
44 my $errTooManyEmailFound;
45 my $errBadEmail;
46
47 #new password form error
48 my $errLinkNotValid;
49
50 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
51
52     #try with the main email
53     my $borrower;
54     my $search_results;
55
56     # Find the borrower by userid, card number, or email
57     if ($username) {
58         $search_results = Koha::Patrons->search( { -or => { userid => $username, cardnumber => $username } } );
59     }
60     elsif ($email) {
61         $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email  => $email } } );
62     }
63
64     if ( !defined $search_results || $search_results->count < 1) {
65         $hasError           = 1;
66         $errNoBorrowerFound = 1;
67     }
68     elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
69         $hasError           = 1;
70         $errNoBorrowerFound = 1;
71     }
72     elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
73         $hasError           = 1;
74         $errMultipleAccountsForEmail = 1;
75     }
76     elsif ( $borrower = $search_results->next() ) {    # One matching borrower
77         my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
78
79         my $firstNonEmptyEmail = '';
80         foreach my $address ( @emails ) {
81             $firstNonEmptyEmail = $address if length $address;
82             last if $firstNonEmptyEmail;
83         }
84
85         # Is the given email one of the borrower's ?
86         if ( $email && !( grep { $_ eq $email } @emails ) ) {
87             $hasError    = 1;
88             $errNoBorrowerFound = 1;
89         }
90
91         # If there is no given email, and there is no email on record
92         elsif ( !$email && !$firstNonEmptyEmail ) {
93             $hasError           = 1;
94             $errNoBorrowerEmail = 1;
95         }
96
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;
102             }
103             else {
104                 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
105             }
106         }
107     }
108     else {    # 0 matching borrower
109         $hasError           = 1;
110         $errNoBorrowerFound = 1;
111     }
112     if ($hasError) {
113         $template->param(
114             hasError                => 1,
115             errNoBorrowerFound      => $errNoBorrowerFound,
116             errTooManyEmailFound    => $errTooManyEmailFound,
117             errAlreadyStartRecovery => $errAlreadyStartRecovery,
118             errBadEmail             => $errBadEmail,
119             errNoBorrowerEmail      => $errNoBorrowerEmail,
120             errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
121             password_recovery       => 1,
122             email                   => HTML::Entities::encode($email),
123             username                => $username
124         );
125     }
126     elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) {    # generate uuid and send recovery email
127         $template->param(
128             mail_sent => 1,
129             email     => $email
130         );
131     }
132     else {    # if it doesn't work....
133         $template->param(
134             hasError          => 1,
135             password_recovery => 1,
136             sendmailError     => 1
137         );
138     }
139 }
140 elsif ( $query->param('passwordReset') ) {
141     ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
142
143     my $error;
144     if ( not $borrower_number ) {
145         $error = 'errLinkNotValid';
146     } elsif ( $password ne $repeatPassword ) {
147         $error = 'errPassNotMatch';
148     } else {
149         my ( $is_valid, $err) = Koha::AuthUtils::is_password_valid( $password );
150         unless ( $is_valid ) {
151             $error = 'password_too_short' if $err eq 'too_short';
152             $error = 'password_too_weak' if $err eq 'too_weak';
153             $error = 'password_has_whitespaces' if $err eq 'has_whitespaces';
154         } else {
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
160             );
161         }
162     }
163     if ( $error ) {
164         $template->param(
165             new_password => 1,
166             email        => $email,
167             uniqueKey    => $uniqueKey,
168             hasError     => 1,
169             $error       => 1,
170         );
171     }
172 }
173 elsif ($uniqueKey) {    #reset password form
174                         #check if the link is valid
175     ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
176
177     if ( !$borrower_number ) {
178         $errLinkNotValid = 1;
179     }
180
181     $template->param(
182         new_password    => 1,
183         email           => $email,
184         uniqueKey       => $uniqueKey,
185         username        => $username,
186         errLinkNotValid => $errLinkNotValid,
187         hasError        => ( $errLinkNotValid ? 1 : 0 ),
188     );
189 }
190 else {    #password recovery form (to send email)
191     $template->param( password_recovery => 1 );
192 }
193
194 output_html_with_http_headers $query, $cookie, $template->output;