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