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