Update user creation to allow using already encrypted password

This commit is contained in:
osaajani 2020-04-08 23:34:15 +02:00
parent 6f1c726284
commit 3569baf1a1
2 changed files with 12 additions and 7 deletions

View File

@ -67,8 +67,11 @@ namespace controllers\internals;
* @param $admin : Is user admin * @param $admin : Is user admin
* @param $api_key : User API key, if null random api key is generated * @param $api_key : User API key, if null random api key is generated
* @param $status : User status, default \models\User::STATUS_ACTIVE * @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'); $bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
$internal_user = new \controllers\internals\User($bdd); $internal_user = new \controllers\internals\User($bdd);
@ -77,12 +80,12 @@ namespace controllers\internals;
if ($user) if ($user)
{ {
$api_key = $api_key ?? $internal_user->generate_random_api_key(); $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); 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); exit($success ? 0 : 1);
} }

View File

@ -187,14 +187,15 @@ namespace controllers\internals;
* @param mixed $admin * @param mixed $admin
* @param mixed $api_key * @param mixed $api_key
* @param string $status : User status * @param string $status : User status
* @param bool $encrypt_password : Should the password be encrypted, by default true
* *
* @return int : Number of modified user * @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 = [ $user = [
'email' => $email, 'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT), 'password' => $encrypt_password ? password_hash($password, PASSWORD_DEFAULT) : $password,
'admin' => $admin, 'admin' => $admin,
'api_key' => $api_key, 'api_key' => $api_key,
'status' => $status, 'status' => $status,
@ -211,14 +212,15 @@ namespace controllers\internals;
* @param mixed $admin * @param mixed $admin
* @param ?string $api_key : The api key of the user, if null generate randomly * @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 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 * @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 = [ $user = [
'email' => $email, 'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT), 'password' => $encrypt_password ? password_hash($password, PASSWORD_DEFAULT) : $password,
'admin' => $admin, 'admin' => $admin,
'api_key' => $api_key ?? $this->generate_random_api_key(), 'api_key' => $api_key ?? $this->generate_random_api_key(),
'status' => $status, 'status' => $status,