Bug 15830 - Move Rotating Collections actions into a drop-down list
[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::Members qw(changepassword);
9 use C4::Output;
10 use C4::Context;
11 use C4::Passwordrecovery
12   qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
13 use Koha::AuthUtils qw(hash_password);
14 use Koha::Borrowers;
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');
35 my $borrower_number;
36
37 #errors
38 my $hasError;
39
40 #email form error
41 my $errNoBorrowerFound;
42 my $errNoBorrowerEmail;
43 my $errAlreadyStartRecovery;
44 my $errTooManyEmailFound;
45 my $errBadEmail;
46
47 #new password form error
48 my $errLinkNotValid;
49 my $errPassNotMatch;
50 my $errPassTooShort;
51
52 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
53
54     #try with the main email
55     $email ||= '';    # avoid undef
56     my $borrower;
57     my $search_results;
58
59     # Find the borrower by his userid or email
60     if ($username) {
61         $search_results = [ Koha::Borrowers->search( { userid => $username } ) ];
62     }
63     elsif ($email) {
64         $search_results = [ Koha::Borrowers->search( { -or => { email => $email, emailpro => $email, B_email  => $email } } ) ];
65     }
66     if ( not $search_results || scalar @$search_results > 1 ) {
67         $hasError           = 1;
68         $errNoBorrowerFound = 1;
69     }
70     elsif ( $borrower = shift @$search_results ) {    # One matching borrower
71         $username ||= $borrower->userid;
72         my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
73
74         # Is the given email one of the borrower's ?
75         if ( $email && !( grep { $_ eq $email } @emails ) ) {
76             $hasError    = 1;
77             $errNoBorrowerFound = 1;
78         }
79
80 # If we dont have an email yet. Get one of the borrower's email or raise an error.
81 # FIXME: That ugly shift-grep contraption.
82 # $email = shift [ grep { length() } @emails ]
83 # It's supposed to get a non-empty string from the @emails array. There's surely a simpler way
84         elsif ( !$email && !( $email = shift [ grep { length() } @emails ] ) ) {
85             $hasError           = 1;
86             $errNoBorrowerFound = 1;
87         }
88
89 # Check if a password reset already issued for this borrower AND we are not asking for a new email
90         elsif ( ValidateBorrowernumber( $borrower->borrowernumber )
91             && !$query->param('resendEmail') )
92         {
93             $hasError                = 1;
94             $errAlreadyStartRecovery = 1;
95         }
96     }
97     else {    # 0 matching borrower
98         $hasError           = 1;
99         $errNoBorrowerFound = 1;
100     }
101     if ($hasError) {
102         $template->param(
103             hasError                => 1,
104             errNoBorrowerFound      => $errNoBorrowerFound,
105             errTooManyEmailFound    => $errTooManyEmailFound,
106             errAlreadyStartRecovery => $errAlreadyStartRecovery,
107             errBadEmail             => $errBadEmail,
108             errNoBorrowerEmail      => $errNoBorrowerEmail,
109             password_recovery       => 1,
110             email                   => HTML::Entities::encode($email),
111             username                => $username
112         );
113     }
114     elsif ( SendPasswordRecoveryEmail( $borrower, $email, $query->param('resendEmail') ) ) {    # generate uuid and send recovery email
115         $template->param(
116             mail_sent => 1,
117             email     => $email
118         );
119     }
120     else {    # if it doesn't work....
121         $template->param(
122             password_recovery => 1,
123             sendmailError     => 1
124         );
125     }
126 }
127 elsif ( $query->param('passwordReset') ) {
128     ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
129
130     #validate password length & match
131     if (   ($borrower_number)
132         && ( $password eq $repeatPassword )
133         && ( length($password) >= $minPassLength ) )
134     {    #apply changes
135         changepassword( $username, $borrower_number, hash_password($password) );
136         CompletePasswordRecovery($uniqueKey);
137         $template->param(
138             password_reset_done => 1,
139             username            => $username
140         );
141     }
142     else {    #errors
143         if ( !$borrower_number ) {    #parameters not valid
144             $errLinkNotValid = 1;
145         }
146         elsif ( $password ne $repeatPassword ) {    #passwords does not match
147             $errPassNotMatch = 1;
148         }
149         elsif ( length($password) < $minPassLength ) {    #password too short
150             $errPassTooShort = 1;
151         }
152         $template->param(
153             new_password    => 1,
154             minPassLength   => $minPassLength,
155             email           => $email,
156             uniqueKey       => $uniqueKey,
157             errLinkNotValid => $errLinkNotValid,
158             errPassNotMatch => $errPassNotMatch,
159             errPassTooShort => $errPassTooShort,
160             hasError        => 1
161         );
162     }
163 }
164 elsif ($uniqueKey) {    #reset password form
165                         #check if the link is valid
166     ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
167
168     if ( !$borrower_number ) {
169         $errLinkNotValid = 1;
170     }
171
172     $template->param(
173         new_password    => 1,
174         minPassLength   => $minPassLength,
175         email           => $email,
176         uniqueKey       => $uniqueKey,
177         username        => $username,
178         errLinkNotValid => $errLinkNotValid,
179         hasError        => ( $errLinkNotValid ? 1 : 0 ),
180     );
181 }
182 else {    #password recovery form (to send email)
183     $template->param( password_recovery => 1 );
184 }
185
186 output_html_with_http_headers $query, $cookie, $template->output;