2 #script to set the password, and optionally a userid, for a borrower
5 #converted to using templates 3/16/03 by mwhansen@hmc.edu
18 use C4::Members::Attributes qw(GetBorrowerAttributes);
19 use Koha::Patron::Images;
22 use Koha::Patron::Categories;
24 use Digest::MD5 qw(md5_base64);
28 my $theme = $input->param('theme') || "default";
30 # only used if allowthemeoverride is set
32 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
34 template_name => "members/member-password.tt",
38 flagsrequired => { borrowers => 1 },
44 $flagsrequired->{borrowers} = 1;
46 my $member = $input->param('member');
47 my $cardnumber = $input->param('cardnumber');
48 my $destination = $input->param('destination');
49 my $newpassword = $input->param('newpassword');
50 my $newpassword2 = $input->param('newpassword2');
54 my ($bor) = GetMember( 'borrowernumber' => $member );
56 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
57 push( @errors, 'NOPERMISSION' )
58 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
60 # need superlibrarian for koha-conf.xml fakeuser.
63 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
65 my $minpw = C4::Context->preference('minPasswordLength');
66 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
68 if ( $newpassword && !scalar(@errors) ) {
70 die "Wrong CSRF token"
71 unless Koha::Token->new->check_csrf({
72 id => C4::Context->userenv->{id},
73 secret => md5_base64( C4::Context->config('pass') ),
74 token => scalar $input->param('csrf_token'),
77 my $digest = Koha::AuthUtils::hash_password( $input->param('newpassword') );
78 my $uid = $input->param('newuserid') || $bor->{userid};
79 my $dbh = C4::Context->dbh;
80 if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
81 $template->param( newpassword => $newpassword );
82 if ( $destination eq 'circ' ) {
83 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
86 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
90 push( @errors, 'BADUSERID' );
94 my $userid = $bor->{'userid'};
96 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
97 my $length = int( rand(2) ) + C4::Context->preference("minPasswordLength");
98 my $defaultnewpassword = '';
99 for ( my $i = 0 ; $i < $length ; $i++ ) {
100 $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
103 $template->param( defaultnewpassword => $defaultnewpassword );
106 if ( $bor->{'category_type'} eq 'C') {
107 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
108 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
109 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
112 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
114 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
115 $template->param( picture => 1 ) if $patron_image;
117 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
118 my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
120 ExtendedPatronAttributes => 1,
121 extendedattributes => $attributes
126 othernames => $bor->{'othernames'},
127 surname => $bor->{'surname'},
128 firstname => $bor->{'firstname'},
129 borrowernumber => $bor->{'borrowernumber'},
130 cardnumber => $bor->{'cardnumber'},
131 categorycode => $bor->{'categorycode'},
132 category_type => $bor->{'category_type'},
133 categoryname => $bor->{'description'},
134 address => $bor->{address},
135 address2 => $bor->{'address2'},
136 streettype => $bor->{streettype},
137 city => $bor->{'city'},
138 state => $bor->{'state'},
139 zipcode => $bor->{'zipcode'},
140 country => $bor->{'country'},
141 phone => $bor->{'phone'},
142 phonepro => $bor->{'phonepro'},
143 mobile => $bor->{'mobile'},
144 email => $bor->{'email'},
145 emailpro => $bor->{'emailpro'},
146 branchcode => $bor->{'branchcode'},
147 branchname => GetBranchName( $bor->{'branchcode'} ),
148 userid => $bor->{'userid'},
149 destination => $destination,
150 is_child => ( $bor->{'category_type'} eq 'C' ),
151 activeBorrowerRelationship => ( C4::Context->preference('borrowerRelationship') ne '' ),
152 minPasswordLength => $minpw,
153 RoutingSerials => C4::Context->preference('RoutingSerials'),
154 csrf_token => Koha::Token->new->generate_csrf({
155 id => C4::Context->userenv->{id},
156 secret => md5_base64( C4::Context->config('pass') ),
160 if ( scalar(@errors) ) {
161 $template->param( errormsg => 1 );
162 foreach my $error (@errors) {
163 $template->param($error) || $template->param( $error => 1 );
167 output_html_with_http_headers $input, $cookie, $template->output;