Bug 18298: minPaswordLength should not be < 3
[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 C4::Output;
12 use C4::Auth qw(:DEFAULT :EditPermissions);
13 use C4::Context;
14 use C4::Members;
15 use C4::Members::Attributes qw(GetBorrowerAttributes);
16 #use C4::Acquisitions;
17
18 use Koha::Patron::Categories;
19 use Koha::Patrons;
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 $patron = Koha::Patrons->find( $member );
30 unless ( $patron ) {
31     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
32     exit;
33 }
34
35 my $category_type = $patron->category->category_type;
36 my $bor = $patron->unblessed;
37 if( $category_type eq 'S' )  {
38     $flagsrequired->{'staffaccess'} = 1;
39 }
40 my ($template, $loggedinuser, $cookie) = get_template_and_user({
41         template_name   => "members/member-flags.tt",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 0,
45         flagsrequired   => $flagsrequired,
46         debug           => 1,
47 });
48
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     $sth->execute($module_flags, $member);
88     
89     # deal with subpermissions
90     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
91     $sth->execute($member); 
92     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
93                         SELECT ?, bit, ?
94                         FROM userflags
95                         WHERE flag = ?");
96     foreach my $module (keys %sub_perms) {
97         next if exists $all_module_perms{$module};
98         foreach my $sub_perm (@{ $sub_perms{$module} }) {
99             $sth->execute($member, $sub_perm, $module);
100         }
101     }
102     
103     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
104 } else {
105
106     my $flags = C4::Members::patronflags( $bor );
107     my $accessflags;
108     my $dbh = C4::Context->dbh();
109     # FIXME This needs to be improved to avoid doing the same query
110     my $sth = $dbh->prepare("select bit,flag from userflags");
111     $sth->execute;
112     while ( my ( $bit, $flag ) = $sth->fetchrow ) {
113         if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
114             $accessflags->{$flag} = 1;
115         }
116     }
117
118     my $all_perms  = get_all_subpermissions();
119     my $user_perms = get_user_subpermissions($bor->{'userid'});
120     $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
121     $sth->execute;
122     my @loop;
123
124     while (my ($bit, $flag) = $sth->fetchrow) {
125         my $checked='';
126         if ($accessflags->{$flag}) {
127             $checked= 1;
128         }
129
130         my %row = ( bit => $bit,
131             flag => $flag,
132             checked => $checked,
133         );
134
135         my @sub_perm_loop = ();
136         my $expand_parent = 0;
137         if ($checked) {
138             if (exists $all_perms->{$flag}) {
139                 $expand_parent = 1;
140                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
141                     push @sub_perm_loop, {
142                         id => "${flag}_$sub_perm",
143                         perm => "$flag:$sub_perm",
144                         code => $sub_perm,
145                         checked => 1
146                     };
147                 }
148             }
149         } else {
150             if (exists $user_perms->{$flag}) {
151                 $expand_parent = 1;
152                 # put selected ones first
153                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
154                     push @sub_perm_loop, {
155                         id => "${flag}_$sub_perm",
156                         perm => "$flag:$sub_perm",
157                         code => $sub_perm,
158                         checked => 1
159                     };
160                 }
161             }
162             # then ones not selected
163             if (exists $all_perms->{$flag}) {
164                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
165                     push @sub_perm_loop, {
166                         id => "${flag}_$sub_perm",
167                         perm => "$flag:$sub_perm",
168                         code => $sub_perm,
169                         checked => 0
170                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
171                 }
172             }
173         }
174         $row{expand} = $expand_parent;
175         if ($#sub_perm_loop > -1) {
176             $row{sub_perm_loop} = \@sub_perm_loop;
177         }
178         push @loop, \%row;
179     }
180
181     if ( $category_type eq 'C') {
182         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
183         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
184         $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
185     }
186
187 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
188     $template->param( picture => 1 ) if $patron->image;
189
190 if (C4::Context->preference('ExtendedPatronAttributes')) {
191     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
192     $template->param(
193         ExtendedPatronAttributes => 1,
194         extendedattributes => $attributes
195     );
196 }
197
198 $template->param(
199     borrowernumber => $bor->{'borrowernumber'},
200     cardnumber     => $bor->{'cardnumber'},
201     surname        => $bor->{'surname'},
202     firstname      => $bor->{'firstname'},
203     othernames     => $bor->{'othernames'},
204     categorycode   => $bor->{'categorycode'},
205     category_type  => $category_type,
206     categoryname   => $bor->{'description'},
207     address        => $bor->{address},
208     address2       => $bor->{'address2'},
209     streettype     => $bor->{streettype},
210     city           => $bor->{'city'},
211     state          => $bor->{'state'},
212     zipcode        => $bor->{'zipcode'},
213     country        => $bor->{'country'},
214     phone          => $bor->{'phone'},
215     phonepro       => $bor->{'phonepro'},
216     mobile         => $bor->{'mobile'},
217     email          => $bor->{'email'},
218     emailpro       => $bor->{'emailpro'},
219     branchcode     => $bor->{'branchcode'},
220     loop           => \@loop,
221     is_child       => ( $category_type eq 'C' ),
222     RoutingSerials => C4::Context->preference('RoutingSerials'),
223     csrf_token =>
224         Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
225 );
226
227     output_html_with_http_headers $input, $cookie, $template->output;
228
229 }