Bug 12759: (follow-up) Using tabs for diff ways of adding records
[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;
79         $firstNonEmptyEmail = $emails[0] if @emails;
80
81         # Is the given email one of the borrower's ?
82         if ( $email && !( grep /^$email$/i, @emails ) ) {
83             $hasError    = 1;
84             $errNoBorrowerFound = 1;
85         }
86
87         # If there is no given email, and there is no email on record
88         elsif ( !$email && !$firstNonEmptyEmail ) {
89             $hasError           = 1;
90             $errNoBorrowerEmail = 1;
91         }
92
93 # Check if a password reset already issued for this borrower AND we are not asking for a new email
94         elsif ( not $query->param('resendEmail') ) {
95             if ( ValidateBorrowernumber( $borrower->borrowernumber ) ) {
96                 $hasError                = 1;
97                 $errAlreadyStartRecovery = 1;
98             }
99             else {
100                 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
101             }
102         }
103         # Set the $email, if we don't have one.
104         if ( !$hasError && !$email ) {
105             $email = $firstNonEmptyEmail;
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         try {
150             Koha::Patrons->find($borrower_number)->set_password({ password => $password });
151
152             CompletePasswordRecovery($uniqueKey);
153             $template->param(
154                 password_reset_done => 1,
155                 username            => $username
156             );
157         }
158         catch {
159             if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
160                 $error = 'password_too_short';
161             }
162             elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
163                 $error = 'password_has_whitespaces';
164             }
165             elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
166                 $error = 'password_too_weak';
167             }
168         };
169     }
170     if ( $error ) {
171         $template->param(
172             new_password => 1,
173             email        => $email,
174             uniqueKey    => $uniqueKey,
175             hasError     => 1,
176             $error       => 1,
177         );
178     }
179 }
180 elsif ($uniqueKey) {    #reset password form
181                         #check if the link is valid
182     ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
183
184     if ( !$borrower_number ) {
185         $errLinkNotValid = 1;
186     }
187
188     $template->param(
189         new_password    => 1,
190         email           => $email,
191         uniqueKey       => $uniqueKey,
192         username        => $username,
193         errLinkNotValid => $errLinkNotValid,
194         hasError        => ( $errLinkNotValid ? 1 : 0 ),
195     );
196 }
197 else {    #password recovery form (to send email)
198     $template->param( password_recovery => 1 );
199 }
200
201 output_html_with_http_headers $query, $cookie, $template->output;