From b722a8cec5d6f2c840f0e20fae2848be0e601dea Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 26 Dec 2018 12:37:07 -0300 Subject: [PATCH] Bug 22047: Add 'skip_validation' param to Koha::Patron->set_password This patch makes Koha::Patron->set_password expect a hashref as param and adds support for an 'skip_validation' param to be passed. Its purpose is to make the method skip the relevant password strength checks if required. It targets the Auth_with_ldap.pm usage when the 'update_password' flag is set in the configuration. The tests on this bug cover this use case so, to test: - Apply the tests patch - Run: $ kshell k$ prove t/db_dependent/Koha/Patrons.t => FAIL: Tests fail, code doesn't work as expected - Apply this patch - Run: k$ prove t/db_dependent/Koha/Patrons.t => SUCCESS: Tests pass! Yay! - Sign off :-D --- Koha/Patron.pm | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 5831034bdc..b10b116e0f 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -670,13 +670,15 @@ sub update_password { =head3 set_password - $patron->set_password( $plain_text_password ); + $patron->set_password({ password => $plain_text_password [, skip_validation => 1 ] }); Set the patron's password. =head4 Exceptions The passed string is validated against the current password enforcement policy. +Validation can be skipped by passing the I parameter. + Exceptions are thrown if the password is not good enough. =over 4 @@ -692,24 +694,28 @@ Exceptions are thrown if the password is not good enough. =cut sub set_password { - my ( $self, $password ) = @_; + my ( $self, $args ) = @_; - my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password ); + my $password = $args->{password}; - 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; + unless ( $args->{skip_validation} ) { + 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 } ); - } - elsif ( $error eq 'has_whitespaces' ) { - Koha::Exceptions::Password::WhitespaceCharacters->throw(); - } - elsif ( $error eq 'too_weak' ) { - Koha::Exceptions::Password::TooWeak->throw(); + 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; + + 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::WhitespaceCharacters->throw(); + } + elsif ( $error eq 'too_weak' ) { + Koha::Exceptions::Password::TooWeak->throw(); + } } } -- 2.20.1