Cli user
This commit is contained in:
@ -15,7 +15,10 @@ use function setcookie;
|
||||
|
||||
class Login
|
||||
{
|
||||
public function __construct(protected Repository\Login $repository, protected string $cookie_name, protected int $max_login_time, protected string $domain = '', protected string $path = '', protected string $cookie_separator = ':')
|
||||
public function __construct(protected Repository\Login $repository, protected Repository\User $userRepository,
|
||||
protected string $cookie_name,
|
||||
protected int $max_login_time, protected string $domain = '',
|
||||
protected string $path = '', protected string $cookie_separator = ':')
|
||||
{
|
||||
$this->loadCookie();
|
||||
}
|
||||
@ -23,19 +26,18 @@ class Login
|
||||
protected string $selector = '';
|
||||
protected string $token = '';
|
||||
|
||||
public function isIn(): bool
|
||||
public function isIn(?string $selector = null, ?string $sentToken = null): bool
|
||||
{
|
||||
try {
|
||||
$login = $this->repository->fetchActiveBySelector($this->selector);
|
||||
if (!$this->validToken($login)) {
|
||||
$login = $this->repository->fetchActiveBySelector($selector ?? $this->selector);
|
||||
if (!$this->validToken($login, $sentToken)) {
|
||||
return false;
|
||||
}
|
||||
$now = new DateTimeImmutable();
|
||||
if ($login->dateTime->add(new DateInterval("PT{$this->max_login_time}H")) > $now) {
|
||||
return true;
|
||||
}
|
||||
} catch (PDOException|EmptyResult) {
|
||||
}
|
||||
} catch (PDOException|EmptyResult) {}
|
||||
return false;
|
||||
}
|
||||
public function getUser(): Model\User
|
||||
@ -54,6 +56,23 @@ class Login
|
||||
{
|
||||
return $this->cookie_separator;
|
||||
}
|
||||
|
||||
public function addUser(array $data): Model\User
|
||||
{
|
||||
try {
|
||||
return $this->userRepository->fetchByName($data['name']);
|
||||
} catch (EmptyResult) {
|
||||
list($passphrase, $encrypted) = $this->splitPassword($data['password']);
|
||||
$password = $this->cryptoJs_aes_decrypt($encrypted, $passphrase);
|
||||
$password = password_hash($password, PASSWORD_DEFAULT);
|
||||
$user = $this->userRepository->create([
|
||||
'name' => $data['name'],
|
||||
'password' => $password,
|
||||
'enabled' => $data['enabled'] ?? 1
|
||||
]);
|
||||
return $this->userRepository->save($user);
|
||||
}
|
||||
}
|
||||
public function validateUser(Model\User $user, string $encryptedPassword): bool
|
||||
{
|
||||
list($passphrase, $encrypted) = $this->splitPassword($encryptedPassword);
|
||||
@ -106,6 +125,10 @@ class Login
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function parseToken(Model\Login $login): string
|
||||
{
|
||||
return implode($this->cookie_separator, [$login->selector, $login->token]);
|
||||
}
|
||||
|
||||
protected function loadCookie(): void
|
||||
{
|
||||
@ -145,8 +168,11 @@ class Login
|
||||
);
|
||||
}
|
||||
|
||||
protected function validToken(Model\Login $login): bool
|
||||
protected function validToken(Model\Login $login, ?string $sentToken = null): bool
|
||||
{
|
||||
if ($sentToken !== null) {
|
||||
return password_verify($sentToken, $login->token);
|
||||
}
|
||||
return password_verify($this->token, $login->token);
|
||||
}
|
||||
protected function generateToken(Model\Login $login): array
|
||||
|
Reference in New Issue
Block a user