3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
11 use C4
::Auth
qw(:DEFAULT :EditPermissions);
14 #use C4::Acquisitions;
16 use Koha
::Patron
::Categories
;
24 my $flagsrequired = { permissions
=> 1 };
25 my $member=$input->param('member');
26 my $patron = Koha
::Patrons
->find( $member );
28 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
32 my $category_type = $patron->category->category_type;
33 my $bor = $patron->unblessed;
34 if( $category_type eq 'S' ) { # FIXME Is this really needed?
35 $flagsrequired->{'staffaccess'} = 1;
37 my ($template, $loggedinuser, $cookie) = get_template_and_user
({
38 template_name
=> "members/member-flags.tt",
41 flagsrequired
=> $flagsrequired,
45 my $logged_in_user = Koha
::Patrons
->find( $loggedinuser );
46 output_and_exit_if_error
( $input, $cookie, $template, { module
=> 'members', logged_in_user
=> $logged_in_user, current_patron
=> $patron } );
49 $member2{'borrowernumber'}=$member;
51 if ($input->param('newflags')) {
53 output_and_exit
( $input, $cookie, $template, 'wrong_csrf_token' )
54 unless Koha
::Token
->new->check_csrf({
55 session_id
=> scalar $input->cookie('CGISESSID'),
56 token
=> scalar $input->param('csrf_token'),
60 my $dbh=C4
::Context
->dbh();
62 my @perms = $input->multi_param('flag');
63 my %all_module_perms = ();
65 foreach my $perm (@perms) {
67 $all_module_perms{$perm} = 1;
69 my ($module, $sub_perm) = split /:/, $perm, 2;
70 push @
{ $sub_perms{$module} }, $sub_perm;
76 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
78 while (my ($bit, $flag) = $sth->fetchrow_array) {
79 if (exists $all_module_perms{$flag}) {
80 $module_flags += 2**$bit;
84 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
85 my $old_flags = $patron->flags // 0;
86 if( ( $old_flags == 1 || $module_flags == 1 ) &&
87 $old_flags != $module_flags ) {
88 die "Non-superlibrarian is changing superlibrarian privileges" if !C4
::Context
->IsSuperLibrarian && C4
::Context
->preference('ProtectSuperlibrarianPrivileges'); # Interface should not allow this, so we can just die here
90 $sth->execute($module_flags, $member);
92 # deal with subpermissions
93 $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
94 $sth->execute($member);
95 $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
99 foreach my $module (keys %sub_perms) {
100 next if exists $all_module_perms{$module};
101 foreach my $sub_perm (@
{ $sub_perms{$module} }) {
102 $sth->execute($member, $sub_perm, $module);
106 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
110 my $dbh = C4
::Context
->dbh();
111 # FIXME This needs to be improved to avoid doing the same query
112 my $sth = $dbh->prepare("select bit,flag from userflags");
114 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
115 if ( $bor->{flags
} && $bor->{flags
} & 2**$bit ) {
116 $accessflags->{$flag} = 1;
120 my $all_perms = get_all_subpermissions
();
121 my $user_perms = get_user_subpermissions
($bor->{'userid'});
122 $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
126 while (my ($bit, $flag) = $sth->fetchrow) {
128 if ($accessflags->{$flag}) {
132 my %row = ( bit
=> $bit,
137 my @sub_perm_loop = ();
138 my $expand_parent = 0;
140 if (exists $all_perms->{$flag}) {
142 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
143 push @sub_perm_loop, {
144 id
=> "${flag}_$sub_perm",
145 perm
=> "$flag:$sub_perm",
152 if (exists $user_perms->{$flag}) {
154 # put selected ones first
155 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
156 push @sub_perm_loop, {
157 id
=> "${flag}_$sub_perm",
158 perm
=> "$flag:$sub_perm",
164 # then ones not selected
165 if (exists $all_perms->{$flag}) {
166 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
167 push @sub_perm_loop, {
168 id
=> "${flag}_$sub_perm",
169 perm
=> "$flag:$sub_perm",
172 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
176 $row{expand
} = $expand_parent;
177 if ($#sub_perm_loop > -1) {
178 $row{sub_perm_loop
} = \
@sub_perm_loop;
180 next if ( ( $row{flag
} eq 'cash_management' ) && !C4
::Context
->preference('UseCashRegisters') );
188 Koha
::Token
->new->generate_csrf( { session_id
=> scalar $input->cookie('CGISESSID'), } ),
189 disable_superlibrarian_privs
=> C4
::Context
->preference('ProtectSuperlibrarianPrivileges') ?
!C4
::Context
->IsSuperLibrarian : 0,
192 output_html_with_http_headers
$input, $cookie, $template->output;