]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Merge branch 'master' of git+ssh://yeti.esilibrary.com/home/evergreen/evergreen-equin...
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use CGI;
4 use XML::LibXML;
5 use URI::Escape;
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;
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
29 sub new {
30     my($class, $apache, $ctx) = @_;
31
32     my $self = bless({}, ref($class) || $class);
33
34     $self->apache($apache);
35     $self->ctx($ctx);
36     $self->cgi(CGI->new);
37
38     OpenILS::Utils::CStoreEditor->init; # just in case
39     $self->editor(new_editor());
40
41     return $self;
42 }
43
44
45 # current Apache2::RequestRec;
46 sub apache {
47     my($self, $apache) = @_;
48     $self->{apache} = $apache if $apache;
49     return $self->{apache};
50 }
51
52 # runtime / template context
53 sub ctx {
54     my($self, $ctx) = @_;
55     $self->{ctx} = $ctx if $ctx;
56     return $self->{ctx};
57 }
58
59 # cstore editor
60 sub editor {
61     my($self, $editor) = @_;
62     $self->{editor} = $editor if $editor;
63     return $self->{editor};
64 }
65
66 # CGI handle
67 sub cgi {
68     my($self, $cgi) = @_;
69     $self->{cgi} = $cgi if $cgi;
70     return $self->{cgi};
71 }
72
73
74 # -----------------------------------------------------------------------------
75 # Perform initial setup, load common data, then load page data
76 # -----------------------------------------------------------------------------
77 sub load {
78     my $self = shift;
79
80     $self->init_ro_object_cache;
81
82     my $stat = $self->load_common;
83     return $stat unless $stat == Apache2::Const::OK;
84
85     my $path = $self->apache->path_info;
86
87     return $self->load_simple("home") if $path =~ /opac\/home/;
88     return $self->load_simple("advanced") if $path =~ /opac\/advanced/;
89     return $self->load_rresults if $path =~ /opac\/results/;
90     return $self->load_record if $path =~ /opac\/record/;
91     return $self->load_mylist_add if $path =~ /opac\/mylist\/add/;
92     return $self->load_mylist_del if $path =~ /opac\/mylist\/del/;
93     return $self->load_cache_clear if $path =~ /opac\/cache\/clear/;
94
95     # ----------------------------------------------------------------
96     # Logout and login require SSL
97     # ----------------------------------------------------------------
98     if($path =~ /opac\/login/) {
99         return $self->redirect_ssl unless $self->cgi->https;
100         return $self->load_login;
101     }
102
103     if($path =~ /opac\/logout/) {
104         #return Apache2::Const::FORBIDDEN unless $self->cgi->https; 
105         $self->apache->log->warn("catloader: logout called in non-secure context from " . 
106             ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
107         return $self->load_logout;
108     }
109
110     # ----------------------------------------------------------------
111     #  Everything below here requires SSL + authentication
112     # ----------------------------------------------------------------
113     return $self->redirect_auth
114         unless $self->cgi->https and $self->editor->requestor;
115
116     return $self->load_place_hold if $path =~ /opac\/place_hold/;
117     return $self->load_myopac_holds if $path =~ /opac\/myopac\/holds/;
118     return $self->load_myopac_circs if $path =~ /opac\/myopac\/circs/;
119     return $self->load_myopac_fines if $path =~ /opac\/myopac\/main/;
120     return $self->load_myopac_update_email if $path =~ /opac\/myopac\/update_email/;
121     return $self->load_myopac_bookbags if $path =~ /opac\/myopac\/bookbags/;
122     return $self->load_myopac if $path =~ /opac\/myopac/;
123
124     return Apache2::Const::OK;
125 }
126
127
128 # -----------------------------------------------------------------------------
129 # Redirect to SSL equivalent of a given page
130 # -----------------------------------------------------------------------------
131 sub redirect_ssl {
132     my $self = shift;
133     my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
134     $self->apache->print($self->cgi->redirect(-url => $new_page));
135     return Apache2::Const::REDIRECT;
136 }
137
138 # -----------------------------------------------------------------------------
139 # If an authnticated resource is requested w/o auth, redirect to the login page,
140 # then return to the originally requrested resource upon successful login.
141 # -----------------------------------------------------------------------------
142 sub redirect_auth {
143     my $self = shift;
144     my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
145     my $redirect_to = uri_escape($self->apache->unparsed_uri);
146     $self->apache->print($self->cgi->redirect(-url => "$login_page?redirect_to=$redirect_to"));
147     return Apache2::Const::REDIRECT;
148 }
149
150 # -----------------------------------------------------------------------------
151 # Fall-through for loading a basic page
152 # -----------------------------------------------------------------------------
153 sub load_simple {
154     my ($self, $page) = @_;
155     $self->ctx->{page} = $page;
156     return Apache2::Const::OK;
157 }
158
159 # -----------------------------------------------------------------------------
160 # Tests to see if the user is authenticated and sets some common context values
161 # -----------------------------------------------------------------------------
162 sub load_common {
163     my $self = shift;
164
165     my $e = $self->editor;
166     my $ctx = $self->ctx;
167
168     $ctx->{referer} = $self->cgi->referer;
169     $ctx->{path_info} = $self->cgi->path_info;
170     $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
171     $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ 'oils_xulrunner');
172
173     # capture some commonly accessed pages
174     $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
175     $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
176
177     if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
178
179         if($e->checkauth) {
180
181             $ctx->{authtoken} = $e->authtoken;
182             $ctx->{authtime} = $e->authtime;
183             $ctx->{user} = $e->requestor;
184
185             $ctx->{user_stats} = $U->simplereq(
186                 'open-ils.actor', 
187                 'open-ils.actor.user.opac.vital_stats', 
188                 $e->authtoken, $e->requestor->id);
189
190         } else {
191
192             # For now, keep an eye out for any pages being unceremoniously redirected to logout...
193             $self->apache->log->info("catloader: loading " . $ctx->{path_info} . 
194                 "; auth session " .  $e->authtoken . " no longer valid; redirecting to logout");
195
196             return $self->load_logout;
197         }
198     }
199
200     return Apache2::Const::OK;
201 }
202
203
204 # -----------------------------------------------------------------------------
205 # Log in and redirect to the redirect_to URL (or home)
206 # -----------------------------------------------------------------------------
207 sub load_login {
208     my $self = shift;
209     my $cgi = $self->cgi;
210     my $ctx = $self->ctx;
211
212     $ctx->{page} = 'login';
213
214     my $username = $cgi->param('username');
215     my $password = $cgi->param('password');
216     my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
217     my $persist = $cgi->param('persist');
218
219     # initial log form only
220     return Apache2::Const::OK unless $username and $password;
221
222         my $seed = $U->simplereq(
223         'open-ils.auth', 
224                 'open-ils.auth.authenticate.init', $username);
225
226     my $args = {        
227         username => $username, 
228         password => md5_hex($seed . md5_hex($password)), 
229         type => ($persist) ? 'persist' : 'opac' 
230     };
231
232     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
233
234     $args->{barcode} = delete $args->{username} 
235         if $bc_regex and $username =~ /$bc_regex/;
236
237         my $response = $U->simplereq(
238         'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
239
240     if($U->event_code($response)) { 
241         # login failed, report the reason to the template
242         $ctx->{login_failed_event} = $response;
243         return Apache2::Const::OK;
244     }
245
246     # login succeeded, redirect as necessary
247
248     my $acct = $self->apache->unparsed_uri;
249     $acct =~ s#/login#/myopac/main#;
250
251     $self->apache->print(
252         $cgi->redirect(
253             -url => $cgi->param('redirect_to') || $acct,
254             -cookie => $cgi->cookie(
255                 -name => COOKIE_SES,
256                 -path => '/',
257                 -secure => 1,
258                 -value => $response->{payload}->{authtoken},
259                 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
260             )
261         )
262     );
263
264     return Apache2::Const::REDIRECT;
265 }
266
267 # -----------------------------------------------------------------------------
268 # Log out and redirect to the home page
269 # -----------------------------------------------------------------------------
270 sub load_logout {
271     my $self = shift;
272
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;
276
277     $self->apache->print(
278         $self->cgi->redirect(
279             -url => $self->ctx->{home_page},
280             -cookie => $self->cgi->cookie(
281                 -name => COOKIE_SES,
282                 -path => '/',
283                 -value => '',
284                 -expires => '-1h'
285             )
286         )
287     );
288
289     return Apache2::Const::REDIRECT;
290 }
291
292 1;
293