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
17 use C4
::Members
::Attributes
qw(GetBorrowerAttributes);
18 use Koha
::Patron
::Images
;
21 use Koha
::Patron
::Categories
;
23 use Digest
::MD5
qw(md5_base64);
27 my $theme = $input->param('theme') || "default";
29 # only used if allowthemeoverride is set
31 my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user
(
33 template_name
=> "members/member-password.tt",
37 flagsrequired
=> { borrowers
=> 1 },
43 $flagsrequired->{borrowers
} = 1;
45 my $member = $input->param('member');
46 my $cardnumber = $input->param('cardnumber');
47 my $destination = $input->param('destination');
48 my $newpassword = $input->param('newpassword');
49 my $newpassword2 = $input->param('newpassword2');
53 my ($bor) = GetMember
( 'borrowernumber' => $member );
55 if ( ( $member ne $loggedinuser ) && ( $bor->{'category_type'} eq 'S' ) ) {
56 push( @errors, 'NOPERMISSION' )
57 unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
59 # need superlibrarian for koha-conf.xml fakeuser.
62 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
64 my $minpw = C4
::Context
->preference('minPasswordLength');
65 push( @errors, 'SHORTPASSWORD' ) if ( $newpassword && $minpw && ( length($newpassword) < $minpw ) );
67 if ( $newpassword && !scalar(@errors) ) {
69 die "Wrong CSRF token"
70 unless Koha
::Token
->new->check_csrf({
71 id
=> C4
::Context
->userenv->{id
},
72 secret
=> md5_base64
( C4
::Context
->config('pass') ),
73 token
=> scalar $input->param('csrf_token'),
76 my $digest = Koha
::AuthUtils
::hash_password
( scalar $input->param('newpassword') );
77 my $uid = $input->param('newuserid') || $bor->{userid
};
78 my $dbh = C4
::Context
->dbh;
79 if ( Koha
::Patrons
->find( $member )->update_password($uid, $digest) ) {
80 $template->param( newpassword
=> $newpassword );
81 if ( $destination eq 'circ' ) {
82 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
85 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
89 push( @errors, 'BADUSERID' );
93 my $userid = $bor->{'userid'};
95 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
96 my $length = int( rand(2) ) + C4
::Context
->preference("minPasswordLength");
97 my $defaultnewpassword = '';
98 for ( my $i = 0 ; $i < $length ; $i++ ) {
99 $defaultnewpassword .= substr( $chars, int( rand( length($chars) ) ), 1 );
102 $template->param( defaultnewpassword
=> $defaultnewpassword );
105 if ( $bor->{'category_type'} eq 'C') {
106 my $patron_categories = Koha
::Patron
::Categories
->search_limited({ category_type
=> 'A' }, {order_by
=> ['categorycode']});
107 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
108 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
111 $template->param( adultborrower
=> 1 ) if ( $bor->{'category_type'} eq 'A' );
113 my $patron_image = Koha
::Patron
::Images
->find($bor->{borrowernumber
});
114 $template->param( picture
=> 1 ) if $patron_image;
116 if ( C4
::Context
->preference('ExtendedPatronAttributes') ) {
117 my $attributes = GetBorrowerAttributes
( $bor->{'borrowernumber'} );
119 ExtendedPatronAttributes
=> 1,
120 extendedattributes
=> $attributes
125 othernames
=> $bor->{'othernames'},
126 surname
=> $bor->{'surname'},
127 firstname
=> $bor->{'firstname'},
128 borrowernumber
=> $bor->{'borrowernumber'},
129 cardnumber
=> $bor->{'cardnumber'},
130 categorycode
=> $bor->{'categorycode'},
131 category_type
=> $bor->{'category_type'},
132 categoryname
=> $bor->{'description'},
133 address
=> $bor->{address
},
134 address2
=> $bor->{'address2'},
135 streettype
=> $bor->{streettype
},
136 city
=> $bor->{'city'},
137 state => $bor->{'state'},
138 zipcode
=> $bor->{'zipcode'},
139 country
=> $bor->{'country'},
140 phone
=> $bor->{'phone'},
141 phonepro
=> $bor->{'phonepro'},
142 mobile
=> $bor->{'mobile'},
143 email
=> $bor->{'email'},
144 emailpro
=> $bor->{'emailpro'},
145 branchcode
=> $bor->{'branchcode'},
146 userid
=> $bor->{'userid'},
147 destination
=> $destination,
148 is_child
=> ( $bor->{'category_type'} eq 'C' ),
149 activeBorrowerRelationship
=> ( C4
::Context
->preference('borrowerRelationship') ne '' ),
150 minPasswordLength
=> $minpw,
151 RoutingSerials
=> C4
::Context
->preference('RoutingSerials'),
152 csrf_token
=> Koha
::Token
->new->generate_csrf({
153 id
=> C4
::Context
->userenv->{id
},
154 secret
=> md5_base64
( C4
::Context
->config('pass') ),
158 if ( scalar(@errors) ) {
159 $template->param( errormsg
=> 1 );
160 foreach my $error (@errors) {
161 $template->param($error) || $template->param( $error => 1 );
165 output_html_with_http_headers
$input, $cookie, $template->output;