From 3569baf1a117cc6596008892704e0e2cced4657d Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Wed, 8 Apr 2020 23:34:15 +0200 Subject: [PATCH] Update user creation to allow using already encrypted password --- controllers/internals/Console.php | 9 ++++++--- controllers/internals/User.php | 10 ++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/controllers/internals/Console.php b/controllers/internals/Console.php index 782aaa9..19e0bb3 100644 --- a/controllers/internals/Console.php +++ b/controllers/internals/Console.php @@ -67,8 +67,11 @@ namespace controllers\internals; * @param $admin : Is user admin * @param $api_key : User API key, if null random api key is generated * @param $status : User status, default \models\User::STATUS_ACTIVE + * @param bool $encrypt_password : Should the password be encrypted, by default true + * + * @return exit code 0 on success | 1 on error */ - public function create_update_user(string $email, string $password, bool $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE) + public function create_update_user(string $email, string $password, bool $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE, bool $encrypt_password = true) { $bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8'); $internal_user = new \controllers\internals\User($bdd); @@ -77,12 +80,12 @@ namespace controllers\internals; if ($user) { $api_key = $api_key ?? $internal_user->generate_random_api_key(); - $success = $internal_user->update($user['id'], $email, $password, $admin, $api_key, $status); + $success = $internal_user->update($user['id'], $email, $password, $admin, $api_key, $status, $encrypt_password); exit($success ? 0 : 1); } - $success = $internal_user->create($email, $password, $admin, $api_key, $status); + $success = $internal_user->create($email, $password, $admin, $api_key, $status, $encrypt_password); exit($success ? 0 : 1); } diff --git a/controllers/internals/User.php b/controllers/internals/User.php index 13dbc91..4829264 100644 --- a/controllers/internals/User.php +++ b/controllers/internals/User.php @@ -187,14 +187,15 @@ namespace controllers\internals; * @param mixed $admin * @param mixed $api_key * @param string $status : User status + * @param bool $encrypt_password : Should the password be encrypted, by default true * * @return int : Number of modified user */ - public function update($id, $email, $password, $admin, $api_key, $status) + public function update($id, $email, $password, $admin, $api_key, $status, bool $encrypt_password = true) { $user = [ 'email' => $email, - 'password' => password_hash($password, PASSWORD_DEFAULT), + 'password' => $encrypt_password ? password_hash($password, PASSWORD_DEFAULT) : $password, 'admin' => $admin, 'api_key' => $api_key, 'status' => $status, @@ -211,14 +212,15 @@ namespace controllers\internals; * @param mixed $admin * @param ?string $api_key : The api key of the user, if null generate randomly * @param string $status : User status, default \models\User::STATUS_ACTIVE + * @param bool $encrypt_password : Should the password be encrypted, by default true * * @return mixed bool|int : false on error, id of the new user else */ - public function create($email, $password, $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE) + public function create($email, $password, $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE, bool $encrypt_password = true) { $user = [ 'email' => $email, - 'password' => password_hash($password, PASSWORD_DEFAULT), + 'password' => $encrypt_password ? password_hash($password, PASSWORD_DEFAULT) : $password, 'admin' => $admin, 'api_key' => $api_key ?? $this->generate_random_api_key(), 'status' => $status,