Bug 22048: Use set_password in member-password.pl
[koha.git] / members / member-password.pl
index e886c41..34795c3 100755 (executable)
@@ -20,6 +20,8 @@ use Koha::Token;
 use Koha::Patrons;
 use Koha::Patron::Categories;
 
+use Try::Tiny;
+
 my $input = new CGI;
 
 my $theme = $input->param('theme') || "default";
@@ -37,22 +39,21 @@ my ( $template, $loggedinuser, $cookie, $staffflags ) = get_template_and_user(
     }
 );
 
-my $member      = $input->param('member');
-my $cardnumber  = $input->param('cardnumber');
-my $destination = $input->param('destination');
+my $patron_id    = $input->param('member');
+my $destination  = $input->param('destination');
 my $newpassword  = $input->param('newpassword');
 my $newpassword2 = $input->param('newpassword2');
+my $new_user_id  = $input->param('newuserid');
 
 my @errors;
 
 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
-my $patron = Koha::Patrons->find( $member );
+my $patron = Koha::Patrons->find( $patron_id );
 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
 
 my $category_type = $patron->category->category_type;
-my $bor = $patron->unblessed;
 
-if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
+if ( ( $patron_id ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
     push( @errors, 'NOPERMISSION' )
       unless ( $staffflags->{'superlibrarian'} || $staffflags->{'staffaccess'} );
 
@@ -61,15 +62,6 @@ if ( ( $member ne $loggedinuser ) && ( $category_type eq 'S' ) ) {
 
 push( @errors, 'NOMATCH' ) if ( ( $newpassword && $newpassword2 ) && ( $newpassword ne $newpassword2 ) );
 
-if ( $newpassword and not @errors ) {
-    my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $newpassword );
-    unless ( $is_valid ) {
-        push @errors, 'ERROR_password_too_short' if $error eq 'too_short';
-        push @errors, 'ERROR_password_too_weak' if $error eq 'too_weak';
-        push @errors, 'ERROR_password_has_whitespaces' if $error eq 'has_whitespaces';
-    }
-}
-
 if ( $newpassword and not @errors) {
 
     die "Wrong CSRF token"
@@ -78,25 +70,36 @@ if ( $newpassword and not @errors) {
             token  => scalar $input->param('csrf_token'),
         });
 
-    my $uid    = $input->param('newuserid') || $bor->{userid};
-    my $password = $input->param('newpassword');
-    my $dbh    = C4::Context->dbh;
-    if ( Koha::Patrons->find( $member )->update_password($uid, $password) ) {
+    try {
+        $patron->set_password({ password => $newpassword });
+        $patron->userid($new_user_id)->store
+            if $new_user_id and $new_user_id ne $patron->userid;
         $template->param( newpassword => $newpassword );
         if ( $destination eq 'circ' ) {
-            print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
+            print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=" . $patron->cardnumber);
         }
         else {
-            print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
+            print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$patron_id");
         }
     }
-    else {
-        push( @errors, 'BADUSERID' );
-    }
+    catch {
+        if ( $_->isa('Koha::Exceptions::Password::TooShort') ) {
+            push @errors, 'ERROR_password_too_short';
+        }
+        elsif ( $_->isa('Koha::Exceptions::Password::WhitespaceCharacters') ) {
+            push @errors, 'ERROR_password_has_whitespaces';
+        }
+        elsif ( $_->isa('Koha::Exceptions::Password::TooWeak') ) {
+            push @errors, 'ERROR_password_too_weak';
+        }
+        else {
+            push( @errors, 'BADUSERID' );
+        }
+    };
 }
 
 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
-    my $attributes = GetBorrowerAttributes( $bor->{'borrowernumber'} );
+    my $attributes = GetBorrowerAttributes( $patron_id );
     $template->param(
         ExtendedPatronAttributes => 1,
         extendedattributes       => $attributes
@@ -104,9 +107,9 @@ if ( C4::Context->preference('ExtendedPatronAttributes') ) {
 }
 
 $template->param(
-    patron                     => $patron,
-    destination                => $destination,
-    csrf_token                 => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
+    patron      => $patron,
+    destination => $destination,
+    csrf_token  => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID'), }),
 );
 
 if ( scalar(@errors) ) {