2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-29 18:36:25 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\publics;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Page des users.
|
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
class User extends \descartes\Controller
|
|
|
|
{
|
|
|
|
private $internal_user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction est appelée avant toute les autres :
|
2019-10-30 00:30:39 +01:00
|
|
|
* Elle vérifie que l'utilisateur est bien connecté.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return void;
|
|
|
|
*/
|
|
|
|
public function __construct()
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
|
|
|
$this->internal_user = new \controllers\internals\User($bdd);
|
|
|
|
|
|
|
|
\controllers\internals\Tool::verifyconnect();
|
2019-11-15 06:30:23 +01:00
|
|
|
|
|
|
|
if (!\controllers\internals\Tool::is_admin())
|
|
|
|
{
|
|
|
|
return $this->redirect(\descartes\Router::url('Dashboard', 'show'));
|
|
|
|
}
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne tous les users, sous forme d'un tableau permettant l'administration de ces users.
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
2020-04-07 03:02:33 +02:00
|
|
|
public function list()
|
2019-10-30 00:17:10 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$this->render('user/list');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return users as json
|
|
|
|
*/
|
|
|
|
public function list_json()
|
|
|
|
{
|
|
|
|
$entities = $this->internal_user->list();
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode(['data' => $entities]);
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-03-30 01:52:53 +02:00
|
|
|
/**
|
2020-06-23 21:06:13 +02:00
|
|
|
* Update status of users.
|
2020-03-30 01:52:53 +02:00
|
|
|
*
|
|
|
|
* @param array int $_GET['ids'] : User ids
|
|
|
|
* @param mixed $csrf
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param int $status : 1 -> active, 0 -> suspended
|
2020-03-30 01:52:53 +02:00
|
|
|
*
|
|
|
|
* @return boolean;
|
|
|
|
*/
|
2020-06-23 21:06:13 +02:00
|
|
|
public function update_status($csrf, int $status)
|
2020-03-30 01:52:53 +02:00
|
|
|
{
|
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('User', 'list'));
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:06:13 +02:00
|
|
|
if (0 === $status)
|
2020-03-30 01:52:53 +02:00
|
|
|
{
|
|
|
|
$status = \models\User::STATUS_SUSPENDED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$status = \models\User::STATUS_ACTIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $_GET['ids'] ?? [];
|
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
|
|
|
$this->internal_user->update_status($id, $status);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('User', 'list'));
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction va supprimer une liste de users.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @param array int $_GET['ids'] : Les id des useres à supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param mixed $csrf
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function delete($csrf)
|
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('User', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!\controllers\internals\Tool::is_admin())
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Vous devez être administrateur pour supprimer un utilisateur !');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('User', 'list'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$ids = $_GET['ids'] ?? [];
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
2019-11-14 23:09:56 +01:00
|
|
|
$this->internal_user->delete($id);
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('User', 'list'));
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la page d'ajout d'un user.
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
return $this->render('user/add');
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction insert un nouveau user.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @param $csrf : Le jeton CSRF
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param string $_POST['email'] : L'email de l'utilisateur
|
|
|
|
* @param string $_POST['email_confirm'] : Verif de l'email de l'utilisateur
|
|
|
|
* @param optional string $_POST['password'] : Le mot de passe de l'utilisateur (si vide, généré automatiquement)
|
|
|
|
* @param optional string $_POST['password_confirm'] : Confirmation du mot de passe de l'utilisateur
|
|
|
|
* @param optional boolean $_POST['admin'] : Si vrai, l'utilisateur est admin, si vide non
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
|
|
|
public function create($csrf)
|
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
2019-11-10 17:36:42 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('User', 'add'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$email = $_POST['email'] ?? false;
|
2020-05-19 05:00:35 +02:00
|
|
|
$password = !empty($_POST['password']) ? $_POST['password'] : \controllers\internals\Tool::generate_password(rand(6, 12));
|
2019-10-30 00:17:10 +01:00
|
|
|
$admin = $_POST['admin'] ?? false;
|
2020-03-30 01:52:53 +02:00
|
|
|
$status = 'active';
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$email)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Vous devez au moins fournir une adresse e-mail pour l\'utilisateur.');
|
2019-11-10 17:36:42 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('User', 'add'));
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'L\'adresse e-mail n\'est pas valide.');
|
2019-11-10 17:36:42 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('User', 'add'));
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2020-04-16 03:57:32 +02:00
|
|
|
$id_user = $this->internal_user->create($email, $password, $admin);
|
2020-04-02 19:10:54 +02:00
|
|
|
if (!$id_user)
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce user.');
|
2019-11-10 17:36:42 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('User', 'add'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-30 00:17:10 +01:00
|
|
|
|
2020-04-16 07:50:30 +02:00
|
|
|
$mailer = new \controllers\internals\Mailer();
|
|
|
|
$email_send = $mailer->enqueue($email, EMAIL_CREATE_USER, ['email' => $email, 'password' => $password]);
|
2019-11-10 00:27:42 +01:00
|
|
|
if (!$email_send)
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Impossible d\'envoyer l\'e-mail à l\'utilisateur.');
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-10 00:27:42 +01:00
|
|
|
\FlashMessage\FlashMessage::push('success', 'L\'utilisateur a bien été créé.');
|
2019-11-10 17:36:42 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('User', 'list'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|