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