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