Bug 20100: Disallow access to superlib privileges at server side
[koha.git] / members / member-flags.pl
1 #!/usr/bin/perl
2
3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
6
7 use Modern::Perl;
8
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;
16
17 use Koha::Patron::Categories;
18 use Koha::Patrons;
19
20 use C4::Output;
21 use Koha::Token;
22
23 my $input = new CGI;
24
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;
31 }
32
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;
37 }
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 });
46
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 } );
49
50 my %member2;
51 $member2{'borrowernumber'}=$member;
52
53 if ($input->param('newflags')) {
54
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'),
59         });
60
61
62     my $dbh=C4::Context->dbh();
63
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;
73         }
74     }
75
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;
83         }
84     }
85     
86     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
87     if( !C4::Context->preference('ProtectSuperlibPrivs') || C4::Context->IsSuperLibrarian ) {
88         $sth->execute($module_flags, $member);
89     } else {
90         my $old_flags = $patron->flags // 0;
91         if( ( $old_flags == 1 || $module_flags == 1 ) &&
92               $old_flags != $module_flags ) {
93            die "Non-superlibrarian is changing superlibrarian privileges"; # Interface should not allow this, so we can just die here
94         } else {
95             $sth->execute($module_flags, $member);
96         }
97     }
98     
99     # deal with subpermissions
100     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
101     $sth->execute($member); 
102     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
103                         SELECT ?, bit, ?
104                         FROM userflags
105                         WHERE flag = ?");
106     foreach my $module (keys %sub_perms) {
107         next if exists $all_module_perms{$module};
108         foreach my $sub_perm (@{ $sub_perms{$module} }) {
109             $sth->execute($member, $sub_perm, $module);
110         }
111     }
112     
113     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
114 } else {
115
116     my $accessflags;
117     my $dbh = C4::Context->dbh();
118     # FIXME This needs to be improved to avoid doing the same query
119     my $sth = $dbh->prepare("select bit,flag from userflags");
120     $sth->execute;
121     while ( my ( $bit, $flag ) = $sth->fetchrow ) {
122         if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
123             $accessflags->{$flag} = 1;
124         }
125     }
126
127     my $all_perms  = get_all_subpermissions();
128     my $user_perms = get_user_subpermissions($bor->{'userid'});
129     $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
130     $sth->execute;
131     my @loop;
132
133     while (my ($bit, $flag) = $sth->fetchrow) {
134         my $checked='';
135         if ($accessflags->{$flag}) {
136             $checked= 1;
137         }
138
139         my %row = ( bit => $bit,
140             flag => $flag,
141             checked => $checked,
142         );
143
144         my @sub_perm_loop = ();
145         my $expand_parent = 0;
146         if ($checked) {
147             if (exists $all_perms->{$flag}) {
148                 $expand_parent = 1;
149                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
150                     push @sub_perm_loop, {
151                         id => "${flag}_$sub_perm",
152                         perm => "$flag:$sub_perm",
153                         code => $sub_perm,
154                         checked => 1
155                     };
156                 }
157             }
158         } else {
159             if (exists $user_perms->{$flag}) {
160                 $expand_parent = 1;
161                 # put selected ones first
162                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
163                     push @sub_perm_loop, {
164                         id => "${flag}_$sub_perm",
165                         perm => "$flag:$sub_perm",
166                         code => $sub_perm,
167                         checked => 1
168                     };
169                 }
170             }
171             # then ones not selected
172             if (exists $all_perms->{$flag}) {
173                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
174                     push @sub_perm_loop, {
175                         id => "${flag}_$sub_perm",
176                         perm => "$flag:$sub_perm",
177                         code => $sub_perm,
178                         checked => 0
179                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
180                 }
181             }
182         }
183         $row{expand} = $expand_parent;
184         if ($#sub_perm_loop > -1) {
185             $row{sub_perm_loop} = \@sub_perm_loop;
186         }
187         push @loop, \%row;
188     }
189
190     if ( $patron->is_child ) {
191         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
192         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
193         $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
194     }
195
196 if (C4::Context->preference('ExtendedPatronAttributes')) {
197     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
198     $template->param(
199         ExtendedPatronAttributes => 1,
200         extendedattributes => $attributes
201     );
202 }
203
204 $template->param(
205     patron         => $patron,
206     loop           => \@loop,
207     csrf_token =>
208         Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
209     disable_superlibrarian_privs => C4::Context->preference('ProtectSuperlibPrivs') ? !C4::Context->IsSuperLibrarian : 0,
210 );
211
212     output_html_with_http_headers $input, $cookie, $template->output;
213
214 }