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