From c0064e00f23e6c5e706291a14a48fe2412bcb506 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 9 Aug 2018 13:20:20 -0300 Subject: [PATCH] Bug 21178: (QA follow-up) Rely on is_password_valid This patch makes set_password still rely on Koha::AuthUtils' is_password_valid method as the only source for truth regarding password validity. To test: - Run: $ kshell k$ prove t/db_dependent/Koha/Patrons.t => SUCCESS: Tests still pass! Signed-off-by: Tomas Cohen Arazi --- Koha/Patron.pm | 29 ++++++++++++++++------------- t/db_dependent/Koha/Patrons.t | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index b0568d154e..1984b500ea 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -729,21 +729,24 @@ Exceptions are thrown if the password is not good enough. sub set_password { my ( $self, $password ) = @_; - my $min_length = C4::Context->preference('minPasswordLength'); - $min_length = 3 if not $min_length or $min_length < 3; + my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password ); - my $password_length = length($password); - Koha::Exceptions::Password::TooShort->throw( - { length => $password_length, min_length => $min_length } ) - if $password_length < $min_length; + if ( !$is_valid ) { + if ( $error eq 'too_short' ) { + my $min_length = C4::Context->preference('minPasswordLength'); + $min_length = 3 if not $min_length or $min_length < 3; - Koha::Exceptions::Password::TrailingWhitespaces->throw( - "Password contains trailing spaces, which is forbidden.") - if $password =~ m[^\s|\s$]; - - if ( C4::Context->preference('RequireStrongPassword') ) { - Koha::Exceptions::Password::TooWeak->throw() - if $password !~ m|(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{$min_length,}|; + my $password_length = length($password); + Koha::Exceptions::Password::TooShort->throw( + { length => $password_length, min_length => $min_length } ); + } + elsif ( $error eq 'has_whitespaces' ) { + Koha::Exceptions::Password::TrailingWhitespaces->throw( + "Password contains trailing spaces, which is forbidden."); + } + elsif ( $error eq 'too_weak' ) { + Koha::Exceptions::Password::TooWeak->throw(); + } } my $digest = Koha::AuthUtils::hash_password($password); diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 7be2b8fcdb..f8c8a8046e 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -1529,7 +1529,7 @@ subtest '->set_password' => sub { 'minPasswordLength is 5, fail test'; # Trailing spaces tests - throws_ok { $patron->set_password('abcd '); } + throws_ok { $patron->set_password('abcD12d '); } 'Koha::Exceptions::Password::TrailingWhitespaces', 'Password contains trailing spaces, exception is thrown'; -- 2.18.0