Bug 18403: Update permissions - borrowers => 1|* becomes borrowers => 'edit_borrowers'
[koha.git] / members / member-password.pl
1 #!/usr/bin/perl
2 #script to set the password, and optionally a userid, for a borrower
3 #written 2/5/00
4 #by chris@katipo.co.nz
5 #converted to using templates 3/16/03 by mwhansen@hmc.edu
6
7 use Modern::Perl;
8
9 use C4::Auth;
10 use Koha::AuthUtils;
11 use C4::Output;
12 use C4::Context;
13 use C4::Members;
14 use C4::Circulation;
15 use CGI qw ( -utf8 );
16 use C4::Members::Attributes qw(GetBorrowerAttributes);
17 use Koha::AuthUtils;
18 use Koha::Token;
19
20 use Koha::Patrons;
21 use Koha::Patron::Categories;
22
23 my $input = new CGI;
24
25 my $theme = $input->param('theme') || "default";
26
27 # only used if allowthemeoverride is set
28
29 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
30     {
31         template_name   => "members/member-password.tt",
32         query           => $input,
33         type            => "intranet",
34         authnotrequired => 0,
35         flagsrequired   => { borrowers => 'edit_borrowers' },
36         debug           => 1,
37     }
38 );
39
40 my $member      = $input->param('member');
41 my $cardnumber  = $input->param('cardnumber');
42 my $destination = $input->param('destination');
43 my $newpassword  = $input->param('newpassword');
44 my $newpassword2 = $input->param('newpassword2');
45
46 my @errors;
47
48 my $patron = Koha::Patrons->find( $member );
49 unless ( $patron ) {
50     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
51     exit;
52 }
53
54 my $category_type = $patron->category->category_type;
55 my $bor = $patron->unblessed;
56
57 if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
58     push( @errors, 'NOPERMISSION' )
59       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
60
61     # need superlibrarian for koha-conf.xml fakeuser.
62 }
63
64 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
65
66 if ( $newpassword and not @errors ) {
67     my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $newpassword );
68     unless ( $is_valid ) {
69         push @errors, 'ERROR_password_too_short' if $error eq 'too_short';
70         push @errors, 'ERROR_password_too_weak' if $error eq 'too_weak';
71         push @errors, 'ERROR_password_has_whitespaces' if $error eq 'has_whitespaces';
72     }
73 }
74
75 if ( $newpassword and not @errors) {
76
77     die "Wrong CSRF token"
78         unless Koha::Token->new->check_csrf({
79             session_id => scalar $input->cookie('CGISESSID'),
80             token  => scalar $input->param('csrf_token'),
81         });
82
83     my $digest = Koha::AuthUtils::hash_password( scalar $input->param('newpassword') );
84     my $uid    = $input->param('newuserid') || $bor->{userid};
85     my $dbh    = C4::Context->dbh;
86     if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
87         $template->param( newpassword => $newpassword );
88         if ( $destination eq 'circ' ) {
89             print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
90         }
91         else {
92             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
93         }
94     }
95     else {
96         push( @errors, 'BADUSERID' );
97     }
98 }
99
100 if ( $category_type eq 'C') {
101     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
102     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
103     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
104 }
105
106 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
107
108 $template->param( picture => 1 ) if $patron->image;
109
110 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
111     my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
112     $template->param(
113         ExtendedPatronAttributes => 1,
114         extendedattributes       => $attributes
115     );
116 }
117
118 $template->param(
119     othernames                 => $bor->{'othernames'},
120     surname                    => $bor->{'surname'},
121     firstname                  => $bor->{'firstname'},
122     borrowernumber             => $bor->{'borrowernumber'},
123     cardnumber                 => $bor->{'cardnumber'},
124     categorycode               => $bor->{'categorycode'},
125     category_type              => $category_type,
126     categoryname               => $bor->{'description'},
127     address                    => $bor->{address},
128     address2                   => $bor->{'address2'},
129     streettype                 => $bor->{streettype},
130     city                       => $bor->{'city'},
131     state                      => $bor->{'state'},
132     zipcode                    => $bor->{'zipcode'},
133     country                    => $bor->{'country'},
134     phone                      => $bor->{'phone'},
135     phonepro                   => $bor->{'phonepro'},
136     streetnumber               => $bor->{'streetnumber'},
137     mobile                     => $bor->{'mobile'},
138     email                      => $bor->{'email'},
139     emailpro                   => $bor->{'emailpro'},
140     branchcode                 => $bor->{'branchcode'},
141     userid                     => $bor->{'userid'},
142     destination                => $destination,
143     is_child                   => ( $category_type eq 'C' ),
144     csrf_token                 => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
145 );
146
147 if ( scalar(@errors) ) {
148     $template->param( errormsg => 1 );
149     foreach my $error (@errors) {
150         $template->param($error) || $template->param( $error => 1 );
151     }
152 }
153
154 output_html_with_http_headers $input, $cookie, $template->output;