]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
updated cstoreditor to fetch full cached object to get the authtime; capturing authti...
[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     if($e->authtoken($self->cgi->cookie('ses'))) {
144
145         if($e->checkauth) {
146
147             $self->apache->log->warn("authtime = " . $e->authtime);
148
149             $ctx->{authtoken} = $e->authtoken;
150             $ctx->{authtime} = $e->authtime;
151             $ctx->{user} = $e->requestor;
152
153             $ctx->{user_stats} = $U->simplereq(
154                 'open-ils.actor', 
155                 'open-ils.actor.user.opac.vital_stats', 
156                 $e->authtoken, $e->requestor->id);
157
158         } else {
159
160             return $self->load_logout;
161         }
162     }
163
164     return Apache2::Const::OK;
165 }
166
167
168 # -----------------------------------------------------------------------------
169 # Log in and redirect to the redirect_to URL (or home)
170 # -----------------------------------------------------------------------------
171 sub load_login {
172     my $self = shift;
173     my $cgi = $self->cgi;
174     my $ctx = $self->ctx;
175
176     $ctx->{page} = 'login';
177
178     my $username = $cgi->param('username');
179     my $password = $cgi->param('password');
180     my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
181     my $persist = $cgi->param('persist');
182
183     # initial log form only
184     return Apache2::Const::OK unless $username and $password;
185
186         my $seed = $U->simplereq(
187         'open-ils.auth', 
188                 'open-ils.auth.authenticate.init', $username);
189
190     my $args = {        
191         username => $username, 
192         password => md5_hex($seed . md5_hex($password)), 
193         type => ($persist) ? 'persist' : 'opac' 
194     };
195
196     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
197
198     $args->{barcode} = delete $args->{username} 
199         if $bc_regex and $username =~ /$bc_regex/;
200
201         my $response = $U->simplereq(
202         'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
203
204     if($U->event_code($response)) { 
205         # login failed, report the reason to the template
206         $ctx->{login_failed_event} = $response;
207         return Apache2::Const::OK;
208     }
209
210     # login succeeded, redirect as necessary
211
212     my $home = $self->apache->unparsed_uri;
213     $home =~ s/\/login/\/home/;
214
215     $self->apache->print(
216         $cgi->redirect(
217             -url => $cgi->param('redirect_to') || $home,
218             -cookie => $cgi->cookie(
219                 -name => 'ses',
220                 -path => '/',
221                 -secure => 1,
222                 -value => $response->{payload}->{authtoken},
223                 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
224             )
225         )
226     );
227
228     return Apache2::Const::REDIRECT;
229 }
230
231 # -----------------------------------------------------------------------------
232 # Log out and redirect to the home page
233 # -----------------------------------------------------------------------------
234 sub load_logout {
235     my $self = shift;
236
237     my $url = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
238
239     $self->apache->print(
240         $self->cgi->redirect(
241             -url => $url,
242             -cookie => $self->cgi->cookie(
243                 -name => 'ses',
244                 -path => '/',
245                 -value => '',
246                 -expires => '-1h'
247             )
248         )
249     );
250
251     return Apache2::Const::REDIRECT;
252 }
253
254 1;
255