Bug 21547: (follow-up) Rely on Koha::Patron->set_password checks
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 26 Dec 2018 15:25:54 +0000 (12:25 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 3 Jan 2019 14:29:50 +0000 (14:29 +0000)
This patch makes the controller just call $patron->set_password and use
the exceptions it might raise instead of manually checking the passwor
strength.

No behaviour change should be expected. It also removes some leftovers.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
(cherry picked from commit 3f734900f0a86e5478afc22cb77b493500e998e9)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

opac/opac-passwd.pl

index f183506..60d79f6 100755 (executable)
@@ -24,15 +24,14 @@ use CGI qw ( -utf8 );
 
 use C4::Auth;    # checkauth, getborrowernumber.
 use C4::Context;
-use Digest::MD5 qw(md5_base64);
 use C4::Circulation;
 use C4::Members;
 use C4::Output;
-use Koha::AuthUtils qw(hash_password);
 use Koha::Patrons;
 
+use Try::Tiny;
+
 my $query = new CGI;
-my $dbh   = C4::Context->dbh;
 
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {
@@ -60,17 +59,19 @@ if ( C4::Context->preference("OpacPasswordChange") ) {
                 $template->param( 'Error_messages' => '1' );
                 $template->param( 'passwords_mismatch'   => '1' );
             } else {
-                my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $new_password );
-                unless ( $is_valid ) {
-                    $error = 'password_too_short' if $error eq 'too_short';
-                    $error = 'password_too_weak' if $error eq 'too_weak';
-                    $error = 'password_has_whitespaces' if $error eq 'has_whitespaces';
-                } else {
-                    # Password is valid and match
+                try {
                     $patron->set_password( $new_password );
                     $template->param( 'password_updated' => '1' );
                     $template->param( 'borrowernumber'   => $borrowernumber );
                 }
+                catch {
+                    $error = 'password_too_short'
+                        if $_->isa('Koha::Exceptions::Password::TooShort');
+                    $error = 'password_too_weak'
+                        if $_->isa('Koha::Exceptions::Password::TooWeak');
+                    $error = 'password_has_whitespaces'
+                        if $_->isa('Koha::Exceptions::Password::WhitespaceCharacters');
+                };
             }
         }
         else {