76 lines
2.7 KiB
PHP
Raw Normal View History

2020-05-15 12:27:34 +02:00
<?php
require_once $GLOBALS['app_path'].'Framework/Controller.php';
require_once $GLOBALS['app_path'].'Model/ModelUser.php';
class ControllerConnexion extends Controller
{
private $modelUser;
public function __construct()
{
$this->modelUser = new ModelUser();
}
public function index()
{
$this->generateView();
}
public function logOn()
{
if ($this->request->existParameter("login") && $this->request->existParameter("password")) {
$login = $this->request->getParameter("login");
$password = sha1($this->request->getParameter("password"));
if ($this->modelUser->exist($login, $password))
{
$user = $this->modelUser->getUser($login, $password);
$this->request->getSession()->setAttribute("user_id",
$user->getId());
$this->request->getSession()->setAttribute("login",
$user->getLogin());
if($login == "root")
$this->redirect('default','admin');
else
$this->redirect("default");
}
else
$this->generateView(array('msgError' => 'Login ou mot de passe incorrects'),
"index");
}
else
throw new Exception("Action impossible : login ou mot de passe non défini");
}
public function registration()
{
$this->generateView();
}
public function register()
{
if ($this->request->existParameter("login") && $this->request->existParameter("password") && $this->request->existParameter("passwordBis")) {
$login = $this->request->getParameter("login");
$password = sha1($this->request->getParameter("password"));
$passwordBis = sha1($this->request->getParameter("passwordBis"));
if ($password == $passwordBis) {
if(!$this->modelUser->exist($login,$password))
{
$this->modelUser->addUser($login, $password);
$this->redirect("default");
}
else
$this->generateView(array('msgError' => 'Utilisateur déja existant'),
"registration");
}
else
$this->generateView(array('msgError' => 'Les mots de passes doivent être identiques'),
"registration");
}
else
throw new Exception("Action impossible : login ou mot de passe non défini");
}
public function disconnect()
{
$this->request->getSession()->destruct();
$this->redirect("connexion");
}
}