2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
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;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Page de connexion.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
class Connect extends \descartes\Controller
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
private $internal_user;
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction est appelée avant toute les autres :.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return void;
|
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function __construct()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$this->internal_user = new \controllers\internals\User($bdd);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la fenetre de connexion.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function login()
|
|
|
|
{
|
2019-11-11 00:22:40 +01:00
|
|
|
if (\controllers\internals\Tool::is_connected())
|
2019-11-09 03:38:20 +01:00
|
|
|
{
|
|
|
|
return $this->redirect(\descartes\Router::url('Dashboard', 'show'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('connect/login');
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction connecte un utilisateur, et le redirige sur la page d'accueil.
|
|
|
|
*
|
|
|
|
* @param string $_POST['mail'] : L'email de l'utilisateur
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $_POST['password'] : Le mot de passe de l'utilisateur
|
|
|
|
*/
|
|
|
|
public function connection()
|
|
|
|
{
|
|
|
|
$email = $_POST['mail'] ?? false;
|
2019-10-29 14:57:13 +01:00
|
|
|
$password = $_POST['password'] ?? false;
|
|
|
|
|
|
|
|
$user = $this->internal_user->check_credentials($email, $password);
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$user)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Email ou mot de passe invalide.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Connect', 'login'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
$_SESSION['connect'] = true;
|
|
|
|
$_SESSION['user'] = $user;
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Dashboard', 'show'));
|
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-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la fenetre de changement de password.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return void;
|
|
|
|
*/
|
|
|
|
public function forget_password()
|
|
|
|
{
|
|
|
|
$this->render('connect/forget-password');
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction envoi un email contenant un lien pour re-générer un password oublié.
|
|
|
|
*
|
|
|
|
* @param string $csrf : jeton csrf
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param string $_POST['email'] : L'email pour lequel on veut envoyer un nouveau password
|
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
public function send_reset_password($csrf)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
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-06 20:34:26 +01:00
|
|
|
|
2019-11-04 18:23:11 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Connect', 'forget_password'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$email = $_POST['email'] ?? false;
|
|
|
|
$user = $this->internal_user->get_by_email($email);
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$email || !$user)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Aucun utilisateur n\'existe pour cette adresse mail.');
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-11-04 18:23:11 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Connect', 'forget_password'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$Tokenista = new \Ingenerator\Tokenista(APP_SECRET);
|
|
|
|
$token = $Tokenista->generate(3600, ['user_id' => $user['id']]);
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
$reset_link = \descartes\Router::url('Connect', 'reset_password', ['user_id' => $user['id'], 'token' => $token]);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
\controllers\internals\Tool::send_email($email, EMAIL_RESET_PASSWORD, ['reset_link' => $reset_link]);
|
|
|
|
|
|
|
|
return $this->render('connect/send-reset-password');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction permet à un utilisateur de re-définir son mot de passe.
|
|
|
|
*
|
|
|
|
* @param int $user_id : L'id du user dont on veut modifier le password
|
|
|
|
* @param string $token : Le token permetttant de vérifier que l'opération est légitime
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param optionnal $_POST['password'] : Le nouveau password à utiliser
|
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
public function reset_password($user_id, $token)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$password = $_POST['password'] ?? false;
|
|
|
|
|
|
|
|
$Tokenista = new \Ingenerator\Tokenista(APP_SECRET);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
if (!$Tokenista->isValid($token, ['user_id' => $user_id]))
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return $this->render('connect/reset-password-invalid');
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$password)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return $this->render('connect/reset-password');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->internal_user->update_password($user_id, $password);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
return $this->render('connect/reset-password-done');
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction déconnecte un utilisateur et le renvoie sur la page d'accueil.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
session_unset();
|
|
|
|
session_destroy();
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-11-04 18:23:11 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Connect', 'login'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
}
|