1 package OpenILS::WWW::PasswordReset;
3 # Copyright (C) 2010 Laurentian University
4 # Dan Scott <dscott@laurentian.ca>
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.
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.
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.
20 use strict; use warnings;
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;
31 use OpenSRF::EX qw(:try);
32 use OpenSRF::Utils qw/:datetime/;
33 use OpenSRF::Utils::Cache;
35 use OpenSRF::AppSession;
37 use OpenILS::Utils::Fieldmapper;
38 use OpenSRF::Utils::Logger qw/$logger/;
39 use OpenILS::Application::AppUtils;
40 use OpenILS::Utils::CStoreEditor qw/:funcs/;
42 my $log = 'OpenSRF::Utils::Logger';
43 my $U = 'OpenILS::Application::AppUtils';
45 my ($bootstrap, $actor, $templates);
47 my $init_done = 0; # has child_init been called?
55 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
57 my $conf = OpenSRF::Utils::SettingsClient->new();
58 my $idl = $conf->config_value("IDL");
59 Fieldmapper->import(IDL => $idl);
60 $templates = $conf->config_value("dirs", "templates");
61 $actor = OpenSRF::AppSession->create('open-ils.actor');
64 return Apache2::Const::OK;
70 child_init() unless $init_done;
72 return Apache2::Const::DECLINED if (-e $apache->filename);
74 $apache->content_type('text/html');
79 $ctx->{'uri'} = $apache->uri;
81 # Get our locale from the URL
82 (my $locale = $apache->path_info) =~ s{^.*?/([a-z]{2}-[A-Z]{2})/.*?$}{$1};
87 # If locale exists, use it; otherwise fall back to en-US
88 if (exists $i18n->{$locale}) {
89 $ctx->{'i18n'} = $i18n->{$locale};
91 $ctx->{'i18n'} = $i18n->{'en-US'};
94 my $tt = Template->new({
95 INCLUDE_PATH => $templates
96 }) || die "$Template::ERROR\n";
98 # Get our UUID: if no UUID, then display barcode / username / email prompt
99 (my $uuid = $apache->path_info) =~ s{^/$locale/([^/]*?)$}{$1};
100 $logger->info("Password reset: UUID = $uuid");
103 request_password_reset($apache, $cgi, $tt, $ctx);
105 reset_password($apache, $cgi, $tt, $ctx, $uuid);
110 my ($apache, $cgi, $tt, $ctx, $uuid) = @_;
112 my $password_1 = $cgi->param('pwd1');
113 my $password_2 = $cgi->param('pwd2');
115 $ctx->{'title'} = $ctx->{'i18n'}{'TITLE'};
116 $ctx->{'password_prompt'} = $ctx->{'i18n'}{'PASSWORD_PROMPT'};
117 $ctx->{'password_prompt2'} = $ctx->{'i18n'}{'PASSWORD_PROMPT2'};
119 # In case non-matching passwords slip through our funky Web interface
120 if ($password_1 and $password_2 and ($password_1 ne $password_2)) {
123 msg => $ctx->{'i18n'}{'NO_MATCH'}
125 $tt->process('password-reset/reset-form.tt2', $ctx)
127 return Apache2::Const::OK;
130 if ($password_1 and $password_2 and ($password_1 eq $password_2)) {
131 my $response = $actor->request('open-ils.actor.patron.password_reset.commit', $uuid, $password_1)->gather();
132 if (ref($response) && $response->{'textcode'}) {
134 if ($response->{'textcode'} eq 'PATRON_NOT_AN_ACTIVE_PASSWORD_RESET_REQUEST') {
137 msg => $ctx->{'i18n'}{'NOT_ACTIVE'}
141 if ($response->{'textcode'} eq 'PATRON_PASSWORD_WAS_NOT_STRONG') {
144 msg => $ctx->{'i18n'}{'NOT_STRONG'}
148 $tt->process('password-reset/reset-form.tt2', $ctx)
150 return Apache2::Const::OK;
154 msg => $ctx->{'i18n'}{'SUCCESS'}
158 # Either the password change was successful, or this is their first time through
159 $tt->process('password-reset/reset-form.tt2', $ctx)
162 return Apache2::Const::OK;
165 # Load our localized strings - lame, need to convert to Locale::Maketext
167 foreach my $string_bundle (glob("$templates/password-reset/strings.*")) {
168 open(I18NFH, '<', $string_bundle);
169 (my $locale = $string_bundle) =~ s/^.*\.([a-z]{2}-[A-Z]{2})$/$1/;
170 $logger->debug("Loaded locale [$locale] from file: [$string_bundle]");
172 my ($string_id, $string) = ($_ =~ m/^(.+?)=(.*?)$/);
173 $i18n->{$locale}{$string_id} = $string;
179 sub request_password_reset {
180 my ($apache, $cgi, $tt, $ctx) = @_;
182 my $barcode = $cgi->param('barcode');
183 my $username = $cgi->param('username');
184 my $email = $cgi->param('email');
186 if (!($barcode or $username or $email)) {
189 msg => $ctx->{'i18n'}{'IDENTIFY_YOURSELF'}
191 $tt->process('password-reset/request-form.tt2', $ctx)
193 return Apache2::Const::OK;
195 my $response = $actor->request('open-ils.actor.patron.password_reset.request', 'barcode', $barcode)->gather();
198 msg => $ctx->{'i18n'}{'REQUEST_SUCCESS'}
201 $tt->process('password-reset/request-form.tt2', $ctx)
203 return Apache2::Const::OK;
204 } elsif ($username) {
205 my $response = $actor->request('open-ils.actor.patron.password_reset.request', 'username', $username)->gather();
208 msg => $ctx->{'i18n'}{'REQUEST_SUCCESS'}
211 $tt->process('password-reset/request-form.tt2', $ctx)
213 return Apache2::Const::OK;