Add tools for command line manipulation of users

This commit is contained in:
osaajani 2020-10-22 20:31:01 +02:00
parent cae9674097
commit df7c66f91c
2 changed files with 39 additions and 9 deletions

View File

@ -67,6 +67,21 @@ namespace controllers\internals;
new \daemons\Phone($phone);
}
/**
* Check if a user exists based on email
*
* @param string $email : User email
* @return exit code 1 on false, 0 else
*/
public function user_exists(string $email)
{
$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);
exit($user ? 0 : 1);
}
/**
* Create a user or update an existing user.
*
@ -89,26 +104,27 @@ namespace controllers\internals;
{
$api_key = $api_key ?? $internal_user->generate_random_api_key();
$success = $internal_user->update($user['id'], $email, $password, $admin, $api_key, $status, $encrypt_password);
echo json_encode(['id' => $user['id']]);
exit($success ? 0 : 1);
}
$success = $internal_user->create($email, $password, $admin, $api_key, $status, $encrypt_password);
exit($success ? 0 : 1);
$new_user_id = $internal_user->create($email, $password, $admin, $api_key, $status, $encrypt_password);
echo json_encode(['id' => $new_user_id]);
exit($new_user_id ? 0 : 1);
}
/**
* Update a user status.
*
* @param string $email : User email
* @param string $id : User id
* @param string $status : User status, default \models\User::STATUS_ACTIVE
*/
public function update_user_status(string $email, string $status)
public function update_user_status(string $id, 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);
$user = $internal_user->get($id);
if (!$user)
{
exit(1);
@ -117,4 +133,19 @@ namespace controllers\internals;
$success = $internal_user->update_status($user['id'], $status);
exit($success ? 0 : 1);
}
/**
* Delete a user
*
* @param string $id : User id
*/
public function delete_user(string $id)
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
$internal_user = new \controllers\internals\User($bdd);
$success = $internal_user->delete($id);
exit($success ? 0 : 1);
}
}

View File

@ -67,10 +67,9 @@ namespace models;
}
/**
* Retourne une liste de useres sous forme d'un tableau.
* Delete a user
*
* @param array $ids : un ou plusieurs id d'entrées à supprimer
* @param mixed $id
* @param int $id : Id de l'utilisateur a supprimer
*
* @return int : Le nombre de lignes supprimées
*/