]> 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
24 my $U = 'OpenILS::Application::AppUtils';
25
26 sub new {
27     my($class, $apache, $ctx) = @_;
28
29     my $self = bless({}, ref($class) || $class);
30
31     $self->apache($apache);
32     $self->ctx($ctx);
33     $self->cgi(CGI->new);
34
35     OpenILS::Utils::CStoreEditor->init; # just in case
36     $self->editor(new_editor());
37
38     return $self;
39 }
40
41
42 # current Apache2::RequestRec;
43 sub apache {
44     my($self, $apache) = @_;
45     $self->{apache} = $apache if $apache;
46     return $self->{apache};
47 }
48
49 # runtime / template context
50 sub ctx {
51     my($self, $ctx) = @_;
52     $self->{ctx} = $ctx if $ctx;
53     return $self->{ctx};
54 }
55
56 # cstore editor
57 sub editor {
58     my($self, $editor) = @_;
59     $self->{editor} = $editor if $editor;
60     return $self->{editor};
61 }
62
63 # CGI handle
64 sub cgi {
65     my($self, $cgi) = @_;
66     $self->{cgi} = $cgi if $cgi;
67     return $self->{cgi};
68 }
69
70
71 # -----------------------------------------------------------------------------
72 # Perform initial setup, load common data, then load page data
73 # -----------------------------------------------------------------------------
74 sub load {
75     my $self = shift;
76
77     $self->init_ro_object_cache;
78
79     my $stat = $self->load_common;
80     return $stat unless $stat == Apache2::Const::OK;
81
82     my $path = $self->apache->path_info;
83
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/;
90
91     # ----------------------------------------------------------------
92     #  Everything below here requires authentication
93     # ----------------------------------------------------------------
94     return $self->redirect_secure($path) 
95         unless $self->cgi->https and $self->editor->requestor;
96
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/;
104
105     return Apache2::Const::OK;
106 }
107
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;
118 }
119
120 # -----------------------------------------------------------------------------
121 # Fall-through for loading a basic page
122 # -----------------------------------------------------------------------------
123 sub load_simple {
124     my ($self, $page) = @_;
125     $self->ctx->{page} = $page;
126     return Apache2::Const::OK;
127 }
128
129 # -----------------------------------------------------------------------------
130 # Tests to see if the user is authenticated and sets some common context values
131 # -----------------------------------------------------------------------------
132 sub load_common {
133     my $self = shift;
134
135     my $e = $self->editor;
136     my $ctx = $self->ctx;
137
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');
142
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";
146
147     if($e->authtoken($self->cgi->cookie('ses'))) {
148
149         if($e->checkauth) {
150
151             $ctx->{authtoken} = $e->authtoken;
152             $ctx->{authtime} = $e->authtime;
153             $ctx->{user} = $e->requestor;
154
155             $ctx->{user_stats} = $U->simplereq(
156                 'open-ils.actor', 
157                 'open-ils.actor.user.opac.vital_stats', 
158                 $e->authtoken, $e->requestor->id);
159
160         } else {
161
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");
165
166             return $self->load_logout;
167         }
168     }
169
170     return Apache2::Const::OK;
171 }
172
173
174 # -----------------------------------------------------------------------------
175 # Log in and redirect to the redirect_to URL (or home)
176 # -----------------------------------------------------------------------------
177 sub load_login {
178     my $self = shift;
179     my $cgi = $self->cgi;
180     my $ctx = $self->ctx;
181
182     $ctx->{page} = 'login';
183
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');
188
189     # initial log form only
190     return Apache2::Const::OK unless $username and $password;
191
192         my $seed = $U->simplereq(
193         'open-ils.auth', 
194                 'open-ils.auth.authenticate.init', $username);
195
196     my $args = {        
197         username => $username, 
198         password => md5_hex($seed . md5_hex($password)), 
199         type => ($persist) ? 'persist' : 'opac' 
200     };
201
202     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
203
204     $args->{barcode} = delete $args->{username} 
205         if $bc_regex and $username =~ /$bc_regex/;
206
207         my $response = $U->simplereq(
208         'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
209
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;
214     }
215
216     # login succeeded, redirect as necessary
217
218     my $home = $self->apache->unparsed_uri;
219     $home =~ s/\/login/\/home/;
220
221     $self->apache->print(
222         $cgi->redirect(
223             -url => $cgi->param('redirect_to') || $home,
224             -cookie => $cgi->cookie(
225                 -name => 'ses',
226                 -path => '/',
227                 -secure => 1,
228                 -value => $response->{payload}->{authtoken},
229                 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
230             )
231         )
232     );
233
234     return Apache2::Const::REDIRECT;
235 }
236
237 # -----------------------------------------------------------------------------
238 # Log out and redirect to the home page
239 # -----------------------------------------------------------------------------
240 sub load_logout {
241     my $self = shift;
242
243     $self->apache->print(
244         $self->cgi->redirect(
245             -url => $self->ctx->{home_page},
246             -cookie => $self->cgi->cookie(
247                 -name => 'ses',
248                 -path => '/',
249                 -value => '',
250                 -expires => '-1h'
251             )
252         )
253     );
254
255     return Apache2::Const::REDIRECT;
256 }
257
258 1;
259