Permet à l'utilisateur de se connecter

This commit is contained in:
2022-11-22 09:30:43 +01:00
parent 53038a5022
commit b4f9d31c2e
11 changed files with 265 additions and 17 deletions

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Silex\Security;
use Silex\Gateway\UserGateway;
use Silex\Model\User;
const USER = 'USER';
class Security
{
private array $session;
private UserGateway $userGateway;
private ?User $user = null;
public function __construct(UserGateway $userGateway, array $session)
{
$this->userGateway = $userGateway;
$this->session = $session;
}
public function initLogin(string $login, string $rawPassword): bool
{
$user = $this->userGateway->getByLogin($login);
if ($user === null || !password_verify($rawPassword, $user->getPasswordHash())) {
return false;
}
$this->session[USER] = $user->getId();
$this->user = $user;
return true;
}
public function logout()
{
$this->user = null;
unset($this->session[USER]);
}
public function getCurrentUser(): ?User
{
if (!empty($this->session[USER]) && $this->user === null) {
$this->user = $this->userGateway->getById($this->session[USER]);
}
return $this->user;
}
}