Bug 17905: FIX CSRF in member-flags
[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 strict;
8 use warnings;
9
10 use CGI qw ( -utf8 );
11 use Digest::MD5 qw(md5_base64);
12 use Encode qw( encode );
13 use C4::Output;
14 use C4::Auth qw(:DEFAULT :EditPermissions);
15 use C4::Context;
16 use C4::Members;
17 use C4::Members::Attributes qw(GetBorrowerAttributes);
18 #use C4::Acquisitions;
19
20 use Koha::Patron::Categories;
21
22 use C4::Output;
23 use Koha::Patron::Images;
24 use Koha::Token;
25
26 my $input = new CGI;
27
28 my $flagsrequired = { permissions => 1 };
29 my $member=$input->param('member');
30 my $bor = GetMember( borrowernumber => $member );
31 if( $bor->{'category_type'} eq 'S' )  {
32         $flagsrequired->{'staffaccess'} = 1;
33 }
34 my ($template, $loggedinuser, $cookie) = get_template_and_user({
35         template_name   => "members/member-flags.tt",
36         query           => $input,
37         type            => "intranet",
38         authnotrequired => 0,
39         flagsrequired   => $flagsrequired,
40         debug           => 1,
41 });
42
43
44 my %member2;
45 $member2{'borrowernumber'}=$member;
46
47 if ($input->param('newflags')) {
48
49     die "Wrong CSRF token"
50         unless Koha::Token->new->check_csrf({
51             id     => Encode::encode( 'UTF-8', C4::Context->userenv->{id} ),
52             secret => md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ),
53             token  => scalar $input->param('csrf_token'),
54         });
55
56
57     my $dbh=C4::Context->dbh();
58
59     my @perms = $input->multi_param('flag');
60     my %all_module_perms = ();
61     my %sub_perms = ();
62     foreach my $perm (@perms) {
63         if ($perm !~ /:/) {
64             $all_module_perms{$perm} = 1;
65         } else {
66             my ($module, $sub_perm) = split /:/, $perm, 2;
67             push @{ $sub_perms{$module} }, $sub_perm;
68         }
69     }
70
71     # construct flags
72     my $module_flags = 0;
73     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
74     $sth->execute();
75     while (my ($bit, $flag) = $sth->fetchrow_array) {
76         if (exists $all_module_perms{$flag}) {
77             $module_flags += 2**$bit;
78         }
79     }
80     
81     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
82     $sth->execute($module_flags, $member);
83     
84     # deal with subpermissions
85     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
86     $sth->execute($member); 
87     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
88                         SELECT ?, bit, ?
89                         FROM userflags
90                         WHERE flag = ?");
91     foreach my $module (keys %sub_perms) {
92         next if exists $all_module_perms{$module};
93         foreach my $sub_perm (@{ $sub_perms{$module} }) {
94             $sth->execute($member, $sub_perm, $module);
95         }
96     }
97     
98     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
99 } else {
100
101     my $flags = C4::Members::patronflags( $bor );
102     my $accessflags;
103     my $dbh = C4::Context->dbh();
104     # FIXME This needs to be improved to avoid doing the same query
105     my $sth = $dbh->prepare("select bit,flag from userflags");
106     $sth->execute;
107     while ( my ( $bit, $flag ) = $sth->fetchrow ) {
108         if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
109             $accessflags->{$flag} = 1;
110         }
111     }
112
113     my $all_perms  = get_all_subpermissions();
114     my $user_perms = get_user_subpermissions($bor->{'userid'});
115     $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
116     $sth->execute;
117     my @loop;
118
119     while (my ($bit, $flag) = $sth->fetchrow) {
120             my $checked='';
121             if ($accessflags->{$flag}) {
122                 $checked= 1;
123             }
124
125             my %row = ( bit => $bit,
126                     flag => $flag,
127                     checked => $checked,
128         );
129
130         my @sub_perm_loop = ();
131         my $expand_parent = 0;
132         if ($checked) {
133             if (exists $all_perms->{$flag}) {
134                 $expand_parent = 1;
135                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
136                     push @sub_perm_loop, {
137                         id => "${flag}_$sub_perm",
138                         perm => "$flag:$sub_perm",
139                         code => $sub_perm,
140                         checked => 1
141                     };
142                 }
143             }
144         } else {
145             if (exists $user_perms->{$flag}) {
146                 $expand_parent = 1;
147                 # put selected ones first
148                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
149                     push @sub_perm_loop, {
150                         id => "${flag}_$sub_perm",
151                         perm => "$flag:$sub_perm",
152                         code => $sub_perm,
153                         checked => 1
154                     };
155                 }
156             }
157             # then ones not selected
158             if (exists $all_perms->{$flag}) {
159                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
160                     push @sub_perm_loop, {
161                         id => "${flag}_$sub_perm",
162                         perm => "$flag:$sub_perm",
163                         code => $sub_perm,
164                         checked => 0
165                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
166                 }
167             }
168         }
169         $row{expand} = $expand_parent;
170         if ($#sub_perm_loop > -1) {
171             $row{sub_perm_loop} = \@sub_perm_loop;
172         }
173             push @loop, \%row;
174     }
175
176     if ( $bor->{'category_type'} eq 'C') {
177         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
178         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
179         $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
180     }
181         
182 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
183     my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
184     $template->param( picture => 1 ) if $patron_image;
185
186 if (C4::Context->preference('ExtendedPatronAttributes')) {
187     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
188     $template->param(
189         ExtendedPatronAttributes => 1,
190         extendedattributes => $attributes
191     );
192 }
193
194 $template->param(
195                 borrowernumber => $bor->{'borrowernumber'},
196     cardnumber => $bor->{'cardnumber'},
197                 surname => $bor->{'surname'},
198                 firstname => $bor->{'firstname'},
199         othernames => $bor->{'othernames'},
200                 categorycode => $bor->{'categorycode'},
201                 category_type => $bor->{'category_type'},
202                 categoryname => $bor->{'description'},
203         address => $bor->{address},
204                 address2 => $bor->{'address2'},
205         streettype => $bor->{streettype},
206                 city => $bor->{'city'},
207         state => $bor->{'state'},
208                 zipcode => $bor->{'zipcode'},
209                 country => $bor->{'country'},
210                 phone => $bor->{'phone'},
211         phonepro => $bor->{'phonepro'},
212         mobile => $bor->{'mobile'},
213                 email => $bor->{'email'},
214         emailpro => $bor->{'emailpro'},
215                 branchcode => $bor->{'branchcode'},
216                 loop => \@loop,
217                 is_child        => ($bor->{'category_type'} eq 'C'),
218                 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
219         RoutingSerials => C4::Context->preference('RoutingSerials'),
220         csrf_token => Koha::Token->new->generate_csrf(
221             {   id     => Encode::encode( 'UTF-8', C4::Context->userenv->{id} ),
222                 secret => md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ),
223             }
224         ),
225                 );
226
227     output_html_with_http_headers $input, $cookie, $template->output;
228
229 }