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
::Members
::Attributes
qw(GetBorrowerAttributes);
15 #use C4::Acquisitions;
17 use Koha
::Patron
::Categories
;
25 my $flagsrequired = { permissions
=> 1 };
26 my $member=$input->param('member');
27 my $patron = Koha
::Patrons
->find( $member );
29 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
33 my $category_type = $patron->category->category_type;
34 my $bor = $patron->unblessed;
35 if( $category_type eq 'S' ) { # FIXME Is this really needed?
36 $flagsrequired->{'staffaccess'} = 1;
38 my ($template, $loggedinuser, $cookie) = get_template_and_user
({
39 template_name
=> "members/member-flags.tt",
43 flagsrequired
=> $flagsrequired,
47 my $logged_in_user = Koha
::Patrons
->find( $loggedinuser ) or die "Not logged in";
48 output_and_exit_if_error
( $input, $cookie, $template, { module
=> 'members', logged_in_user
=> $logged_in_user, current_patron
=> $patron } );
51 $member2{'borrowernumber'}=$member;
53 if ($input->param('newflags')) {
55 die "Wrong CSRF token"
56 unless Koha
::Token
->new->check_csrf({
57 session_id
=> scalar $input->cookie('CGISESSID'),
58 token
=> scalar $input->param('csrf_token'),
62 my $dbh=C4
::Context
->dbh();
64 my @perms = $input->multi_param('flag');
65 my %all_module_perms = ();
67 foreach my $perm (@perms) {
69 $all_module_perms{$perm} = 1;
71 my ($module, $sub_perm) = split /:/, $perm, 2;
72 push @
{ $sub_perms{$module} }, $sub_perm;
78 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
80 while (my ($bit, $flag) = $sth->fetchrow_array) {
81 if (exists $all_module_perms{$flag}) {
82 $module_flags += 2**$bit;
86 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
87 my $old_flags = $patron->flags // 0;
88 if( ( $old_flags == 1 || $module_flags == 1 ) &&
89 $old_flags != $module_flags ) {
90 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
92 $sth->execute($module_flags, $member);
94 # deal with subpermissions
95 $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
96 $sth->execute($member);
97 $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
101 foreach my $module (keys %sub_perms) {
102 next if exists $all_module_perms{$module};
103 foreach my $sub_perm (@
{ $sub_perms{$module} }) {
104 $sth->execute($member, $sub_perm, $module);
108 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
112 my $dbh = C4
::Context
->dbh();
113 # FIXME This needs to be improved to avoid doing the same query
114 my $sth = $dbh->prepare("select bit,flag from userflags");
116 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
117 if ( $bor->{flags
} && $bor->{flags
} & 2**$bit ) {
118 $accessflags->{$flag} = 1;
122 my $all_perms = get_all_subpermissions
();
123 my $user_perms = get_user_subpermissions
($bor->{'userid'});
124 $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
128 while (my ($bit, $flag) = $sth->fetchrow) {
130 if ($accessflags->{$flag}) {
134 my %row = ( bit
=> $bit,
139 my @sub_perm_loop = ();
140 my $expand_parent = 0;
142 if (exists $all_perms->{$flag}) {
144 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
145 push @sub_perm_loop, {
146 id
=> "${flag}_$sub_perm",
147 perm
=> "$flag:$sub_perm",
154 if (exists $user_perms->{$flag}) {
156 # put selected ones first
157 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
158 push @sub_perm_loop, {
159 id
=> "${flag}_$sub_perm",
160 perm
=> "$flag:$sub_perm",
166 # then ones not selected
167 if (exists $all_perms->{$flag}) {
168 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
169 push @sub_perm_loop, {
170 id
=> "${flag}_$sub_perm",
171 perm
=> "$flag:$sub_perm",
174 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
178 $row{expand
} = $expand_parent;
179 if ($#sub_perm_loop > -1) {
180 $row{sub_perm_loop
} = \
@sub_perm_loop;
185 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
186 my $attributes = GetBorrowerAttributes
($bor->{'borrowernumber'});
188 ExtendedPatronAttributes
=> 1,
189 extendedattributes
=> $attributes
197 Koha
::Token
->new->generate_csrf( { session_id
=> scalar $input->cookie('CGISESSID'), } ),
198 disable_superlibrarian_privs
=> C4
::Context
->preference('ProtectSuperlibrarianPrivileges') ?
!C4
::Context
->IsSuperLibrarian : 0,
201 output_html_with_http_headers
$input, $cookie, $template->output;