Bug 24083: Add circ rules management
[koha.git] / members / member-flags.pl
blob6ba2005ff9910a1aeeb0b2ad6efb5689d00458f1
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::Acquisitions;
16 use Koha::Patron::Categories;
17 use Koha::Patrons;
19 use C4::Output;
20 use Koha::Token;
22 my $input = CGI->new;
24 my $flagsrequired = { permissions => 1 };
25 my $member=$input->param('member');
26 my $patron = Koha::Patrons->find( $member );
27 unless ( $patron ) {
28 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
29 exit;
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",
39 query => $input,
40 type => "intranet",
41 flagsrequired => $flagsrequired,
42 debug => 1,
43 });
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 } );
48 my %member2;
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'),
57 });
60 my $dbh=C4::Context->dbh();
62 my @perms = $input->multi_param('flag');
63 my %all_module_perms = ();
64 my %sub_perms = ();
65 foreach my $perm (@perms) {
66 if ($perm !~ /:/) {
67 $all_module_perms{$perm} = 1;
68 } else {
69 my ($module, $sub_perm) = split /:/, $perm, 2;
70 push @{ $sub_perms{$module} }, $sub_perm;
74 # construct flags
75 my $module_flags = 0;
76 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
77 $sth->execute();
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)
96 SELECT ?, bit, ?
97 FROM userflags
98 WHERE flag = ?");
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");
107 } else {
109 my $accessflags;
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");
113 $sth->execute;
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");
123 $sth->execute;
124 my @loop;
126 while (my ($bit, $flag) = $sth->fetchrow) {
127 my $checked='';
128 if ($accessflags->{$flag}) {
129 $checked= 1;
132 my %row = ( bit => $bit,
133 flag => $flag,
134 checked => $checked,
137 my @sub_perm_loop = ();
138 my $expand_parent = 0;
139 if ($checked) {
140 if (exists $all_perms->{$flag}) {
141 $expand_parent = 1;
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",
146 code => $sub_perm,
147 checked => 1
151 } else {
152 if (exists $user_perms->{$flag}) {
153 $expand_parent = 1;
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",
159 code => $sub_perm,
160 checked => 1
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",
170 code => $sub_perm,
171 checked => 0
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') );
181 push @loop, \%row;
184 $template->param(
185 patron => $patron,
186 loop => \@loop,
187 csrf_token =>
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;