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",
42 flagsrequired
=> $flagsrequired,
46 my $logged_in_user = Koha
::Patrons
->find( $loggedinuser ) or die "Not logged in";
47 output_and_exit_if_error
( $input, $cookie, $template, { module
=> 'members', logged_in_user
=> $logged_in_user, current_patron
=> $patron } );
50 $member2{'borrowernumber'}=$member;
52 if ($input->param('newflags')) {
54 output_and_exit
( $input, $cookie, $template, 'wrong_csrf_token' )
55 unless Koha
::Token
->new->check_csrf({
56 session_id
=> scalar $input->cookie('CGISESSID'),
57 token
=> scalar $input->param('csrf_token'),
61 my $dbh=C4
::Context
->dbh();
63 my @perms = $input->multi_param('flag');
64 my %all_module_perms = ();
66 foreach my $perm (@perms) {
68 $all_module_perms{$perm} = 1;
70 my ($module, $sub_perm) = split /:/, $perm, 2;
71 push @
{ $sub_perms{$module} }, $sub_perm;
77 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
79 while (my ($bit, $flag) = $sth->fetchrow_array) {
80 if (exists $all_module_perms{$flag}) {
81 $module_flags += 2**$bit;
85 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
86 my $old_flags = $patron->flags // 0;
87 if( ( $old_flags == 1 || $module_flags == 1 ) &&
88 $old_flags != $module_flags ) {
89 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
91 $sth->execute($module_flags, $member);
93 # deal with subpermissions
94 $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
95 $sth->execute($member);
96 $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
100 foreach my $module (keys %sub_perms) {
101 next if exists $all_module_perms{$module};
102 foreach my $sub_perm (@
{ $sub_perms{$module} }) {
103 $sth->execute($member, $sub_perm, $module);
107 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
111 my $dbh = C4
::Context
->dbh();
112 # FIXME This needs to be improved to avoid doing the same query
113 my $sth = $dbh->prepare("select bit,flag from userflags");
115 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
116 if ( $bor->{flags
} && $bor->{flags
} & 2**$bit ) {
117 $accessflags->{$flag} = 1;
121 my $all_perms = get_all_subpermissions
();
122 my $user_perms = get_user_subpermissions
($bor->{'userid'});
123 $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
127 while (my ($bit, $flag) = $sth->fetchrow) {
129 if ($accessflags->{$flag}) {
133 my %row = ( bit
=> $bit,
138 my @sub_perm_loop = ();
139 my $expand_parent = 0;
141 if (exists $all_perms->{$flag}) {
143 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
144 push @sub_perm_loop, {
145 id
=> "${flag}_$sub_perm",
146 perm
=> "$flag:$sub_perm",
153 if (exists $user_perms->{$flag}) {
155 # put selected ones first
156 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
157 push @sub_perm_loop, {
158 id
=> "${flag}_$sub_perm",
159 perm
=> "$flag:$sub_perm",
165 # then ones not selected
166 if (exists $all_perms->{$flag}) {
167 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
168 push @sub_perm_loop, {
169 id
=> "${flag}_$sub_perm",
170 perm
=> "$flag:$sub_perm",
173 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
177 $row{expand
} = $expand_parent;
178 if ($#sub_perm_loop > -1) {
179 $row{sub_perm_loop
} = \
@sub_perm_loop;
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;