Bug 22847: [18.11] Correctly displayed circ rule values for max_holds
[koha.git] / members / member-flags.pl
blob10324dd959e2799f3ae284963d1a0be76322b078
1 #!/usr/bin/perl
3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
7 use Modern::Perl;
9 use CGI qw ( -utf8 );
10 use C4::Output;
11 use C4::Auth qw(:DEFAULT :EditPermissions);
12 use C4::Context;
13 use C4::Members;
14 use C4::Members::Attributes qw(GetBorrowerAttributes);
15 #use C4::Acquisitions;
17 use Koha::Patron::Categories;
18 use Koha::Patrons;
20 use C4::Output;
21 use Koha::Token;
23 my $input = new CGI;
25 my $flagsrequired = { permissions => 1 };
26 my $member=$input->param('member');
27 my $patron = Koha::Patrons->find( $member );
28 unless ( $patron ) {
29 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
30 exit;
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",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => $flagsrequired,
44 debug => 1,
45 });
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 } );
50 my %member2;
51 $member2{'borrowernumber'}=$member;
53 if ($input->param('newflags')) {
55 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
56 unless Koha::Token->new->check_csrf({
57 session_id => scalar $input->cookie('CGISESSID'),
58 token => scalar $input->param('csrf_token'),
59 });
62 my $dbh=C4::Context->dbh();
64 my @perms = $input->multi_param('flag');
65 my %all_module_perms = ();
66 my %sub_perms = ();
67 foreach my $perm (@perms) {
68 if ($perm !~ /:/) {
69 $all_module_perms{$perm} = 1;
70 } else {
71 my ($module, $sub_perm) = split /:/, $perm, 2;
72 push @{ $sub_perms{$module} }, $sub_perm;
76 # construct flags
77 my $module_flags = 0;
78 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
79 $sth->execute();
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)
98 SELECT ?, bit, ?
99 FROM userflags
100 WHERE flag = ?");
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");
109 } else {
111 my $accessflags;
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");
115 $sth->execute;
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");
125 $sth->execute;
126 my @loop;
128 while (my ($bit, $flag) = $sth->fetchrow) {
129 my $checked='';
130 if ($accessflags->{$flag}) {
131 $checked= 1;
134 my %row = ( bit => $bit,
135 flag => $flag,
136 checked => $checked,
139 my @sub_perm_loop = ();
140 my $expand_parent = 0;
141 if ($checked) {
142 if (exists $all_perms->{$flag}) {
143 $expand_parent = 1;
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",
148 code => $sub_perm,
149 checked => 1
153 } else {
154 if (exists $user_perms->{$flag}) {
155 $expand_parent = 1;
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",
161 code => $sub_perm,
162 checked => 1
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",
172 code => $sub_perm,
173 checked => 0
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;
182 push @loop, \%row;
185 if (C4::Context->preference('ExtendedPatronAttributes')) {
186 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
187 $template->param(
188 ExtendedPatronAttributes => 1,
189 extendedattributes => $attributes
193 $template->param(
194 patron => $patron,
195 loop => \@loop,
196 csrf_token =>
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;