Bug 17301 - Add callnumber to label-edit-batch.pl
[koha.git] / opac / opac-password-recovery.pl
blob9560c388cef50f1ea56642166f429433440e30d8
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use CGI;
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);
12 use Koha::Patrons;
13 use Koha::AuthUtils qw(hash_password);
14 use Koha::Patrons;
15 my $query = new CGI;
16 use HTML::Entities;
18 my ( $template, $dummy, $cookie ) = get_template_and_user(
20 template_name => "opac-password-recovery.tt",
21 query => $query,
22 type => "opac",
23 authnotrequired => 1,
24 debug => 1,
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;
37 #errors
38 my $hasError;
40 #email form error
41 my $errNoBorrowerFound;
42 my $errNoBorrowerEmail;
43 my $errAlreadyStartRecovery;
44 my $errTooManyEmailFound;
45 my $errBadEmail;
47 #new password form error
48 my $errLinkNotValid;
49 my $errPassNotMatch;
50 my $errPassTooShort;
52 if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
54 #try with the main email
55 $email ||= ''; # avoid undef
56 my $borrower;
57 my $search_results = [];
59 # Find the borrower by his userid or email
60 if ($username) {
61 $search_results = [ Koha::Patrons->search( { userid => $username } ) ];
63 elsif ($email) {
64 $search_results = [ Koha::Patrons->search( { -or => { email => $email, emailpro => $email, B_email => $email } } ) ];
66 if ( not $search_results || scalar @$search_results > 1 ) {
67 $hasError = 1;
68 $errNoBorrowerFound = 1;
70 elsif ( $borrower = shift @$search_results ) { # One matching borrower
71 $username ||= $borrower->userid;
72 my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
74 my $firstNonEmptyEmail = '';
75 foreach my $address ( @emails ) {
76 $firstNonEmptyEmail = $address if length $address;
77 last if $firstNonEmptyEmail;
80 # Is the given email one of the borrower's ?
81 if ( $email && !( grep { $_ eq $email } @emails ) ) {
82 $hasError = 1;
83 $errNoBorrowerFound = 1;
86 # If we dont have an email yet. Get one of the borrower's email or raise an error.
87 elsif ( !$email && !( $email = $firstNonEmptyEmail ) ) {
88 $hasError = 1;
89 $errNoBorrowerEmail = 1;
92 # Check if a password reset already issued for this borrower AND we are not asking for a new email
93 elsif ( ValidateBorrowernumber( $borrower->borrowernumber )
94 && !$query->param('resendEmail') )
96 $hasError = 1;
97 $errAlreadyStartRecovery = 1;
100 else { # 0 matching borrower
101 $hasError = 1;
102 $errNoBorrowerFound = 1;
104 if ($hasError) {
105 $template->param(
106 hasError => 1,
107 errNoBorrowerFound => $errNoBorrowerFound,
108 errTooManyEmailFound => $errTooManyEmailFound,
109 errAlreadyStartRecovery => $errAlreadyStartRecovery,
110 errBadEmail => $errBadEmail,
111 errNoBorrowerEmail => $errNoBorrowerEmail,
112 password_recovery => 1,
113 email => HTML::Entities::encode($email),
114 username => $username
117 elsif ( SendPasswordRecoveryEmail( $borrower, $email, $query->param('resendEmail') ) ) { # generate uuid and send recovery email
118 $template->param(
119 mail_sent => 1,
120 email => $email
123 else { # if it doesn't work....
124 $template->param(
125 password_recovery => 1,
126 sendmailError => 1
130 elsif ( $query->param('passwordReset') ) {
131 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
133 #validate password length & match
134 if ( ($borrower_number)
135 && ( $password eq $repeatPassword )
136 && ( length($password) >= $minPassLength ) )
137 { #apply changes
138 Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
139 CompletePasswordRecovery($uniqueKey);
140 $template->param(
141 password_reset_done => 1,
142 username => $username
145 else { #errors
146 if ( !$borrower_number ) { #parameters not valid
147 $errLinkNotValid = 1;
149 elsif ( $password ne $repeatPassword ) { #passwords does not match
150 $errPassNotMatch = 1;
152 elsif ( length($password) < $minPassLength ) { #password too short
153 $errPassTooShort = 1;
155 $template->param(
156 new_password => 1,
157 minPassLength => $minPassLength,
158 email => $email,
159 uniqueKey => $uniqueKey,
160 errLinkNotValid => $errLinkNotValid,
161 errPassNotMatch => $errPassNotMatch,
162 errPassTooShort => $errPassTooShort,
163 hasError => 1
167 elsif ($uniqueKey) { #reset password form
168 #check if the link is valid
169 ( $borrower_number, $username ) = GetValidLinkInfo($uniqueKey);
171 if ( !$borrower_number ) {
172 $errLinkNotValid = 1;
175 $template->param(
176 new_password => 1,
177 minPassLength => $minPassLength,
178 email => $email,
179 uniqueKey => $uniqueKey,
180 username => $username,
181 errLinkNotValid => $errLinkNotValid,
182 hasError => ( $errLinkNotValid ? 1 : 0 ),
185 else { #password recovery form (to send email)
186 $template->param( password_recovery => 1 );
189 output_html_with_http_headers $query, $cookie, $template->output;