From cb7bbd5aee86e4d347a4e2da54ff5c98ae84ebdf Mon Sep 17 00:00:00 2001
From: Josef Moravec <josef.moravec@gmail.com>
Date: Tue, 18 Sep 2018 20:45:13 +0200
Subject: [PATCH] Add changePassword to Koha ILSDI driver (#1229)

---
 .../src/VuFind/ILS/Driver/KohaILSDI.php       | 43 +++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php b/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
index 268745b5e04..ccdba432e7a 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
@@ -1926,6 +1926,49 @@ class KohaILSDI extends \VuFind\ILS\Driver\AbstractBase implements
         }
     }
 
+    /**
+     * Change Password
+     *
+     * This method changes patron's password
+     *
+     * @param array $detail An associative array with three keys
+     *      patron      - The patron array from patronLogin
+     *      oldPassword - Old password
+     *      newPassword - New password
+     *
+     * @return array  An associative array with keys:
+     *      success - boolean, true if change was made
+     *      status  - string, A status message - subject to translation
+     */
+    public function changePassword($detail)
+    {
+        if (!$this->db) {
+            $this->initDb();
+        }
+        $sql = "UPDATE borrowers SET password = ? WHERE borrowernumber = ?";
+        $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+        $max = mb_strlen($keyspace, '8bit') - 1;
+        $salt = '';
+        for ($i = 0; $i < 16; ++$i) { // 16 is length of salt
+            $salt .= $keyspace[random_int(0, $max)];
+        }
+        $salt = base64_encode($salt);
+        $newPassword_hashed = crypt($detail['newPassword'], '$2a$08$' . $salt);
+        try {
+            $stmt = $this->db->prepare($sql);
+            $result = $stmt->execute(
+                [ $newPassword_hashed, $detail['patron']['id'] ]
+            );
+        } catch (Exception $e) {
+            return [ 'success' => false, 'status' => $e->getMessage() ];
+        }
+        return [
+            'success' => $result,
+            'status' => $result ? 'new_password_success'
+                : 'password_error_not_unique'
+        ];
+    }
+
     /**
      * Convert a database date to a displayable date.
      *
-- 
GitLab