]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Reporter/Proxy.pm
LP1915464 follow-up: use spaces, not tabs; remove extra comma
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Reporter / Proxy.pm
1 package OpenILS::Reporter::Proxy;
2 use strict; use warnings;
3
4 use Apache2::Log;
5 use Apache2::Const -compile => qw(REDIRECT FORBIDDEN OK NOT_FOUND DECLINED :log);
6 use APR::Const    -compile => qw(:error SUCCESS);
7 use CGI;
8 use Data::Dumper;
9 use Digest::MD5 qw/md5_hex/;
10
11 use OpenSRF::EX qw(:try);
12 use OpenSRF::System;
13
14
15 # set the bootstrap config and template include directory when 
16 # this module is loaded
17 my $bootstrap;
18
19 sub import {
20     my $self = shift;
21     $bootstrap = shift;
22 }
23
24
25 sub child_init {
26     OpenSRF::System->bootstrap_client( config_file => $bootstrap );
27 }
28
29 sub handler {
30     my $apache = shift;
31     my $cgi = new CGI;
32     my $auth_ses = $cgi->cookie('ses') || $cgi->cookie('eg.auth.token');
33     if ($auth_ses =~ /^"(.+)"$/) {
34         $auth_ses = $1;
35     }
36     my $ws_ou = $cgi->cookie('ws_ou') || 1;
37
38     my $url = $cgi->url;
39
40     # push everyone to the secure site
41     if ($url =~ /^http:/o) {
42         $url =~ s/^http:/https:/o;
43         print "Location: $url\n\n";
44         return Apache2::Const::OK;
45     }
46
47     if (!$auth_ses) {
48         my $u = $cgi->param('user');
49         my $p = $cgi->param('passwd');
50
51         if (!$u) {
52
53             print $cgi->header(-type=>'text/html', -expires=>'-1d');
54             print <<"            HTML";
55
56 <html>
57     <head>
58         <title>Report Output Login</title>
59     </head>
60     <body>
61         <br/><br/><br/>
62         <center>
63         <form method='POST'>
64             <table style='border-collapse: collapse; border: 1px solid black;'>
65                 <tr>
66                     <th colspan='2' align='center'><u>Please log in to view reports</u></th>
67                 </tr>
68                 <tr>
69                     <th align="right">Username or barcode:</th>
70                     <td><input type="text" name="user"/></td>
71                 </tr>
72                 <tr>
73                     <th align="right">Password:</th>
74                     <td><input type="password" name="passwd"/></td>
75                 </tr>
76             </table>
77             <input type="submit" value="Log in"/>
78         </form>
79         </center>
80     </body>
81 </html>
82
83             HTML
84             return Apache2::Const::OK;
85         }
86
87         $auth_ses = oils_login($u, $p);
88         if ($auth_ses) {
89             print $cgi->redirect(
90                 -uri=>$url,
91                 -cookie=>$cgi->cookie(
92                     -name=>'ses',
93                     -value=>$auth_ses,
94                     -path=>'/',-expires=>'+1h'
95                 )
96             );
97             return Apache2::Const::REDIRECT;
98         }
99     }
100
101     my $user = verify_login($auth_ses);
102     return Apache2::Const::FORBIDDEN unless ($user);
103
104     my $failures = OpenSRF::AppSession
105         ->create('open-ils.actor')
106         ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, ['VIEW_REPORT_OUTPUT'])
107         ->gather(1);
108
109     return Apache2::Const::FORBIDDEN if (@$failures > 0);
110
111     # they're good, let 'em through
112     return Apache2::Const::DECLINED if (-e $apache->filename);
113
114     # oops, file not found
115     return Apache2::Const::NOT_FOUND;
116 }
117
118 # returns the user object if the session is valid, 0 otherwise
119 sub verify_login {
120     my $auth_token = shift;
121     return undef unless $auth_token;
122
123     my $user = OpenSRF::AppSession
124         ->create("open-ils.auth")
125         ->request( "open-ils.auth.session.retrieve", $auth_token )
126         ->gather(1);
127
128     if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
129         return undef;
130     }
131
132     return $user if ref($user);
133     return undef;
134 }
135
136 sub oils_login {
137         my( $username, $password, $type ) = @_;
138
139         $type |= "staff";
140     my $nametype = 'username';
141     $nametype = 'barcode' if ($username =~ /^\d+$/o);
142
143         my $seed = OpenSRF::AppSession
144         ->create("open-ils.auth")
145         ->request( 'open-ils.auth.authenticate.init', $username )
146         ->gather(1);
147
148         return undef unless $seed;
149
150         my $response = OpenSRF::AppSession
151         ->create("open-ils.auth")
152         ->request( 'open-ils.auth.authenticate.complete',
153             { $nametype => $username,
154               password => md5_hex($seed . md5_hex($password)),
155               type => $type })
156         ->gather(1);
157
158         return undef unless $response;
159
160         return $response->{payload}->{authtoken};
161 }
162
163
164
165 1;