Lines 24-50
use Carp qw( croak );
Link Here
|
24 |
use Digest::MD5 qw( md5_base64 ); |
24 |
use Digest::MD5 qw( md5_base64 ); |
25 |
use CGI::Session; |
25 |
use CGI::Session; |
26 |
|
26 |
|
|
|
27 |
use Encode; |
28 |
use List::MoreUtils qw( any ); |
29 |
use Net::CIDR; |
30 |
|
31 |
use C4::Auth_with_shibboleth qw( shib_ok get_login_shib login_shib_url logout_shib checkpw_shib ); |
27 |
use C4::Context; |
32 |
use C4::Context; |
28 |
use C4::Templates; # to get the template |
|
|
29 |
use C4::Languages; |
33 |
use C4::Languages; |
|
|
34 |
use C4::Log qw( logaction ); |
30 |
use C4::Search::History; |
35 |
use C4::Search::History; |
31 |
use Koha; |
36 |
use C4::Templates; # to get the template |
32 |
use Koha::Logger; |
|
|
33 |
use Koha::Caches; |
34 |
use Koha::AuthUtils qw( get_script_name hash_password ); |
37 |
use Koha::AuthUtils qw( get_script_name hash_password ); |
|
|
38 |
use Koha::Caches; |
39 |
use Koha::Cash::Registers; |
35 |
use Koha::Checkouts; |
40 |
use Koha::Checkouts; |
36 |
use Koha::DateUtils qw( dt_from_string ); |
41 |
use Koha::DateUtils qw( dt_from_string ); |
37 |
use Koha::Library::Groups; |
|
|
38 |
use Koha::Libraries; |
39 |
use Koha::Cash::Registers; |
40 |
use Koha::Desks; |
42 |
use Koha::Desks; |
41 |
use Koha::Patrons; |
43 |
use Koha::Libraries; |
|
|
44 |
use Koha::Library::Groups; |
45 |
use Koha::Logger; |
42 |
use Koha::Patron::Consents; |
46 |
use Koha::Patron::Consents; |
43 |
use List::MoreUtils qw( any ); |
47 |
use Koha::Patrons; |
44 |
use Encode; |
48 |
use Koha::Permissions; |
45 |
use C4::Auth_with_shibboleth qw( shib_ok get_login_shib login_shib_url logout_shib checkpw_shib ); |
49 |
use Koha; |
46 |
use Net::CIDR; |
|
|
47 |
use C4::Log qw( logaction ); |
48 |
|
50 |
|
49 |
# use utf8; |
51 |
# use utf8; |
50 |
|
52 |
|
Lines 1125-1131
sub checkauth {
Link Here
|
1125 |
|
1127 |
|
1126 |
if ( $return == 1 ) { |
1128 |
if ( $return == 1 ) { |
1127 |
my $select = " |
1129 |
my $select = " |
1128 |
SELECT borrowernumber, firstname, surname, flags, borrowers.branchcode, |
1130 |
SELECT borrowernumber, firstname, surname, borrowers.branchcode, |
1129 |
branches.branchname as branchname, email |
1131 |
branches.branchname as branchname, email |
1130 |
FROM borrowers |
1132 |
FROM borrowers |
1131 |
LEFT JOIN branches on borrowers.branchcode=branches.branchcode |
1133 |
LEFT JOIN branches on borrowers.branchcode=branches.branchcode |
Lines 1141-1147
sub checkauth {
Link Here
|
1141 |
} |
1143 |
} |
1142 |
} |
1144 |
} |
1143 |
if ( $sth->rows ) { |
1145 |
if ( $sth->rows ) { |
1144 |
( $borrowernumber, $firstname, $surname, $userflags, |
1146 |
( $borrowernumber, $firstname, $surname, |
1145 |
$branchcode, $branchname, $emailaddress ) = $sth->fetchrow; |
1147 |
$branchcode, $branchname, $emailaddress ) = $sth->fetchrow; |
1146 |
} |
1148 |
} |
1147 |
|
1149 |
|
Lines 1209-1215
sub checkauth {
Link Here
|
1209 |
$session->param( 'branchname', $branchname ); |
1211 |
$session->param( 'branchname', $branchname ); |
1210 |
$session->param( 'desk_id', $desk_id); |
1212 |
$session->param( 'desk_id', $desk_id); |
1211 |
$session->param( 'desk_name', $desk_name); |
1213 |
$session->param( 'desk_name', $desk_name); |
1212 |
$session->param( 'flags', $userflags ); |
|
|
1213 |
$session->param( 'emailaddress', $emailaddress ); |
1214 |
$session->param( 'emailaddress', $emailaddress ); |
1214 |
$session->param( 'ip', $session->remote_addr() ); |
1215 |
$session->param( 'ip', $session->remote_addr() ); |
1215 |
$session->param( 'lasttime', time() ); |
1216 |
$session->param( 'lasttime', time() ); |
Lines 1955-1989
sub checkpw_internal {
Link Here
|
1955 |
|
1956 |
|
1956 |
my $sth = |
1957 |
my $sth = |
1957 |
$dbh->prepare( |
1958 |
$dbh->prepare( |
1958 |
"select password,cardnumber,borrowernumber,userid,firstname,surname,borrowers.branchcode,branches.branchname,flags from borrowers join branches on borrowers.branchcode=branches.branchcode where userid=?" |
1959 |
"select password,cardnumber,borrowernumber,userid,firstname,surname,borrowers.branchcode,branches.branchname from borrowers join branches on borrowers.branchcode=branches.branchcode where userid=?" |
1959 |
); |
1960 |
); |
1960 |
$sth->execute($userid); |
1961 |
$sth->execute($userid); |
1961 |
if ( $sth->rows ) { |
1962 |
if ( $sth->rows ) { |
1962 |
my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname, |
1963 |
my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname, |
1963 |
$surname, $branchcode, $branchname, $flags ) |
1964 |
$surname, $branchcode, $branchname ) |
1964 |
= $sth->fetchrow; |
1965 |
= $sth->fetchrow; |
1965 |
|
1966 |
|
1966 |
if ( checkpw_hash( $password, $stored_hash ) ) { |
1967 |
if ( checkpw_hash( $password, $stored_hash ) ) { |
1967 |
|
1968 |
|
1968 |
C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber, |
1969 |
C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber, |
1969 |
$firstname, $surname, $branchcode, $branchname, $flags ) unless $no_set_userenv; |
1970 |
$firstname, $surname, $branchcode, $branchname ) unless $no_set_userenv; |
1970 |
return 1, $cardnumber, $userid; |
1971 |
return 1, $cardnumber, $userid; |
1971 |
} |
1972 |
} |
1972 |
} |
1973 |
} |
1973 |
$sth = |
1974 |
$sth = |
1974 |
$dbh->prepare( |
1975 |
$dbh->prepare( |
1975 |
"select password,cardnumber,borrowernumber,userid,firstname,surname,borrowers.branchcode,branches.branchname,flags from borrowers join branches on borrowers.branchcode=branches.branchcode where cardnumber=?" |
1976 |
"select password,cardnumber,borrowernumber,userid,firstname,surname,borrowers.branchcode,branches.branchname from borrowers join branches on borrowers.branchcode=branches.branchcode where cardnumber=?" |
1976 |
); |
1977 |
); |
1977 |
$sth->execute($userid); |
1978 |
$sth->execute($userid); |
1978 |
if ( $sth->rows ) { |
1979 |
if ( $sth->rows ) { |
1979 |
my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname, |
1980 |
my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname, |
1980 |
$surname, $branchcode, $branchname, $flags ) |
1981 |
$surname, $branchcode, $branchname ) |
1981 |
= $sth->fetchrow; |
1982 |
= $sth->fetchrow; |
1982 |
|
1983 |
|
1983 |
if ( checkpw_hash( $password, $stored_hash ) ) { |
1984 |
if ( checkpw_hash( $password, $stored_hash ) ) { |
1984 |
|
1985 |
|
1985 |
C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber, |
1986 |
C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber, |
1986 |
$firstname, $surname, $branchcode, $branchname, $flags ) unless $no_set_userenv; |
1987 |
$firstname, $surname, $branchcode, $branchname ) unless $no_set_userenv; |
1987 |
return 1, $cardnumber, $userid; |
1988 |
return 1, $cardnumber, $userid; |
1988 |
} |
1989 |
} |
1989 |
} |
1990 |
} |
Lines 2007-2053
sub checkpw_hash {
Link Here
|
2007 |
|
2008 |
|
2008 |
=head2 getuserflags |
2009 |
=head2 getuserflags |
2009 |
|
2010 |
|
2010 |
my $authflags = getuserflags($flags, $userid, [$dbh]); |
2011 |
my $authflags = getuserflags($userid); |
2011 |
|
2012 |
|
2012 |
Translates integer flags into permissions strings hash. |
2013 |
Translates integer flags into permissions strings hash. |
2013 |
|
2014 |
|
2014 |
C<$flags> is the integer userflags value ( borrowers.userflags ) |
|
|
2015 |
C<$userid> is the members.userid, used for building subpermissions |
2015 |
C<$userid> is the members.userid, used for building subpermissions |
2016 |
C<$authflags> is a hashref of permissions |
|
|
2017 |
|
2016 |
|
2018 |
=cut |
2017 |
=cut |
2019 |
|
2018 |
|
2020 |
sub getuserflags { |
2019 |
sub getuserflags { |
2021 |
my $flags = shift; |
2020 |
my $params = shift; |
2022 |
my $userid = shift; |
2021 |
|
2023 |
my $dbh = @_ ? shift : C4::Context->dbh; |
2022 |
my $userid = $params->{userid}; |
|
|
2023 |
my $force_subpermissions = $params->{force_subpermissions}; |
2024 |
|
2025 |
my $patron = Koha::Patrons->find( { userid => $userid } ); |
2026 |
return unless $patron; |
2027 |
|
2024 |
my $userflags; |
2028 |
my $userflags; |
2025 |
{ |
2029 |
my @permissions = Koha::Permissions->search( { parent => undef } ); |
2026 |
# I don't want to do this, but if someone logs in as the database |
2030 |
foreach my $p (@permissions) { |
2027 |
# user, it would be preferable not to spam them to death with |
2031 |
my $patron_has = $p->patron_has( $patron->id ); |
2028 |
# numeric warnings. So, we make $flags numeric. |
|
|
2029 |
no warnings 'numeric'; |
2030 |
$flags += 0; |
2031 |
} |
2032 |
my $sth = $dbh->prepare("SELECT bit, flag, defaulton FROM userflags"); |
2033 |
$sth->execute; |
2034 |
|
2032 |
|
2035 |
while ( my ( $bit, $flag, $defaulton ) = $sth->fetchrow ) { |
2033 |
if ( $patron_has && !$force_subpermissions ) { |
2036 |
if ( ( $flags & ( 2**$bit ) ) || $defaulton ) { |
2034 |
$userflags->{ $p->code() } = $p->patron_has( $patron->id ); |
2037 |
$userflags->{$flag} = 1; |
|
|
2038 |
} |
2035 |
} |
2039 |
else { |
2036 |
else { |
2040 |
$userflags->{$flag} = 0; |
2037 |
my @subpermissions = $p->subpermissions; |
|
|
2038 |
foreach my $sp ( $p->subpermissions ) { |
2039 |
$userflags->{ $p->code } = $sp->patron_has( $patron->id ); |
2040 |
} |
2041 |
} |
2041 |
} |
2042 |
} |
2042 |
} |
2043 |
|
2043 |
|
2044 |
# get subpermissions and merge with top-level permissions |
|
|
2045 |
my $user_subperms = get_user_subpermissions($userid); |
2046 |
foreach my $module ( keys %$user_subperms ) { |
2047 |
next if $userflags->{$module} == 1; # user already has permission for everything in this module |
2048 |
$userflags->{$module} = $user_subperms->{$module}; |
2049 |
} |
2050 |
|
2051 |
return $userflags; |
2044 |
return $userflags; |
2052 |
} |
2045 |
} |
2053 |
|
2046 |
|
Lines 2079-2098
necessary to check borrowers.flags.
Link Here
|
2079 |
sub get_user_subpermissions { |
2072 |
sub get_user_subpermissions { |
2080 |
my $userid = shift; |
2073 |
my $userid = shift; |
2081 |
|
2074 |
|
2082 |
my $dbh = C4::Context->dbh; |
2075 |
return getuserflags( { userid => $userid, force_subpermissions => 1 } ); |
2083 |
my $sth = $dbh->prepare( "SELECT flag, user_permissions.code |
|
|
2084 |
FROM user_permissions |
2085 |
JOIN permissions USING (module_bit, code) |
2086 |
JOIN userflags ON (module_bit = bit) |
2087 |
JOIN borrowers USING (borrowernumber) |
2088 |
WHERE userid = ?" ); |
2089 |
$sth->execute($userid); |
2090 |
|
2091 |
my $user_perms = {}; |
2092 |
while ( my $perm = $sth->fetchrow_hashref ) { |
2093 |
$user_perms->{ $perm->{'flag'} }->{ $perm->{'code'} } = 1; |
2094 |
} |
2095 |
return $user_perms; |
2096 |
} |
2076 |
} |
2097 |
|
2077 |
|
2098 |
=head2 get_all_subpermissions |
2078 |
=head2 get_all_subpermissions |
Lines 2100-2124
sub get_user_subpermissions {
Link Here
|
2100 |
my $perm_hashref = get_all_subpermissions(); |
2080 |
my $perm_hashref = get_all_subpermissions(); |
2101 |
|
2081 |
|
2102 |
Returns a hashref of hashrefs defining all specific |
2082 |
Returns a hashref of hashrefs defining all specific |
2103 |
permissions currently defined. The return value |
2083 |
permissions currently defined. The return value has |
2104 |
has the same structure as that of C<get_user_subpermissions>, |
2084 |
the same basic structure as that of C<get_user_subpermissions> |
2105 |
except that the innermost hash value is the description |
|
|
2106 |
of the subpermission. |
2107 |
|
2085 |
|
2108 |
=cut |
2086 |
=cut |
2109 |
|
2087 |
|
2110 |
sub get_all_subpermissions { |
2088 |
sub get_all_subpermissions { |
2111 |
my $dbh = C4::Context->dbh; |
2089 |
my $permissions; |
2112 |
my $sth = $dbh->prepare( "SELECT flag, code |
2090 |
|
2113 |
FROM permissions |
2091 |
my @permissions = Koha::Permissions->search({ parent => undef }); |
2114 |
JOIN userflags ON (module_bit = bit)" ); |
2092 |
foreach my $p ( @permissions ) { |
2115 |
$sth->execute(); |
2093 |
foreach my $sp ( $p->subpermissions ) { |
2116 |
|
2094 |
$permissions->{ $p->code }->{ $sp->code } = 1; |
2117 |
my $all_perms = {}; |
2095 |
} |
2118 |
while ( my $perm = $sth->fetchrow_hashref ) { |
|
|
2119 |
$all_perms->{ $perm->{'flag'} }->{ $perm->{'code'} } = 1; |
2120 |
} |
2096 |
} |
2121 |
return $all_perms; |
2097 |
|
|
|
2098 |
return $permissions; |
2122 |
} |
2099 |
} |
2123 |
|
2100 |
|
2124 |
=head2 haspermission |
2101 |
=head2 haspermission |
Lines 2180-2192
sub _dispatch {
Link Here
|
2180 |
sub haspermission { |
2157 |
sub haspermission { |
2181 |
my ( $userid, $flagsrequired ) = @_; |
2158 |
my ( $userid, $flagsrequired ) = @_; |
2182 |
|
2159 |
|
2183 |
#Koha::Exceptions::WrongParameter->throw('$flagsrequired should not be undef') |
2160 |
my $flags = getuserflags( { userid => $userid } ); |
2184 |
# unless defined($flagsrequired); |
|
|
2185 |
|
2161 |
|
2186 |
my $sth = C4::Context->dbh->prepare("SELECT flags FROM borrowers WHERE userid=?"); |
2162 |
if ( $userid eq C4::Context->config('user') ) { |
2187 |
$sth->execute($userid); |
2163 |
|
2188 |
my $row = $sth->fetchrow(); |
2164 |
# Super User Account from /etc/koha.conf |
2189 |
my $flags = getuserflags( $row, $userid ); |
2165 |
$flags->{'superlibrarian'} = 1; |
|
|
2166 |
} |
2190 |
|
2167 |
|
2191 |
return $flags unless defined($flagsrequired); |
2168 |
return $flags unless defined($flagsrequired); |
2192 |
return $flags if $flags->{superlibrarian}; |
2169 |
return $flags if $flags->{superlibrarian}; |