]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/PasswordReset.pm
4c57a74cd9fae4aa34c896b2f54f6c168b33df0d
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / PasswordReset.pm
1 package OpenILS::WWW::PasswordReset;
2
3 # Copyright (C) 2010 Laurentian University
4 # Dan Scott <dscott@laurentian.ca>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20 use strict; use warnings;
21
22 use Apache2::Log;
23 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
24 use APR::Const    -compile => qw(:error SUCCESS);
25 use Apache2::RequestRec ();
26 use Apache2::RequestIO ();
27 use Apache2::RequestUtil;
28 use CGI;
29 use Template;
30
31 use OpenSRF::EX qw(:try);
32 use OpenSRF::Utils qw/:datetime/;
33 use OpenSRF::Utils::Cache;
34 use OpenSRF::System;
35 use OpenSRF::AppSession;
36
37 use OpenILS::Utils::Fieldmapper;
38 use OpenSRF::Utils::Logger qw/$logger/;
39 use OpenILS::Application::AppUtils;
40 use OpenILS::Utils::CStoreEditor qw/:funcs/;
41
42 my $log = 'OpenSRF::Utils::Logger';
43 my $U = 'OpenILS::Application::AppUtils';
44
45 my ($bootstrap, $actor, $templates);
46 my $i18n = {};
47
48 sub child_init {
49     OpenSRF::System->bootstrap_client( config_file => $bootstrap );
50     
51     my $conf = OpenSRF::Utils::SettingsClient->new();
52     my $idl = $conf->config_value("IDL");
53     Fieldmapper->import(IDL => $idl);
54     $templates = $conf->config_value("dirs", "templates");
55     $actor = OpenSRF::AppSession->create('open-ils.actor');
56     load_i18n();
57 }
58
59 sub password_reset {
60     my $apache = shift;
61     return Apache2::Const::DECLINED if (-e $apache->filename);
62
63     $apache->content_type('text/html');
64
65         my $cgi = new CGI;
66     my $ctx = {};
67
68     $ctx->{'uri'} = $apache->uri;
69
70     # Get our locale from the URL
71     (my $locale = $apache->path_info) =~ s{^.*?/([a-z]{2}-[A-Z]{2})/.*?$}{$1};
72     if (!$locale) {
73         $locale = 'en-US';
74     }
75
76     # If locale exists, use it; otherwise fall back to en-US
77     if (exists $i18n->{$locale}) {
78         $ctx->{'i18n'} = $i18n->{$locale};
79     } else {
80         $ctx->{'i18n'} = $i18n->{'en-US'};
81     }
82
83     my $tt = Template->new({
84         INCLUDE_PATH => $templates
85     }) || die "$Template::ERROR\n";
86
87     # Get our UUID: if no UUID, then display barcode / username / email prompt
88     (my $uuid = $apache->path_info) =~ s{^/$locale/([^/]*?)$}{$1};
89     $logger->info("Password reset: UUID = $uuid");
90
91     if (!$uuid) {
92         request_password_reset($apache, $cgi, $tt, $ctx);
93     } else {
94         reset_password($apache, $cgi, $tt, $ctx, $uuid);
95     }
96 }
97
98 sub reset_password {
99     my ($apache, $cgi, $tt, $ctx, $uuid) = @_;
100
101     my $password_1 = $cgi->param('pwd1');
102     my $password_2 = $cgi->param('pwd2');
103
104     $ctx->{'title'} = $ctx->{'i18n'}{'TITLE'};
105     $ctx->{'password_prompt'} = $ctx->{'i18n'}{'PASSWORD_PROMPT'};
106     $ctx->{'password_prompt2'} = $ctx->{'i18n'}{'PASSWORD_PROMPT2'};
107
108     # In case non-matching passwords slip through our funky Web interface
109     if ($password_1 and $password_2 and ($password_1 ne $password_2)) {
110         $ctx->{'status'} = {
111             style => 'error',
112             msg => $ctx->{'i18n'}{'NO_MATCH'}
113         };
114         $tt->process('password-reset/reset-form.tt2', $ctx)
115             || die $tt->error();
116         return Apache2::Const::DECLINED;
117     }
118
119     if ($password_1 and $password_2 and ($password_1 eq $password_2)) {
120         my $response = $actor->request('open-ils.actor.patron.password_reset.commit', $uuid, $password_1)->gather();
121         if (ref($response) && $response->{'textcode'}) {
122
123             if ($response->{'textcode'} eq 'PATRON_NOT_AN_ACTIVE_PASSWORD_RESET_REQUEST') {
124                 $ctx->{'status'} = { 
125                     style => 'error',
126                     msg => $ctx->{'i18n'}{'NOT_ACTIVE'}
127
128                 };
129             }
130             if ($response->{'textcode'} eq 'PATRON_PASSWORD_WAS_NOT_STRONG') {
131                 $ctx->{'status'} = { 
132                     style => 'error',
133                     msg => $ctx->{'i18n'}{'NOT_STRONG'}
134
135                 };
136             }
137             $tt->process('password-reset/reset-form.tt2', $ctx)
138                 || die $tt->error();
139             return Apache2::Const::DECLINED;
140         }
141         $ctx->{'status'} = { 
142             style => 'success',
143             msg => $ctx->{'i18n'}{'SUCCESS'}
144         };
145     }
146
147     # Either the password change was successful, or this is their first time through
148     $tt->process('password-reset/reset-form.tt2', $ctx)
149         || die $tt->error();
150
151     return Apache2::Const::OK;
152 }
153
154 # Load our localized strings - lame, need to convert to Locale::Maketext
155 sub load_i18n {
156     foreach my $string_bundle (glob("$templates/password-reset/strings.*")) {
157         open(I18NFH, '<', $string_bundle);
158         (my $locale = $string_bundle) =~ s/^.*\.([a-z]{2}-[A-Z]{2})$/$1/;
159         $logger->debug("Loaded locale [$locale] from file: [$string_bundle]");
160         while(<I18NFH>) {
161             my ($string_id, $string) = ($_ =~ m/^(.+?)=(.*?)$/);
162             $i18n->{$locale}{$string_id} = $string;
163         }
164         close(I18NFH);
165     }
166 }
167
168 sub request_password_reset {
169     my ($apache, $cgi, $tt, $ctx) = @_;
170
171     my $barcode = $cgi->param('barcode');
172     my $username = $cgi->param('username');
173     my $email = $cgi->param('email');
174
175     if (!($barcode or $username or $email)) {
176         $ctx->{'status'} = {
177             style => 'plain',
178             msg => $ctx->{'i18n'}{'IDENTIFY_YOURSELF'}
179         };
180         $tt->process('password-reset/request-form.tt2', $ctx)
181             || die $tt->error();
182         return Apache2::Const::OK;
183     } elsif ($barcode) {
184         my $response = $actor->request('open-ils.actor.patron.password_reset.request', 'barcode', $barcode)->gather();
185         $ctx->{'status'} = {
186             style => 'plain',
187             msg => $ctx->{'i18n'}{'REQUEST_SUCCESS'}
188         };
189         # Hide form
190         $tt->process('password-reset/request-form.tt2', $ctx)
191             || die $tt->error();
192         return Apache2::Const::OK;
193     } elsif ($username) {
194         my $response = $actor->request('open-ils.actor.patron.password_reset.request', 'username', $username)->gather();
195         $ctx->{'status'} = {
196             style => 'plain',
197             msg => $ctx->{'i18n'}{'REQUEST_SUCCESS'}
198         };
199         # Hide form
200         $tt->process('password-reset/request-form.tt2', $ctx)
201             || die $tt->error();
202         return Apache2::Const::OK;
203     }
204 }
205
206 1;
207
208 # vim: et:ts=4:sw=4