Bug 18956: [QA Follow-up] Resolve a CGI::Param in list context warn
[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 $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;
36
37 #errors
38 my $hasError;
39
40 #email form error
41 my $errNoBorrowerFound;
42 my $errNoBorrowerEmail;
43 my $errMultipleAccountsForEmail;
44 my $errAlreadyStartRecovery;
45 my $errTooManyEmailFound;
46 my $errBadEmail;
47
48 #new password form error
49 my $errLinkNotValid;
50 my $errPassNotMatch;
51 my $errPassTooShort;
52
53 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
54
55     #try with the main email
56     my $borrower;
57     my $search_results;
58
59     # Find the borrower by his userid or email
60     if ($username) {
61         $search_results = Koha::Patrons->search( { userid => $username } );
62     }
63     elsif ($email) {
64         $search_results = Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email  => $email } } );
65     }
66
67     if ( !defined $search_results || $search_results->count < 1) {
68         $hasError           = 1;
69         $errNoBorrowerFound = 1;
70     }
71     elsif ( $username && $search_results->count > 1) { # Multiple accounts for username
72         $hasError           = 1;
73         $errNoBorrowerFound = 1;
74     }
75     elsif ( $email && $search_results->count > 1) { # Muliple accounts for E-Mail
76         $hasError           = 1;
77         $errMultipleAccountsForEmail = 1;
78     }
79     elsif ( $borrower = $search_results->next() ) {    # One matching borrower
80         my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
81
82         my $firstNonEmptyEmail = '';
83         foreach my $address ( @emails ) {
84             $firstNonEmptyEmail = $address if length $address;
85             last if $firstNonEmptyEmail;
86         }
87
88         # Is the given email one of the borrower's ?
89         if ( $email && !( grep { $_ eq $email } @emails ) ) {
90             $hasError    = 1;
91             $errNoBorrowerFound = 1;
92         }
93
94         # If there is no given email, and there is no email on record
95         elsif ( !$email && !$firstNonEmptyEmail ) {
96             $hasError           = 1;
97             $errNoBorrowerEmail = 1;
98         }
99
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;
105             }
106             else {
107                 DeleteExpiredPasswordRecovery( $borrower->borrowernumber );
108             }
109         }
110     }
111     else {    # 0 matching borrower
112         $hasError           = 1;
113         $errNoBorrowerFound = 1;
114     }
115     if ($hasError) {
116         $template->param(
117             hasError                => 1,
118             errNoBorrowerFound      => $errNoBorrowerFound,
119             errTooManyEmailFound    => $errTooManyEmailFound,
120             errAlreadyStartRecovery => $errAlreadyStartRecovery,
121             errBadEmail             => $errBadEmail,
122             errNoBorrowerEmail      => $errNoBorrowerEmail,
123             errMultipleAccountsForEmail => $errMultipleAccountsForEmail,
124             password_recovery       => 1,
125             email                   => HTML::Entities::encode($email),
126             username                => $username
127         );
128     }
129     elsif ( SendPasswordRecoveryEmail( $borrower, $email, scalar $query->param('resendEmail') ) ) {    # generate uuid and send recovery email
130         $template->param(
131             mail_sent => 1,
132             email     => $email
133         );
134     }
135     else {    # if it doesn't work....
136         $template->param(
137             password_recovery => 1,
138             sendmailError     => 1
139         );
140     }
141 }
142 elsif ( $query->param('passwordReset') ) {
143     ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
144
145     #validate password length & match
146     if (   ($borrower_number)
147         && ( $password eq $repeatPassword )
148         && ( length($password) >= $minPassLength ) )
149     {    #apply changes
150         Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
151         CompletePasswordRecovery($uniqueKey);
152         $template->param(
153             password_reset_done => 1,
154             username            => $username
155         );
156     }
157     else {    #errors
158         if ( !$borrower_number ) {    #parameters not valid
159             $errLinkNotValid = 1;
160         }
161         elsif ( $password ne $repeatPassword ) {    #passwords does not match
162             $errPassNotMatch = 1;
163         }
164         elsif ( length($password) < $minPassLength ) {    #password too short
165             $errPassTooShort = 1;
166         }
167         $template->param(
168             new_password    => 1,
169             minPassLength   => $minPassLength,
170             email           => $email,
171             uniqueKey       => $uniqueKey,
172             errLinkNotValid => $errLinkNotValid,
173             errPassNotMatch => $errPassNotMatch,
174             errPassTooShort => $errPassTooShort,
175             hasError        => 1
176         );
177     }
178 }
179 elsif ($uniqueKey) {    #reset password form
180                         #check if the link is valid
181     ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
182
183     if ( !$borrower_number ) {
184         $errLinkNotValid = 1;
185     }
186
187     $template->param(
188         new_password    => 1,
189         minPassLength   => $minPassLength,
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;