You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
50 lines
1.3 KiB
<?php
|
|
|
|
class Security
|
|
{
|
|
const tokenSession = 'tokenSession';
|
|
private array $session;
|
|
private UserRepository $userGateway;
|
|
private ?User $user = null;
|
|
|
|
public function __construct(UserRepository $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[tokenSession] = $user->getId();
|
|
$this->user = $user;
|
|
return true;
|
|
}
|
|
|
|
public function logout(): bool
|
|
{
|
|
|
|
if(session_unset()){
|
|
return true;
|
|
}
|
|
$this->user = null;
|
|
session_unset();
|
|
session_destroy();
|
|
$_SESSION['role'] = "";
|
|
$_SESSION=array();
|
|
unset($this->session[tokenSession]);
|
|
return true;
|
|
}
|
|
|
|
public function getCurrentUser(): ?User
|
|
{
|
|
if (!empty($this->session[tokenSession]) && $this->user === null) {
|
|
$this->user = $this->userGateway->getById($this->session[tokenSession]);
|
|
}
|
|
return $this->user;
|
|
}
|
|
}
|