1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
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);
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;
25 my $U = 'OpenILS::Application::AppUtils';
27 use constant COOKIE_SES => 'ses';
30 my($class, $apache, $ctx) = @_;
32 my $self = bless({}, ref($class) || $class);
34 $self->apache($apache);
38 OpenILS::Utils::CStoreEditor->init; # just in case
39 $self->editor(new_editor());
45 # current Apache2::RequestRec;
47 my($self, $apache) = @_;
48 $self->{apache} = $apache if $apache;
49 return $self->{apache};
52 # runtime / template context
55 $self->{ctx} = $ctx if $ctx;
61 my($self, $editor) = @_;
62 $self->{editor} = $editor if $editor;
63 return $self->{editor};
69 $self->{cgi} = $cgi if $cgi;
74 # -----------------------------------------------------------------------------
75 # Perform initial setup, load common data, then load page data
76 # -----------------------------------------------------------------------------
80 $self->init_ro_object_cache;
82 my $stat = $self->load_common;
83 return $stat unless $stat == Apache2::Const::OK;
85 my $path = $self->apache->path_info;
87 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
88 $path =~ /opac\/my(opac\/lists|list)/;
90 return $self->load_simple("home") if $path =~ /opac\/home/;
91 return $self->load_simple("advanced") if $path =~ /opac\/advanced/;
92 return $self->load_rresults if $path =~ /opac\/results/;
93 return $self->load_record if $path =~ /opac\/record/;
95 return $self->load_mylist_add if $path =~ /opac\/mylist\/add/;
96 return $self->load_mylist_move if $path =~ /opac\/mylist\/move/;
97 return $self->load_mylist if $path =~ /opac\/mylist/;
98 return $self->load_cache_clear if $path =~ /opac\/cache\/clear/;
100 # ----------------------------------------------------------------
101 # Logout and login require SSL
102 # ----------------------------------------------------------------
103 if($path =~ /opac\/login/) {
104 return $self->redirect_ssl unless $self->cgi->https;
105 return $self->load_login;
108 if($path =~ /opac\/logout/) {
109 #return Apache2::Const::FORBIDDEN unless $self->cgi->https;
110 $self->apache->log->warn("catloader: logout called in non-secure context from " .
111 ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
112 return $self->load_logout;
115 # ----------------------------------------------------------------
116 # Everything below here requires SSL + authentication
117 # ----------------------------------------------------------------
118 return $self->redirect_auth
119 unless $self->cgi->https and $self->editor->requestor;
121 return $self->load_place_hold if $path =~ /opac\/place_hold/;
122 return $self->load_myopac_holds if $path =~ /opac\/myopac\/holds/;
123 return $self->load_myopac_circs if $path =~ /opac\/myopac\/circs/;
124 return $self->load_myopac_fines if $path =~ /opac\/myopac\/main/;
125 return $self->load_myopac_update_email if $path =~ /opac\/myopac\/update_email/;
126 return $self->load_myopac_bookbags if $path =~ /opac\/myopac\/lists/;
127 return $self->load_myopac_bookbag_update if $path =~ /opac\/myopac\/list\/update/;
128 return $self->load_myopac if $path =~ /opac\/myopac/;
130 return Apache2::Const::OK;
134 # -----------------------------------------------------------------------------
135 # Redirect to SSL equivalent of a given page
136 # -----------------------------------------------------------------------------
139 my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
140 return $self->generic_redirect($new_page);
143 # -----------------------------------------------------------------------------
144 # If an authnticated resource is requested w/o auth, redirect to the login page,
145 # then return to the originally requrested resource upon successful login.
146 # -----------------------------------------------------------------------------
149 my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
150 my $redirect_to = uri_escape($self->apache->unparsed_uri);
151 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
154 # -----------------------------------------------------------------------------
155 # Fall-through for loading a basic page
156 # -----------------------------------------------------------------------------
158 my ($self, $page) = @_;
159 $self->ctx->{page} = $page;
160 return Apache2::Const::OK;
163 # -----------------------------------------------------------------------------
164 # Tests to see if the user is authenticated and sets some common context values
165 # -----------------------------------------------------------------------------
169 my $e = $self->editor;
170 my $ctx = $self->ctx;
172 $ctx->{referer} = $self->cgi->referer;
173 $ctx->{path_info} = $self->cgi->path_info;
174 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
175 $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ 'oils_xulrunner');
177 # capture some commonly accessed pages
178 $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
179 $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
181 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
185 $ctx->{authtoken} = $e->authtoken;
186 $ctx->{authtime} = $e->authtime;
187 $ctx->{user} = $e->requestor;
189 $ctx->{user_stats} = $U->simplereq(
191 'open-ils.actor.user.opac.vital_stats',
192 $e->authtoken, $e->requestor->id);
196 # For now, keep an eye out for any pages being unceremoniously redirected to logout...
197 $self->apache->log->info("catloader: loading " . $ctx->{path_info} .
198 "; auth session " . $e->authtoken . " no longer valid; redirecting to logout");
200 return $self->load_logout;
204 return Apache2::Const::OK;
208 # -----------------------------------------------------------------------------
209 # Log in and redirect to the redirect_to URL (or home)
210 # -----------------------------------------------------------------------------
213 my $cgi = $self->cgi;
214 my $ctx = $self->ctx;
216 $ctx->{page} = 'login';
218 my $username = $cgi->param('username');
219 my $password = $cgi->param('password');
220 my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
221 my $persist = $cgi->param('persist');
223 # initial log form only
224 return Apache2::Const::OK unless $username and $password;
226 my $seed = $U->simplereq(
228 'open-ils.auth.authenticate.init', $username);
231 username => $username,
232 password => md5_hex($seed . md5_hex($password)),
233 type => ($persist) ? 'persist' : 'opac'
236 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
238 $args->{barcode} = delete $args->{username}
239 if $bc_regex and $username =~ /$bc_regex/;
241 my $response = $U->simplereq(
242 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
244 if($U->event_code($response)) {
245 # login failed, report the reason to the template
246 $ctx->{login_failed_event} = $response;
247 return Apache2::Const::OK;
250 # login succeeded, redirect as necessary
252 my $acct = $self->apache->unparsed_uri;
253 $acct =~ s#/login#/myopac/main#;
255 return $self->generic_redirect(
256 $cgi->param('redirect_to') || $acct,
261 -value => $response->{payload}->{authtoken},
262 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
267 # -----------------------------------------------------------------------------
268 # Log out and redirect to the home page
269 # -----------------------------------------------------------------------------
273 # If the user was adding anyting to an anonymous cache
274 # while logged in, go ahead and clear it out.
275 $self->clear_anon_cache;
277 return $self->generic_redirect(
278 $self->ctx->{home_page},