From bf624eb19b729246278df6b2bcfca40754094a47 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Wed, 8 Aug 2018 11:01:50 -0300
Subject: [PATCH] Bug 21178: Add Koha::Patron::set_password method

This patch adds the set_password method. This method implements the
password validation done in Koha::AuthUtils and raises exceptions when
the current password policy is not met.

To test:
- Apply this patch
- Run:
  $ kshell
 k$ prove t/db_dependent/Koha/Patrons.t
=> SUCCESS: Tests pass!
- Sign off! :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 Koha/Exceptions/Password.pm | 72 +++++++++++++++++++++++++++++++++++++
 Koha/Patron.pm              | 58 ++++++++++++++++++++++++++++++
 2 files changed, 130 insertions(+)
 create mode 100644 Koha/Exceptions/Password.pm

diff --git a/Koha/Exceptions/Password.pm b/Koha/Exceptions/Password.pm
new file mode 100644
index 0000000000..30a239bbb9
--- /dev/null
+++ b/Koha/Exceptions/Password.pm
@@ -0,0 +1,72 @@
+package Koha::Exceptions::Password;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Exception::Class (
+
+    'Koha::Exceptions::Password' => {
+        description => 'Something went wrong!',
+    },
+    'Koha::Exceptions::Password::Invalid' => {
+        isa => 'Koha::Exceptions::Password',
+        description => 'Invalid password'
+    },
+    'Koha::Exceptions::Password::TooShort' => {
+        isa => 'Koha::Exceptions::Password',
+        description => 'Password is too short',
+        fields => ['length','min_length']
+    },
+    'Koha::Exceptions::Password::TooWeak' => {
+        isa => 'Koha::Exceptions::Password',
+        description => 'Password is too weak'
+    },
+    'Koha::Exceptions::Password::TrailingWhitespaces' => {
+        isa => 'Koha::Exceptions::Password',
+        description => 'Password contains trailing whitespace(s)'
+    }
+);
+
+=head1 NAME
+
+Koha::Exceptions::Password - Base class for password exceptions
+
+=head1 Exceptions
+
+=head2 Koha::Exceptions::Password
+
+Generic password exception
+
+=head2 Koha::Exceptions::Password::Invalid
+
+The supplied password is invalid.
+
+=head2 Koha::Exceptions::Password::TooShort
+
+Password is too short.
+
+=head2 Koha::Exceptions::Password::TooWeak
+
+Password is too weak.
+
+=head2 Koha::Exceptions::Password::TrailingWhitespaces
+
+Password contains trailing spaces, which is forbidden.
+
+=cut
+
+1;
diff --git a/Koha/Patron.pm b/Koha/Patron.pm
index 9e8b3cfefd..4b20c1da32 100644
--- a/Koha/Patron.pm
+++ b/Koha/Patron.pm
@@ -33,6 +33,7 @@ use Koha::AuthUtils;
 use Koha::Checkouts;
 use Koha::Database;
 use Koha::DateUtils;
+use Koha::Exceptions::Password;
 use Koha::Holds;
 use Koha::Old::Checkouts;
 use Koha::Patron::Categories;
@@ -702,6 +703,63 @@ sub update_password {
     return $digest;
 }
 
+=head3 set_password
+
+    $patron->set_password( $plain_text_password );
+
+Set the patron's password.
+
+=head4 Exceptions
+
+The passed string is validated against the current password enforcement policy.
+Exceptions are thrown if the password is not good enough.
+
+=over 4
+
+=item Koha::Exceptions::Password::TooShort
+
+=item Koha::Exceptions::Password::TrailingWhitespaces
+
+=item Koha::Exceptions::Password::TooWeak
+
+=back
+
+=cut
+
+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 $password_length = length($password);
+    Koha::Exceptions::Password::TooShort->throw(
+        { length => $password_length, min_length => $min_length } )
+        if $password_length < $min_length;
+
+    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 $digest = Koha::AuthUtils::hash_password($password);
+    $self->update(
+        {   password       => $digest,
+            login_attempts => 0,
+        }
+    );
+
+    logaction( "MEMBERS", "CHANGE PASS", $self->borrowernumber, "" )
+        if C4::Context->preference("BorrowersLog");
+
+    return $self;
+}
+
+
 =head3 renew_account
 
 my $new_expiry_date = $patron->renew_account
-- 
2.18.0