Permet à l'utilisateur de se connecter
This commit is contained in:
48
src/Silex/Security/Security.php
Normal file
48
src/Silex/Security/Security.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user