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
|
|
|
/*
|
|
|
|
* This file is part of PHP CS Fixer.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* 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-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.
|
|
|
|
*
|
|
|
|
* @param mixed $page
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
|
|
|
public function list($page = 0)
|
|
|
|
{
|
|
|
|
$page = (int) $page;
|
|
|
|
$users = $this->internal_user->list(25, $page);
|
|
|
|
$this->render('user/list', ['users' => $users]);
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
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-10-30 00:17:10 +01:00
|
|
|
$this->internal_user->delete($id);
|
|
|
|
}
|
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-10-30 00:30:39 +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;
|
|
|
|
$password = $_POST['password'] ?? \controllers\internals\Tool::generate_password(rand(6, 12));
|
|
|
|
$admin = $_POST['admin'] ?? false;
|
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-10-30 00:30:39 +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-10-30 00:30:39 +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:17:10 +01:00
|
|
|
$email_send = \controllers\internals\Tool::send_email($email, EMAIL_CREATE_USER, ['email' => $email, 'password' => $password]);
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$email_send)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Impossible d\'envoyer l\'e-mail à l\'utilisateur, le compte n\'a donc pas été créé.');
|
2019-10-30 00:30:39 +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:17:10 +01:00
|
|
|
$user_id = $this->internal_user->create($email, $password, $admin);
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$user_id)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce user.');
|
2019-10-30 00:30:39 +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
|
|
|
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('success', 'L\'utilisateur a bien été créé.');
|
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-30 00:17:10 +01:00
|
|
|
}
|