2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
|
|
|
namespace controllers\internals;
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
class User extends \descartes\InternalController
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
private $model_user;
|
2019-10-30 00:17:10 +01:00
|
|
|
private $internal_event;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
public function __construct(\PDO $bdd)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$this->model_user = new \models\User($bdd);
|
|
|
|
$this->internal_event = new \controllers\internals\Event($bdd);
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-29 14:57:13 +01:00
|
|
|
* Return list of users as an array
|
|
|
|
* @param mixed(int|bool) $nb_entry : Number of entry to return
|
|
|
|
* @param mixed(int|bool) $page : Numero of page
|
|
|
|
* @return array|bool : List of user or false
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function list(?int $nb_entry = null, ?int $page = null)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
return $this->model_user->list($nb_entry, $page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-29 18:36:25 +01:00
|
|
|
* Cette fonction va supprimer une liste de users
|
|
|
|
* @param array $ids : Les id des useres à supprimer
|
|
|
|
* @return int : Le nombre de useres supprimées;
|
|
|
|
*/
|
|
|
|
public function delete($id)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
return $this->model_user->remove($id);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-29 14:57:13 +01:00
|
|
|
* Cette fonction vérifie s'il existe un utilisateur qui corresponde à ce couple login/password
|
|
|
|
* @param string $email : L'eamil de l'utilisateur
|
|
|
|
* @param string $password : Le mot de passe de l'utilisateur
|
|
|
|
* @return mixed false | array : False si pas de user, le user correspondant sous forme d'array sinon
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function check_credentials($email, $password)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$user = $this->model_user->get_by_email($email);
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!$user) {
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!password_verify($password, $user['password'])) {
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-29 14:57:13 +01:00
|
|
|
* Update a user password
|
|
|
|
* @param string $id : User id
|
|
|
|
* @param string $password : New password
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return bool;
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function update_password(int $id, string $password) : bool
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
return (bool) $this->model_user->update_password($id, $password);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a user transfer property value
|
|
|
|
* @param string $id : User id
|
|
|
|
* @param string $transfer : New value of property transfer
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return boolean;
|
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function update_transfer(int $id, int $transfer) : bool
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return (bool) $this->model_user->update_transfer($id, $transfer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update user email
|
|
|
|
* @param string $id : user id
|
|
|
|
* @param string $email : new mail
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function update_email($id, $email)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return (bool) $this->model_user->update_email($id, $email);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction retourne un utilisateur pour un mail donné
|
|
|
|
* @param string $email : L'email de l'utilisateur
|
|
|
|
* @return mixed boolean | array : false si pas de user pour le mail, le user sinon
|
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
public function get_by_email($email)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->model_user->get_by_email($email);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-29 14:57:13 +01:00
|
|
|
* Cette fonction met à jour une série de users
|
|
|
|
* @return int : le nombre de ligne modifiées
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function update($id, $email, $password, $admin, $transfer)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$user = [
|
|
|
|
'email' => $email,
|
|
|
|
'password' => password_hash($password, PASSWORD_DEFAULT),
|
|
|
|
'admin' => $admin,
|
|
|
|
'transfer' => $transfer,
|
|
|
|
];
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->model_user->update($id, $user);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction insert une nouvelle usere
|
|
|
|
* @param array $user : Un tableau représentant la usere à insérer
|
|
|
|
* @return mixed bool|int : false si echec, sinon l'id de la nouvelle usere insérée
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function create($email, $password, $admin, $transfer = false)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$user = [
|
|
|
|
'email' => $email,
|
|
|
|
'password' => password_hash($password, PASSWORD_DEFAULT),
|
|
|
|
'admin' => $admin,
|
|
|
|
'transfer' => $transfer,
|
|
|
|
];
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$result = $this->model_user->insert($user);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!$result) {
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$this->internal_event->create('CONTACT_ADD', 'Ajout de l\'utilisateur : ' . $email . '.');
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
return $result;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
}
|