Line 0
Link Here
|
|
|
1 |
package Koha::Auth; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use C4::Auth qw//; |
7 |
use C4::Context qw//; |
8 |
|
9 |
=head2 authenticate |
10 |
|
11 |
my $user = Koha::Auth->authenticate({ |
12 |
sessionID => $sessionID, |
13 |
}); |
14 |
|
15 |
Verifies that this user has an authenticated Koha session |
16 |
|
17 |
=cut |
18 |
|
19 |
#NOTE: This method requires that a Koha session exists |
20 |
sub authenticate { |
21 |
my ($class,$args) = @_; |
22 |
my ($auth_user,$auth_session,$auth_cookie); |
23 |
my $sessionID = $args->{sessionID}; |
24 |
if ($sessionID){ |
25 |
my $session = C4::Auth::get_session($sessionID); |
26 |
if ($session){ |
27 |
my $id = $session->param('id'); |
28 |
if ( $id ){ |
29 |
my $patrons = Koha::Patrons->search({ userid => $id }); |
30 |
if ($patrons->count){ |
31 |
$auth_user = $patrons->next; |
32 |
$auth_session = $session; |
33 |
_set_userenv_compat({ session => $session }); |
34 |
} |
35 |
} |
36 |
} |
37 |
} |
38 |
if ($auth_user){ |
39 |
#FIXME: Check for timeout |
40 |
} |
41 |
#FIXME: Ideally, it would be nice to calculate patron authorizations at login time, |
42 |
#rather than on each page load (like we do currently)... |
43 |
return ($auth_user,$auth_session,$auth_cookie); |
44 |
} |
45 |
|
46 |
#NOTE: Needed since C4::Context->userenv is so ubiquitous |
47 |
sub _set_userenv_compat { |
48 |
my ($args) = @_; |
49 |
my $session = $args->{session}; |
50 |
if ($session){ |
51 |
C4::Context->_new_userenv($session->param('id')); |
52 |
C4::Context->set_userenv( |
53 |
$session->param('number'), $session->param('id'), |
54 |
$session->param('cardnumber'), $session->param('firstname'), |
55 |
$session->param('surname'), $session->param('branch'), |
56 |
$session->param('branchname'), $session->param('flags'), |
57 |
$session->param('emailaddress'), $session->param('shibboleth'), |
58 |
$session->param('desk_id'), $session->param('desk_name') |
59 |
); |
60 |
} |
61 |
} |
62 |
|
63 |
=head2 authorize |
64 |
|
65 |
my $flags = Koha::Auth->authorize({ |
66 |
session => $session, |
67 |
flagsrequired => { self_check => 'self_checkout_module' }, |
68 |
}); |
69 |
|
70 |
=cut |
71 |
|
72 |
sub authorize { |
73 |
my ($class,$args) = @_; |
74 |
my $flags; |
75 |
my $session = $args->{session}; |
76 |
my $flagsrequired = $args->{flagsrequired}; |
77 |
if ($session && $flagsrequired){ |
78 |
my $userid = $session->param('id'); |
79 |
if ($userid){ |
80 |
$flags = C4::Auth::haspermission($userid,$flagsrequired); |
81 |
} |
82 |
} |
83 |
return $flags; |
84 |
} |
85 |
|
86 |
sub get_authz_from_flags { |
87 |
my ($args) = @_; |
88 |
my $flags = $args->{flags}; |
89 |
my $authz; |
90 |
|
91 |
#FIXME: This contains a lot of copy/paste from C4::Auth |
92 |
my $all_perms = C4::Auth::get_all_subpermissions(); |
93 |
|
94 |
if ($flags){ |
95 |
if ( $flags->{superlibrarian} == 1 ){ |
96 |
#NOTE: These are similar but slightly different to @flatroots... |
97 |
my @perms = qw/ |
98 |
circulate |
99 |
catalogue |
100 |
parameters |
101 |
borrowers |
102 |
permissions |
103 |
reserveforothers |
104 |
editcatalogue |
105 |
updatecharges |
106 |
acquisition |
107 |
tools |
108 |
editauthorities |
109 |
serials |
110 |
reports |
111 |
staffaccess |
112 |
plugins |
113 |
coursereserves |
114 |
clubs |
115 |
ill |
116 |
stockrotation |
117 |
problem_reports |
118 |
/; |
119 |
foreach my $perm (@perms){ |
120 |
$authz->{ "CAN_user_${perm}" } = 1; |
121 |
} |
122 |
foreach my $module ( keys %$all_perms ) { |
123 |
foreach my $subperm ( keys %{ $all_perms->{$module} } ) { |
124 |
$authz->{ "CAN_user_${module}_${subperm}" } = 1; |
125 |
} |
126 |
} |
127 |
} |
128 |
else { |
129 |
foreach my $module ( keys %$all_perms ) { |
130 |
if ( defined($flags->{$module}) && $flags->{$module} == 1 ) { |
131 |
foreach my $subperm ( keys %{ $all_perms->{$module} } ) { |
132 |
$authz->{ "CAN_user_${module}_${subperm}" } = 1; |
133 |
} |
134 |
} elsif ( ref( $flags->{$module} ) ) { |
135 |
foreach my $subperm ( keys %{ $flags->{$module} } ) { |
136 |
$authz->{ "CAN_user_${module}_${subperm}" } = 1; |
137 |
} |
138 |
} |
139 |
} |
140 |
foreach my $module ( keys %$flags ) { |
141 |
if ( $flags->{$module} == 1 or ref( $flags->{$module} ) ) { |
142 |
$authz->{ "CAN_user_$module" } = 1; |
143 |
} |
144 |
} |
145 |
} |
146 |
} |
147 |
|
148 |
return $authz; |
149 |
} |
150 |
|
151 |
1; |