1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
6 use Digest::MD5 qw(md5_hex);
7 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
8 use OpenSRF::AppSession;
9 use OpenSRF::EX qw/:try/;
10 use OpenSRF::Utils qw/:datetime/;
11 use OpenSRF::Utils::JSON;
12 use OpenSRF::Utils::Logger qw/$logger/;
13 use OpenILS::Application::AppUtils;
14 use OpenILS::Utils::CStoreEditor qw/:funcs/;
15 use OpenILS::Utils::Fieldmapper;
16 use DateTime::Format::ISO8601;
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;
24 my $U = 'OpenILS::Application::AppUtils';
27 my($class, $apache, $ctx) = @_;
29 my $self = bless({}, ref($class) || $class);
31 $self->apache($apache);
35 OpenILS::Utils::CStoreEditor->init; # just in case
36 $self->editor(new_editor());
42 # current Apache2::RequestRec;
44 my($self, $apache) = @_;
45 $self->{apache} = $apache if $apache;
46 return $self->{apache};
49 # runtime / template context
52 $self->{ctx} = $ctx if $ctx;
58 my($self, $editor) = @_;
59 $self->{editor} = $editor if $editor;
60 return $self->{editor};
66 $self->{cgi} = $cgi if $cgi;
71 # -----------------------------------------------------------------------------
72 # Perform initial setup, load common data, then load page data
73 # -----------------------------------------------------------------------------
77 $self->init_ro_object_cache;
79 my $stat = $self->load_common;
80 return $stat unless $stat == Apache2::Const::OK;
82 my $path = $self->apache->path_info;
84 return $self->load_simple("home") if $path =~ /opac\/home/;
85 return $self->load_simple("advanced") if $path =~ /opac\/advanced/;
86 return $self->load_login if $path =~ /opac\/login/;
87 return $self->load_logout if $path =~ /opac\/logout/;
88 return $self->load_rresults if $path =~ /opac\/results/;
89 return $self->load_record if $path =~ /opac\/record/;
91 # ----------------------------------------------------------------
92 # Everything below here requires authentication
93 # ----------------------------------------------------------------
94 return $self->redirect_secure($path)
95 unless $self->cgi->https and $self->editor->requestor;
97 return $self->load_place_hold if $path =~ /opac\/place_hold/;
98 return $self->load_myopac_holds if $path =~ /opac\/myopac\/holds/;
99 return $self->load_myopac_circs if $path =~ /opac\/myopac\/circs/;
100 return $self->load_myopac_fines if $path =~ /opac\/myopac\/main/;
101 return $self->load_myopac_update_email if $path =~ /opac\/myopac\/update_email/;
102 return $self->load_myopac_bookbags if $path =~ /opac\/myopac\/bookbags/;
103 return $self->load_myopac if $path =~ /opac\/myopac/;
105 return Apache2::Const::OK;
108 # -----------------------------------------------------------------------------
109 # If a secure resource is requested insecurely, redirect to the login page,
110 # then return to the originally requrested resource upon successful login.
111 # -----------------------------------------------------------------------------
112 sub redirect_secure {
113 my ($self, $path) = @_;
114 my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
115 my $redirect_to = uri_escape($self->apache->unparsed_uri);
116 $self->apache->print($self->cgi->redirect(-url => "$login_page?redirect_to=$redirect_to"));
117 return Apache2::Const::REDIRECT;
120 # -----------------------------------------------------------------------------
121 # Fall-through for loading a basic page
122 # -----------------------------------------------------------------------------
124 my ($self, $page) = @_;
125 $self->ctx->{page} = $page;
126 return Apache2::Const::OK;
129 # -----------------------------------------------------------------------------
130 # Tests to see if the user is authenticated and sets some common context values
131 # -----------------------------------------------------------------------------
135 my $e = $self->editor;
136 my $ctx = $self->ctx;
138 $ctx->{referer} = $self->cgi->referer;
139 $ctx->{path_info} = $self->cgi->path_info;
140 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
141 $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ 'oils_xulrunner');
143 # capture some commonly accessed pages
144 $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
145 $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
147 if($e->authtoken($self->cgi->cookie('ses'))) {
151 $ctx->{authtoken} = $e->authtoken;
152 $ctx->{authtime} = $e->authtime;
153 $ctx->{user} = $e->requestor;
155 $ctx->{user_stats} = $U->simplereq(
157 'open-ils.actor.user.opac.vital_stats',
158 $e->authtoken, $e->requestor->id);
162 # For now, keep an eye out for any pages being unceremoniously redirected to logout...
163 $self->apache->log->info("loading " . $ctx->{path_info} . "; auth session " .
164 $e->authtoken . " no longer valid; redirecting to logout");
166 return $self->load_logout;
170 return Apache2::Const::OK;
174 # -----------------------------------------------------------------------------
175 # Log in and redirect to the redirect_to URL (or home)
176 # -----------------------------------------------------------------------------
179 my $cgi = $self->cgi;
180 my $ctx = $self->ctx;
182 $ctx->{page} = 'login';
184 my $username = $cgi->param('username');
185 my $password = $cgi->param('password');
186 my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
187 my $persist = $cgi->param('persist');
189 # initial log form only
190 return Apache2::Const::OK unless $username and $password;
192 my $seed = $U->simplereq(
194 'open-ils.auth.authenticate.init', $username);
197 username => $username,
198 password => md5_hex($seed . md5_hex($password)),
199 type => ($persist) ? 'persist' : 'opac'
202 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
204 $args->{barcode} = delete $args->{username}
205 if $bc_regex and $username =~ /$bc_regex/;
207 my $response = $U->simplereq(
208 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
210 if($U->event_code($response)) {
211 # login failed, report the reason to the template
212 $ctx->{login_failed_event} = $response;
213 return Apache2::Const::OK;
216 # login succeeded, redirect as necessary
218 my $home = $self->apache->unparsed_uri;
219 $home =~ s/\/login/\/home/;
221 $self->apache->print(
223 -url => $cgi->param('redirect_to') || $home,
224 -cookie => $cgi->cookie(
228 -value => $response->{payload}->{authtoken},
229 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
234 return Apache2::Const::REDIRECT;
237 # -----------------------------------------------------------------------------
238 # Log out and redirect to the home page
239 # -----------------------------------------------------------------------------
243 $self->apache->print(
244 $self->cgi->redirect(
245 -url => $self->ctx->{home_page},
246 -cookie => $self->cgi->cookie(
255 return Apache2::Const::REDIRECT;