mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-05-12 03:06:26 +02:00
add support status on users
This commit is contained in:
parent
e59631607b
commit
413c058ffc
10 changed files with 156 additions and 8 deletions
controllers/internals
|
@ -66,8 +66,9 @@ namespace controllers\internals;
|
|||
* @param $password : User password
|
||||
* @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
|
||||
*/
|
||||
public function create_update_user(string $email, string $password, bool $admin, ?string $api_key = null)
|
||||
public function create_update_user(string $email, string $password, bool $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE)
|
||||
{
|
||||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
$internal_user = new \controllers\internals\User($bdd);
|
||||
|
@ -76,12 +77,34 @@ 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);
|
||||
$success = $internal_user->update($user['id'], $email, $password, $admin, $api_key, $status);
|
||||
|
||||
exit($success ? 0 : 1);
|
||||
}
|
||||
|
||||
$success = $internal_user->create($email, $password, $admin, $api_key);
|
||||
$success = $internal_user->create($email, $password, $admin, $api_key, $status);
|
||||
exit($success ? 0 : 1);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Update a user status
|
||||
*
|
||||
* @param string $email : User email
|
||||
* @param string $status : User status, default \models\User::STATUS_ACTIVE
|
||||
*/
|
||||
public function update_user_status (string $email, string $status)
|
||||
{
|
||||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
$internal_user = new \controllers\internals\User($bdd);
|
||||
|
||||
$user = $internal_user->get_by_email($email);
|
||||
if (!$user)
|
||||
{
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$success = $internal_user->update_status($user['id'], $status);
|
||||
exit($success ? 0 : 1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,6 +104,20 @@ namespace controllers\internals;
|
|||
{
|
||||
return (bool) $this->model_user->update_email($id, $email);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update user status.
|
||||
*
|
||||
* @param string $id : user id
|
||||
* @param string $status : new status
|
||||
*
|
||||
* @return boolean;
|
||||
*/
|
||||
public function update_status($id, $status)
|
||||
{
|
||||
return (bool) $this->model_user->update($id, ['status' => $status]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user api key.
|
||||
|
@ -170,16 +184,18 @@ namespace controllers\internals;
|
|||
* @param mixed $password
|
||||
* @param mixed $admin
|
||||
* @param mixed $api_key
|
||||
* @param string $status : User status
|
||||
*
|
||||
* @return int : Number of modified user
|
||||
*/
|
||||
public function update($id, $email, $password, $admin, $api_key)
|
||||
public function update($id, $email, $password, $admin, $api_key, $status)
|
||||
{
|
||||
$user = [
|
||||
'email' => $email,
|
||||
'password' => password_hash($password, PASSWORD_DEFAULT),
|
||||
'admin' => $admin,
|
||||
'api_key' => $api_key,
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
return $this->model_user->update($id, $user);
|
||||
|
@ -192,16 +208,18 @@ namespace controllers\internals;
|
|||
* @param mixed $password
|
||||
* @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
|
||||
*
|
||||
* @return mixed bool|int : false on error, id of the new user else
|
||||
*/
|
||||
public function create($email, $password, $admin, ?string $api_key = null)
|
||||
public function create($email, $password, $admin, ?string $api_key = null, string $status = \models\User::STATUS_ACTIVE)
|
||||
{
|
||||
$user = [
|
||||
'email' => $email,
|
||||
'password' => password_hash($password, PASSWORD_DEFAULT),
|
||||
'admin' => $admin,
|
||||
'api_key' => $api_key ?? $this->generate_random_api_key(),
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
$new_user_id = $this->model_user->insert($user);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue