]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
LP#1570072: update hold notification methods upon preference changes
[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 OpenILS::Utils::DateTime 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 use Time::HiRes;
18
19 # EGCatLoader sub-modules 
20 use OpenILS::WWW::EGCatLoader::Util;
21 use OpenILS::WWW::EGCatLoader::Account;
22 use OpenILS::WWW::EGCatLoader::Browse;
23 use OpenILS::WWW::EGCatLoader::Library;
24 use OpenILS::WWW::EGCatLoader::Search;
25 use OpenILS::WWW::EGCatLoader::Record;
26 use OpenILS::WWW::EGCatLoader::Container;
27 use OpenILS::WWW::EGCatLoader::SMS;
28 use OpenILS::WWW::EGCatLoader::Register;
29
30 my $U = 'OpenILS::Application::AppUtils';
31
32 use constant COOKIE_SES => 'ses';
33 use constant COOKIE_LOGGEDIN => 'eg_loggedin';
34 use constant COOKIE_TZ => 'client_tz';
35 use constant COOKIE_PHYSICAL_LOC => 'eg_physical_loc';
36 use constant COOKIE_SSS_EXPAND => 'eg_sss_expand';
37
38 use constant COOKIE_ANON_CACHE => 'anoncache';
39 use constant COOKIE_CART_CACHE => 'cartcache';
40 use constant CART_CACHE_MYLIST => 'mylist';
41 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
42
43 use constant DEBUG_TIMING => 0;
44
45 sub new {
46     my($class, $apache, $ctx) = @_;
47
48     my $self = bless({}, ref($class) || $class);
49
50     $self->apache($apache);
51     $self->ctx($ctx);
52     $self->cgi(new CGI);
53     $self->timelog("New page");
54
55     # Add a timelog helper to the context
56     $self->ctx->{timelog} = sub { return $self->timelog(@_) };
57
58     OpenILS::Utils::CStoreEditor->init; # just in case
59     $self->editor(new_editor());
60
61     return $self;
62 }
63
64 sub DESTROY {
65     my $self = shift;
66     $ENV{TZ} = $self->ctx->{original_tz}
67         if ($self->ctx && exists $self->ctx->{original_tz});
68 }
69
70 # current Apache2::RequestRec;
71 sub apache {
72     my($self, $apache) = @_;
73     $self->{apache} = $apache if $apache;
74     return $self->{apache};
75 }
76
77 # runtime / template context
78 sub ctx {
79     my($self, $ctx) = @_;
80     $self->{ctx} = $ctx if $ctx;
81     return $self->{ctx};
82 }
83
84 # cstore editor
85 sub editor {
86     my($self, $editor) = @_;
87     $self->{editor} = $editor if $editor;
88     return $self->{editor};
89 }
90
91 # CGI handle
92 sub cgi {
93     my($self, $cgi) = @_;
94     $self->{cgi} = $cgi if $cgi;
95     return $self->{cgi};
96 }
97
98 sub timelog {
99     my($self, $description) = @_;
100
101     return unless DEBUG_TIMING;
102     return unless $description;
103     $self->ctx->{timing} ||= [];
104     
105     my $timer = [Time::HiRes::gettimeofday()];
106     $self->ctx->{time_begin} ||= $timer;
107
108     push @{$self->ctx->{timing}}, [
109         Time::HiRes::tv_interval($self->ctx->{time_begin}, $timer), $description
110     ];
111 }
112
113 # -----------------------------------------------------------------------------
114 # Perform initial setup, load common data, then load page data
115 # -----------------------------------------------------------------------------
116 sub load {
117     my $self = shift;
118
119     $self->init_ro_object_cache;
120     $self->timelog("Initial load");
121
122     my $stat = $self->load_common;
123     return $stat unless $stat == Apache2::Const::OK;
124
125     my $path = $self->apache->path_info;
126
127     if ($path =~ m|opac/?$|) {
128         # nowhere specified, just go home
129         return $self->generic_redirect($self->ctx->{home_page});
130     }
131
132     (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
133         $path =~ /opac\/my(opac\/lists|list)/ ||
134         $path =~ m!opac/api/mylist!;
135
136     return $self->load_api_mylist_retrieve if $path =~ m|opac/api/mylist/retrieve|;
137     return $self->load_api_mylist_add if $path =~ m|opac/api/mylist/add|;
138     return $self->load_api_mylist_delete if $path =~ m|opac/api/mylist/delete|;
139     return $self->load_api_mylist_clear if $path =~ m|opac/api/mylist/clear|;
140
141     return $self->load_simple("home") if $path =~ m|opac/home|;
142     return $self->load_simple("css") if $path =~ m|opac/css|;
143     return $self->load_simple("advanced") if
144         $path =~ m:opac/(advanced|numeric|expert):;
145
146     return $self->load_library if $path =~ m|opac/library|;
147     return $self->load_rresults if $path =~ m|opac/results|;
148     return $self->load_print_record if $path =~ m|opac/record/print|;
149     return $self->load_record if $path =~ m|opac/record/\d|;
150     return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
151     return $self->load_browse if $path =~ m|opac/browse|;
152
153     return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
154     return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
155     return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
156     return $self->load_mylist_print if $path =~ m|opac/mylist/doprint|;
157     return $self->load_mylist if $path =~ m|opac/mylist| && $path !~ m|opac/mylist/email| && $path !~ m|opac/mylist/doemail|;
158     return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
159     return $self->load_temp_warn_post if $path =~ m|opac/temp_warn/post|;
160     return $self->load_temp_warn if $path =~ m|opac/temp_warn|;
161
162     # ----------------------------------------------------------------
163     #  Everything below here requires SSL
164     # ----------------------------------------------------------------
165     return $self->redirect_ssl unless $self->cgi->https;
166     return $self->load_password_reset if $path =~ m|opac/password_reset|;
167     return $self->load_logout if $path =~ m|opac/logout|;
168     return $self->load_patron_reg if $path =~ m|opac/register|;
169
170     $self->load_simple("myopac") if $path =~ m:opac/myopac:; # A default page for myopac parts
171
172     if($path =~ m|opac/login|) {
173         return $self->load_login unless $self->editor->requestor; # already logged in?
174
175         # This will be less confusing to users than to be shown a login form
176         # when they're already logged in.
177         return $self->generic_redirect(
178             sprintf(
179                 "%s://%s%s/myopac/main",
180                 $self->ctx->{proto},
181                 $self->ctx->{hostname}, $self->ctx->{opac_root}
182             )
183         );
184     }
185
186     if ($path =~ m|opac/sms_cn| and !$self->editor->requestor) {
187         my $org_unit = $self->ctx->{physical_loc} || $self->cgi->param('loc') || $self->ctx->{aou_tree}->()->id;
188         my $skip_sms_auth = $self->ctx->{get_org_setting}->($org_unit, 'sms.disable_authentication_requirement.callnumbers');
189         return $self->load_sms_cn if $skip_sms_auth;
190     }
191
192     # ----------------------------------------------------------------
193     #  Everything below here requires authentication
194     # ----------------------------------------------------------------
195     return $self->redirect_auth unless $self->editor->requestor;
196
197     # Don't cache anything requiring auth for security reasons
198     $self->apache->headers_out->add("cache-control" => "no-store, no-cache, must-revalidate");
199     $self->apache->headers_out->add("expires" => "-1");
200
201     if ($path =~ m|opac/mylist/email|) {
202         (undef, $self->ctx->{mylist}) = $self->fetch_mylist;
203     }
204     $self->load_simple("mylist/email") if $path =~ m|opac/mylist/email|;
205     return $self->load_mylist_email if $path =~ m|opac/mylist/doemail|;
206     return $self->load_email_record if $path =~ m|opac/record/email|;
207
208     return $self->load_place_hold if $path =~ m|opac/place_hold|;
209     return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
210     return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
211     return $self->load_myopac_messages if $path =~ m|opac/myopac/messages|;
212     return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
213     return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
214     return $self->load_myopac_pay_init if $path =~ m|opac/myopac/main_pay_init|;
215     return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
216     return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
217     return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
218     return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
219     return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
220     return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
221     return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
222     return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
223     return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
224     return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
225     return $self->load_myopac_circ_history_export if $path =~ m|opac/myopac/circ_history/export|;
226     return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
227     return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
228     return $self->load_myopac_prefs_notify_changed_holds if $path =~ m|opac/myopac/prefs_notify_changed_holds|;
229     return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
230     return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
231     return $self->load_myopac_prefs_my_lists if $path =~ m|opac/myopac/prefs_my_lists|;
232     return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
233     return $self->load_myopac_reservations if $path =~ m|opac/myopac/reservations|;
234     return $self->load_sms_cn if $path =~ m|opac/sms_cn|;
235
236     return Apache2::Const::OK;
237 }
238
239
240 # -----------------------------------------------------------------------------
241 # Redirect to SSL equivalent of a given page
242 # -----------------------------------------------------------------------------
243 sub redirect_ssl {
244     my $self = shift;
245     my $new_page = sprintf('%s://%s%s', ($self->ctx->{is_staff} ? 'oils' : 'https'), $self->ctx->{hostname}, $self->apache->unparsed_uri);
246     return $self->generic_redirect($new_page);
247 }
248
249 # -----------------------------------------------------------------------------
250 # If an authnticated resource is requested w/o auth, redirect to the login page,
251 # then return to the originally requrested resource upon successful login.
252 # -----------------------------------------------------------------------------
253 sub redirect_auth {
254     my $self = shift;
255     my $login_page = sprintf('%s://%s%s/login',($self->ctx->{is_staff} ? 'oils' : 'https'), $self->ctx->{hostname}, $self->ctx->{opac_root});
256     my $redirect_to = uri_escape_utf8($self->apache->unparsed_uri);
257     return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
258 }
259
260 # -----------------------------------------------------------------------------
261 # Fall-through for loading a basic page
262 # -----------------------------------------------------------------------------
263 sub load_simple {
264     my ($self, $page) = @_;
265     $self->ctx->{page} = $page;
266     $self->ctx->{search_ou} = $self->_get_search_lib();
267
268     return Apache2::Const::OK;
269 }
270
271 # -----------------------------------------------------------------------------
272 # Tests to see if the user is authenticated and sets some common context values
273 # -----------------------------------------------------------------------------
274 sub load_common {
275     my $self = shift;
276
277     my $e = $self->editor;
278     my $ctx = $self->ctx;
279
280     # redirect non-https to https if we think we are already logged in
281     if ($self->cgi->cookie(COOKIE_LOGGEDIN)) {
282         return $self->redirect_ssl unless $self->cgi->https;
283     }
284
285     # XXX Cache this? Makes testing difficult as apache needs a restart.
286     my $default_sort = $e->retrieve_config_global_flag('opac.default_sort');
287     $ctx->{default_sort} =
288         ($default_sort && $U->is_true($default_sort->enabled)) ? $default_sort->value : '';
289
290     $ctx->{client_tz} = $self->cgi->cookie(COOKIE_TZ) || $ENV{TZ};
291     $ctx->{referer} = $self->cgi->referer;
292     $ctx->{path_info} = $self->cgi->path_info;
293     $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
294     $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
295     $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
296
297     $ctx->{original_tz} = $ENV{TZ};
298     $ENV{TZ} = $ctx->{client_tz};
299
300     my $xul_wrapper = 
301         ($self->apache->headers_in->get('OILS-Wrapper') || '') =~ /true/;
302
303     if ($xul_wrapper) {
304         # XUL client
305         $ctx->{is_staff} = 1;
306         $ctx->{proto} = 'oils';
307         $ctx->{hostname} = 'remote';
308     }
309
310     $ctx->{physical_loc} = $self->get_physical_loc;
311
312     # capture some commonly accessed pages
313     $ctx->{home_page} = $ctx->{proto} . '://' . $ctx->{hostname} . $self->ctx->{opac_root} . "/home";
314     $ctx->{logout_page} = ($ctx->{proto} eq 'http' ? 'https' : $ctx->{proto} ) . '://' . $ctx->{hostname} . $self->ctx->{opac_root} . "/logout";
315
316     if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
317
318         if($e->checkauth) {
319
320             $ctx->{authtoken} = $e->authtoken;
321             $ctx->{authtime} = $e->authtime;
322             $ctx->{user} = $e->requestor;
323             my $card = $self->editor->retrieve_actor_card($ctx->{user}->card);
324             $ctx->{active_card} = (ref $card) ? $card->barcode : undef;
325             $ctx->{place_unfillable} = 1 if $e->requestor->wsid && $e->allowed('PLACE_UNFILLABLE_HOLD', $e->requestor->ws_ou);
326
327             # The browser client does not set an OILS-Wrapper header (above).
328             # The presence of a workstation and no header indicates staff mode.
329             # FIXME: this approach leaves un-wrapped TPAC's within the same
330             # browser (and hence same ses cookie) in an unnatural is_staff
331             # state.  Consider alternatives for determining is_staff / 
332             # is_browser_staff when $xul_wrapper is false.
333             if (!$xul_wrapper and $e->requestor->wsid) {
334                 $ctx->{is_staff} = 1;
335                 $ctx->{is_browser_staff} = 1;
336             }
337
338             $self->update_dashboard_stats();
339
340         } else {
341
342             # if we encounter a stale authtoken, call load_logout 
343             # to clean up the cookie, then redirect the user to the
344             # originally requested page
345             return $self->load_logout($self->apache->unparsed_uri);
346         }
347     }
348
349     # List of <meta> and <link> elements to populate
350     $ctx->{metalinks} = [];
351
352     $self->extract_copy_location_group_info;
353     $ctx->{search_ou} = $self->_get_search_lib();
354     $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
355     $self->load_eg_cache_hash;
356     $self->load_copy_location_groups;
357     $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
358     $self->load_search_filter_groups($ctx->{search_ou});
359     $self->load_org_util_funcs;
360     $self->load_perm_funcs;
361
362     # FIXME - move carousel helpers to a separate file
363     $ctx->{get_visible_carousels} = sub {
364         my $org_unit = $self->ctx->{physical_loc} || $self->cgi->param('loc') || $self->ctx->{aou_tree}->()->id;
365         return $U->simplereq(
366             'open-ils.actor',
367             'open-ils.actor.carousel.retrieve_by_org',
368             $org_unit
369         );
370     };
371     $ctx->{get_carousel} = sub {
372         my $id = shift;
373
374         my $carousel = $e->retrieve_container_carousel($id);
375         my $ret = {
376             id   => $id,
377             name => $carousel->name
378         };
379         my $q = {
380             select => { bre => ['id'], mfde => [{ column => 'value', alias => 'title' }] },
381             from   => {
382                 bre => {
383                     cbrebi => {
384                         join => {
385                             cbreb => {
386                                 join => { cc => {} }
387                             }
388                         }
389                     },
390                     mfde => {}
391                 }
392             },
393             where  => {
394                 '+cc' => { id => $id },
395                 '+bre' => { deleted => 'f' },
396                 '+mfde' => { name => 'title' }
397             }
398         };
399         my $r = $e->json_query($q);
400         $ret->{bibs} = $r;
401         return $ret;
402     };
403
404     $ctx->{fetch_display_fields} = sub {
405         my $id = shift;
406
407         if (@$id == 1) {
408             return $ctx->{_hl_data}{''.$$id[0]}
409                 if ($ctx->{_hl_data}{''.$$id[0]});
410         }
411
412         $self->timelog("HL data not cached, fetching from server.");
413
414         my $rows = $U->simplereq(
415             'open-ils.search', 
416             'open-ils.search.fetch.metabib.display_field.highlight',
417             $ctx->{query_struct}{additional_data}{highlight_map},
418             map {int($_)} @$id
419         );
420
421         $ctx->{_hl_data}{''.$$id[0]} = $rows if (@$id == 1);
422
423         return $rows;
424     };
425
426     return Apache2::Const::OK;
427 }
428
429 sub update_dashboard_stats {
430     my $self = shift;
431
432     my $e = $self->editor;
433     my $ctx = $self->ctx;
434
435     $ctx->{user_stats} = $U->simplereq(
436         'open-ils.actor', 
437         'open-ils.actor.user.opac.vital_stats', 
438         $e->authtoken, $e->requestor->id);
439 }
440
441 sub staff_saved_searches_set_expansion_state {
442     my $self = shift;
443
444     my $param = $self->cgi->param('sss_expand');
445     my $value;
446     
447     if (defined $param) {
448         $value = ($param ? 1 : 0);
449         $self->apache->headers_out->add(
450             "Set-Cookie" => $self->cgi->cookie(
451                 -name => COOKIE_SSS_EXPAND,
452                 -path => $self->ctx->{base_path},
453                 -secure => 1,   # not strictly necessary, but this feature is staff-only, so may as well
454                 -value => $value,
455                 -expires => undef
456             )
457         );
458     } else {
459         $value = $self->cgi->cookie(COOKIE_SSS_EXPAND);
460     }
461
462     $self->ctx->{saved_searches_expanded} = $value;
463 }
464
465 # physical_loc (i.e. "original location") passed in as a URL 
466 # param will replace any existing physical_loc stored as a cookie.
467 # If specified via ENV that rules over all and we don't set cookies.
468 sub get_physical_loc {
469     my $self = shift;
470
471     return $ENV{physical_loc} if($ENV{physical_loc});
472
473     if(my $physical_loc = $self->cgi->param('physical_loc')) {
474         $self->apache->headers_out->add(
475             "Set-Cookie" => $self->cgi->cookie(
476                 -name => COOKIE_PHYSICAL_LOC,
477                 -path => $self->ctx->{base_path},
478                 -value => $physical_loc,
479                 -expires => undef
480             )
481         );
482         return $physical_loc;
483     }
484
485     return $self->cgi->cookie(COOKIE_PHYSICAL_LOC);
486 }
487
488 # -----------------------------------------------------------------------------
489 # Log in and redirect to the redirect_to URL (or home)
490 # -----------------------------------------------------------------------------
491 sub load_login {
492     my $self = shift;
493     my $cgi = $self->cgi;
494     my $ctx = $self->ctx;
495
496     $self->timelog("Load login begins");
497
498     $ctx->{page} = 'login';
499
500     my $username = $cgi->param('username') || '';
501     $username =~ s/\s//g;  # Remove blanks
502     my $password = $cgi->param('password');
503     my $org_unit = $ctx->{physical_loc} || $ctx->{aou_tree}->()->id;
504     my $persist = $cgi->param('persist');
505     my $client_tz = $cgi->param('client_tz');
506
507     # initial log form only
508     return Apache2::Const::OK unless $username and $password;
509
510     my $auth_proxy_enabled = 0; # default false
511     try { # if the service is not running, just let this fail silently
512         $auth_proxy_enabled = $U->simplereq(
513             'open-ils.auth_proxy',
514             'open-ils.auth_proxy.enabled');
515     } catch Error with {};
516
517     $self->timelog("Checked for auth proxy: $auth_proxy_enabled; org = $org_unit; username = $username");
518
519     my $args = {
520         type => ($persist) ? 'persist' : 'opac',
521         org => $org_unit,
522         agent => 'opac'
523     };
524
525     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
526
527     # To avoid surprises, default to "Barcodes start with digits"
528     $bc_regex = '^\d' unless $bc_regex;
529
530     if ($bc_regex and ($username =~ /$bc_regex/)) {
531         $args->{barcode} = $username;
532     } else {
533         $args->{username} = $username;
534     }
535
536     my $response;
537     if (!$auth_proxy_enabled) {
538         my $seed = $U->simplereq(
539             'open-ils.auth',
540             'open-ils.auth.authenticate.init', $username);
541         $args->{password} = md5_hex($seed . md5_hex($password));
542         $response = $U->simplereq(
543             'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
544     } else {
545         $args->{password} = $password;
546         $response = $U->simplereq(
547             'open-ils.auth_proxy',
548             'open-ils.auth_proxy.login', $args);
549     }
550     $self->timelog("Checked password");
551
552     if($U->event_code($response)) { 
553         # login failed, report the reason to the template
554         $ctx->{login_failed_event} = $response;
555         return Apache2::Const::OK;
556     }
557
558     # login succeeded, redirect as necessary
559
560     my $acct = $self->apache->unparsed_uri;
561     $acct =~ s|/login|/myopac/main|;
562
563     # both login-related cookies should expire at the same time
564     my $login_cookie_expires = ($persist) ? CORE::time + $response->{payload}->{authtime} : undef;
565
566     my $cookie_list = [
567         # contains the actual auth token and should be sent only over https
568         $cgi->cookie(
569             -name => COOKIE_SES,
570             -path => '/',
571             -secure => 1,
572             -value => $response->{payload}->{authtoken},
573             -expires => $login_cookie_expires
574         ),
575         # contains only a hint that we are logged in, and is used to
576         # trigger a redirect to https
577         $cgi->cookie(
578             -name => COOKIE_LOGGEDIN,
579             -path => '/',
580             -secure => 0,
581             -value => '1',
582             -expires => $login_cookie_expires
583         )
584     ];
585
586     if ($client_tz) {
587         # contains the client's tz, as passed by the client
588         # trigger a redirect to https
589         push @$cookie_list, $cgi->cookie(
590             -name => COOKIE_TZ,
591             -path => '/',
592             -secure => 0,
593             -value => $client_tz,
594             -expires => $login_cookie_expires
595         );
596     }
597
598     return $self->generic_redirect(
599         $cgi->param('redirect_to') || $acct,
600         $cookie_list
601     );
602 }
603
604 # -----------------------------------------------------------------------------
605 # Log out and redirect to the home page
606 # -----------------------------------------------------------------------------
607 sub load_logout {
608     my $self = shift;
609     my $redirect_to = shift || $self->cgi->param('redirect_to');
610
611     # If the user was adding anyting to an anonymous cache 
612     # while logged in, go ahead and clear it out.
613     $self->clear_anon_cache;
614
615     try { # a missing auth token will cause an ugly explosion
616         $U->simplereq(
617             'open-ils.auth',
618             'open-ils.auth.session.delete',
619             $self->cgi->cookie(COOKIE_SES)
620         );
621     } catch Error with {};
622
623     return $self->generic_redirect(
624         $redirect_to || $self->ctx->{home_page},
625         [
626             # clear value of and expire both of these login-related cookies
627             $self->cgi->cookie(
628                 -name => COOKIE_SES,
629                 -path => '/',
630                 -value => '',
631                 -expires => '-1h'
632             ),
633             $self->cgi->cookie(
634                 -name => COOKIE_LOGGEDIN,
635                 -path => '/',
636                 -value => '',
637                 -expires => '-1h'
638             )
639         ]
640     );
641 }
642
643 1;
644