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
|
||||
|
@ -4,12 +4,14 @@ namespace Incoviba\Service;
|
||||
use DateTimeInterface;
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class UF
|
||||
{
|
||||
protected string $redisKey = 'uf';
|
||||
|
||||
public function __construct(protected Redis $redisService, protected Money $moneyService) {}
|
||||
public function __construct(protected Redis $redisService, protected Money $moneyService,
|
||||
protected LoggerInterface $logger) {}
|
||||
|
||||
public function get(?DateTimeInterface $date = null): float
|
||||
{
|
||||
@ -25,12 +27,36 @@ class UF
|
||||
$uf = $ufs[$date->format('Y-m-d')];
|
||||
} catch (EmptyRedis) {
|
||||
$uf = $this->moneyService->getUF($date);
|
||||
if ($uf === 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
$ufs[$date->format('Y-m-d')] = $uf;
|
||||
ksort($ufs);
|
||||
$this->redisService->set($this->redisKey, json_encode($ufs), 60 * 60 * 24 * 30);
|
||||
}
|
||||
return $uf;
|
||||
}
|
||||
public function updateMany(array $dates): array
|
||||
{
|
||||
$ufs = [];
|
||||
try {
|
||||
$ufs = json_decode($this->redisService->get($this->redisKey), JSON_OBJECT_AS_ARRAY);
|
||||
} catch (EmptyRedis) {}
|
||||
$updated = [];
|
||||
foreach ($dates as $date) {
|
||||
if (!isset($ufs[$date->format('Y-m-d')]) or $ufs[$date->format('Y-m-d')] === 0) {
|
||||
$uf = $this->moneyService->getUF($date);
|
||||
if ($uf === 0.0) {
|
||||
continue;
|
||||
}
|
||||
$updated[$date->format('Y-m-d')] = $uf;
|
||||
$ufs[$date->format('Y-m-d')] = $this->moneyService->getUF($date);
|
||||
}
|
||||
}
|
||||
ksort($ufs);
|
||||
$this->redisService->set($this->redisKey, json_encode($ufs), 60 * 60 * 24 * 30);
|
||||
return $updated;
|
||||
}
|
||||
public function transform(DateTimeInterface $date, float $input, string $from = 'uf'): float
|
||||
{
|
||||
|
||||
|
Reference in New Issue
Block a user