Add api key to user, add status update support, add other things i dont remember at 2am...

This commit is contained in:
osaajani 2020-01-08 02:14:38 +01:00
parent 193dd00c1e
commit fb6abb4d91
14 changed files with 323 additions and 8 deletions

View file

@ -100,6 +100,22 @@ namespace controllers\internals;
return (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
}
/**
* Update a sended status for a sended
* @param int $id_sended : Sended id
* @param string $status : Status of a the sms (unknown, delivered, failed)
* @return bool : false on error, true on success
*/
public function update_status (int $id_sended, string $status) : bool
{
$sended = [
'status' => $status,
];
return (bool) $this->get_model()->update($id_sended, $sended);
}
/**
@ -124,6 +140,18 @@ namespace controllers\internals;
{
return $this->get_model()->gets_by_destination_and_user($id_user, $origin);
}
/**
* Return sended for an uid and an adapter
* @param string $uid : Uid of the sended
* @param string $adapter : Adapter used to send the message
* @return array
*/
public function get_by_uid_and_adapter(string $uid, string $adapter)
{
return $this->get_model()->get_by_uid_and_adapter($uid, $adapter);
}
/**

View file

@ -117,6 +117,28 @@ namespace controllers\internals;
{
return (bool) $this->model_user->update_email($id, $email);
}
/**
* Update user api key.
*
* @param string $id : user id
* @param ?string $api_key : new api key
*
* @return mixed : false on error, else new api key;
*/
public function update_api_key($id, ?string $api_key = null)
{
$api_key = $api_key ?? $this->generate_random_api_key();
$success = $this->model_user->update($id, ['api_key' => $api_key]);
if (!$success)
{
return false;
}
return $api_key;
}
/**
* Get a user by his email address
@ -128,6 +150,18 @@ namespace controllers\internals;
{
return $this->model_user->get_by_email($email);
}
/**
* Get a user by his api_key address
* @param string $api_key : User api key
*
* @return mixed boolean | array : false if cannot find user for this api key, the user else
*/
public function get_by_api_key(string $api_key)
{
return $this->model_user->get_by_api_key($api_key);
}
/**
* Return users by transfer status.
@ -168,16 +202,18 @@ namespace controllers\internals;
* @param mixed $password
* @param mixed $admin
* @param mixed $transfer
* @param ?string $api_key : The api key of the user, if null generate randomly
*
* @return mixed bool|int : false on error, id of the new user else
*/
public function create($email, $password, $admin, $transfer = false)
public function create($email, $password, $admin, $transfer = false, ?string $api_key = null)
{
$user = [
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT),
'admin' => $admin,
'transfer' => $transfer,
'api_key' => $api_key ?? $this->generate_random_api_key(),
];
$new_user_id = $this->model_user->insert($user);
@ -197,4 +233,14 @@ namespace controllers\internals;
return $new_user_id;
}
/**
* Generate a random api key
* @return string : The api key
*/
public function generate_random_api_key () : string
{
return bin2hex(random_bytes(16));
}
}