]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
TPac: integrated patron password reset
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use XML::LibXML;
4 use URI::Escape;
5 use Digest::MD5 qw(md5_hex);
6 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
7 use OpenSRF::AppSession;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::Utils qw/:datetime/;
10 use OpenSRF::Utils::JSON;
11 use OpenSRF::Utils::Logger qw/$logger/;
12 use OpenILS::Application::AppUtils;
13 use OpenILS::Utils::CStoreEditor qw/:funcs/;
14 use OpenILS::Utils::Fieldmapper;
15 use DateTime::Format::ISO8601;
16 use CGI qw(:all -utf8);
17
18 # EGCatLoader sub-modules 
19 use OpenILS::WWW::EGCatLoader::Util;
20 use OpenILS::WWW::EGCatLoader::Account;
21 use OpenILS::WWW::EGCatLoader::Search;
22 use OpenILS::WWW::EGCatLoader::Record;
23 use OpenILS::WWW::EGCatLoader::Container;
24
25 my $U = 'OpenILS::Application::AppUtils';
26
27 use constant COOKIE_SES => 'ses';
28 use constant COOKIE_ORIG_LOC => 'eg_orig_loc';
29
30 use constant COOKIE_ANON_CACHE => 'anoncache';
31 use constant ANON_CACHE_MYLIST => 'mylist';
32 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
33
34 sub new {
35     my($class, $apache, $ctx) = @_;
36
37     my $self = bless({}, ref($class) || $class);
38
39     $self->apache($apache);
40     $self->ctx($ctx);
41     $self->cgi(new CGI);
42
43     OpenILS::Utils::CStoreEditor->init; # just in case
44     $self->editor(new_editor());
45
46     return $self;
47 }
48
49
50 # current Apache2::RequestRec;
51 sub apache {
52     my($self, $apache) = @_;
53     $self->{apache} = $apache if $apache;
54     return $self->{apache};
55 }
56
57 # runtime / template context
58 sub ctx {
59     my($self, $ctx) = @_;
60     $self->{ctx} = $ctx if $ctx;
61     return $self->{ctx};
62 }
63
64 # cstore editor
65 sub editor {
66     my($self, $editor) = @_;
67     $self->{editor} = $editor if $editor;
68     return $self->{editor};
69 }
70
71 # CGI handle
72 sub cgi {
73     my($self, $cgi) = @_;
74     $self->{cgi} = $cgi if $cgi;
75     return $self->{cgi};
76 }
77
78
79 # -----------------------------------------------------------------------------
80 # Perform initial setup, load common data, then load page data
81 # -----------------------------------------------------------------------------
82 sub load {
83     my $self = shift;
84
85     $self->init_ro_object_cache;
86
87     my $stat = $self->load_common;
88     return $stat unless $stat == Apache2::Const::OK;
89
90     my $path = $self->apache->path_info;
91
92     (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
93         $path =~ /opac\/my(opac\/lists|list)/;
94
95     return $self->load_simple("home") if $path =~ m|opac/home|;
96     return $self->load_simple("advanced") if
97         $path =~ m:opac/(advanced|numeric|expert):;
98
99     return $self->load_rresults if $path =~ m|opac/results|;
100     return $self->load_record if $path =~ m|opac/record|;
101     return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
102
103     return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
104     return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
105     return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
106     return $self->load_mylist if $path =~ m|opac/mylist|;
107     return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
108
109     # ----------------------------------------------------------------
110     # Logout and login require SSL
111     # ----------------------------------------------------------------
112     if($path =~ m|opac/login|) {
113         return $self->redirect_ssl unless $self->cgi->https;
114         return $self->load_login unless $self->editor->requestor; # already logged in?
115
116         # This will be less confusing to users than to be shown a login form
117         # when they're already logged in.
118         return $self->generic_redirect(
119             sprintf(
120                 "https://%s%s/myopac/main",
121                 $self->apache->hostname, $self->ctx->{opac_root}
122             )
123         );
124     }
125
126     if($path =~ m|opac/logout|) {
127         #return Apache2::Const::FORBIDDEN unless $self->cgi->https; 
128         $self->apache->log->warn("catloader: logout called in non-secure context from " . 
129             ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
130         return $self->load_logout;
131     }
132
133     return $self->load_password_reset if $path =~ m|opac/password_reset|;
134
135     # ----------------------------------------------------------------
136     #  Everything below here requires SSL + authentication
137     # ----------------------------------------------------------------
138     return $self->redirect_auth
139         unless $self->cgi->https and $self->editor->requestor;
140
141     return $self->load_place_hold if $path =~ m|opac/place_hold|;
142     return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
143     return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
144     return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
145     return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
146     return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
147     return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
148     return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
149     return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
150     return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
151     return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
152     return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
153     return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
154     return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
155     return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
156     return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
157     return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
158     return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
159     return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
160     return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
161
162     return Apache2::Const::OK;
163 }
164
165
166 # -----------------------------------------------------------------------------
167 # Redirect to SSL equivalent of a given page
168 # -----------------------------------------------------------------------------
169 sub redirect_ssl {
170     my $self = shift;
171     my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
172     return $self->generic_redirect($new_page);
173 }
174
175 # -----------------------------------------------------------------------------
176 # If an authnticated resource is requested w/o auth, redirect to the login page,
177 # then return to the originally requrested resource upon successful login.
178 # -----------------------------------------------------------------------------
179 sub redirect_auth {
180     my $self = shift;
181     my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
182     my $redirect_to = uri_escape($self->apache->unparsed_uri);
183     return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
184 }
185
186 # -----------------------------------------------------------------------------
187 # Fall-through for loading a basic page
188 # -----------------------------------------------------------------------------
189 sub load_simple {
190     my ($self, $page) = @_;
191     $self->ctx->{page} = $page;
192
193     if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
194         # Special CGI variable from staff client; propagate henceforth as cookie
195         $self->apache->headers_out->add(
196             "Set-Cookie" => $self->cgi->cookie(
197                 -name => "patron_barcode",
198                 -path => "/",
199                 -secure => 1,
200                 -value => $patron_barcode,
201                 -expires => undef
202             )
203         );
204     }
205     return Apache2::Const::OK;
206 }
207
208 # -----------------------------------------------------------------------------
209 # Tests to see if the user is authenticated and sets some common context values
210 # -----------------------------------------------------------------------------
211 sub load_common {
212     my $self = shift;
213
214     my $e = $self->editor;
215     my $ctx = $self->ctx;
216
217     $ctx->{referer} = $self->cgi->referer;
218     $ctx->{path_info} = $self->cgi->path_info;
219     $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
220     $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
221     $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
222     $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
223     $ctx->{orig_loc} = $self->get_orig_loc;
224
225     # capture some commonly accessed pages
226     $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
227     $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
228
229     if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
230
231         if($e->checkauth) {
232
233             $ctx->{authtoken} = $e->authtoken;
234             $ctx->{authtime} = $e->authtime;
235             $ctx->{user} = $e->requestor;
236
237             $ctx->{user_stats} = $U->simplereq(
238                 'open-ils.actor', 
239                 'open-ils.actor.user.opac.vital_stats', 
240                 $e->authtoken, $e->requestor->id);
241
242         } else {
243
244             # if we encounter a stale authtoken, call load_logout 
245             # to clean up the cookie, then redirect the user to the
246             # originally requested page
247             return $self->load_logout($self->apache->unparsed_uri);
248         }
249     }
250
251     return Apache2::Const::OK;
252 }
253
254 # orig_loc (i.e. "original location") passed in as a URL 
255 # param will replace any existing orig_loc stored as a cookie.
256 sub get_orig_loc {
257     my $self = shift;
258
259     if(my $orig_loc = $self->cgi->param('orig_loc')) {
260         $self->apache->headers_out->add(
261             "Set-Cookie" => $self->cgi->cookie(
262                 -name => COOKIE_ORIG_LOC,
263                 -path => $self->ctx->{base_path},
264                 -value => $orig_loc,
265                 -expires => undef
266             )
267         );
268         return $orig_loc;
269     }
270
271     return $self->cgi->cookie(COOKIE_ORIG_LOC);
272 }
273
274
275
276 # -----------------------------------------------------------------------------
277 # Log in and redirect to the redirect_to URL (or home)
278 # -----------------------------------------------------------------------------
279 sub load_login {
280     my $self = shift;
281     my $cgi = $self->cgi;
282     my $ctx = $self->ctx;
283
284     $ctx->{page} = 'login';
285
286     my $username = $cgi->param('username');
287     my $password = $cgi->param('password');
288     my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
289     my $persist = $cgi->param('persist');
290
291     # initial log form only
292     return Apache2::Const::OK unless $username and $password;
293
294         my $seed = $U->simplereq(
295         'open-ils.auth', 
296                 'open-ils.auth.authenticate.init', $username);
297
298     my $args = {        
299         username => $username, 
300         password => md5_hex($seed . md5_hex($password)), 
301         type => ($persist) ? 'persist' : 'opac' 
302     };
303
304     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
305
306     $args->{barcode} = delete $args->{username} 
307         if $bc_regex and ($username =~ /$bc_regex/);
308
309         my $response = $U->simplereq(
310         'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
311
312     if($U->event_code($response)) { 
313         # login failed, report the reason to the template
314         $ctx->{login_failed_event} = $response;
315         return Apache2::Const::OK;
316     }
317
318     # login succeeded, redirect as necessary
319
320     my $acct = $self->apache->unparsed_uri;
321     $acct =~ s|/login|/myopac/main|;
322
323     return $self->generic_redirect(
324         $cgi->param('redirect_to') || $acct,
325         $cgi->cookie(
326             -name => COOKIE_SES,
327             -path => '/',
328             -secure => 1,
329             -value => $response->{payload}->{authtoken},
330             -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
331         )
332     );
333 }
334
335 # -----------------------------------------------------------------------------
336 # Log out and redirect to the home page
337 # -----------------------------------------------------------------------------
338 sub load_logout {
339     my $self = shift;
340     my $redirect_to = shift;
341
342     # If the user was adding anyting to an anonymous cache 
343     # while logged in, go ahead and clear it out.
344     $self->clear_anon_cache;
345
346     return $self->generic_redirect(
347         $redirect_to || $self->ctx->{home_page},
348         $self->cgi->cookie(
349             -name => COOKIE_SES,
350             -path => '/',
351             -value => '',
352             -expires => '-1h'
353         )
354     );
355 }
356
357 1;
358