Cli user
This commit is contained in:
30
app/src/Controller/API/Admin/Users.php
Normal file
30
app/src/Controller/API/Admin/Users.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API\Admin;
|
||||
|
||||
use Exception;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Controller\API\withJson;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Users
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Login $loginService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => array_filter($body, fn($key) => $key !== 'password', ARRAY_FILTER_USE_KEY),
|
||||
'success' => false,
|
||||
'user' => null
|
||||
];
|
||||
try {
|
||||
$user = $loginService->addUser($body);
|
||||
$output['success'] = true;
|
||||
$output['user'] = $user;
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
33
app/src/Controller/API/Login.php
Normal file
33
app/src/Controller/API/Login.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Login
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\User $userRepository,
|
||||
Repository\Login $loginRepository,
|
||||
Service\Login $loginService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'username' => $body['username'],
|
||||
];
|
||||
try {
|
||||
$user = $userRepository->fetchByName($body['username']);
|
||||
if ($user->validate($body['password'])) {
|
||||
$loginService->login($user);
|
||||
$output['token'] = $loginService->getToken();
|
||||
}
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ use Incoviba\Controller\withRedis;
|
||||
use Incoviba\Service;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Money
|
||||
{
|
||||
@ -77,6 +78,31 @@ class Money
|
||||
$output['uf'] = $ufService->get($date);
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function ufs(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService): ResponseInterface
|
||||
{
|
||||
$redisKey = 'uf';
|
||||
$output = [
|
||||
'ufs' => []
|
||||
];
|
||||
try {
|
||||
$output['ufs'] = (array) $this->fetchRedis($redisService, $redisKey);
|
||||
} catch (EmptyRedis) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function updateUfs(ServerRequestInterface $request, ResponseInterface $response,
|
||||
LoggerInterface $logger,
|
||||
Service\UF $ufService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$dates = array_map(function($dateData) {
|
||||
return new DateTimeImmutable($dateData);
|
||||
}, $body['fechas']);
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'ufs' => $ufService->updateMany($dates)
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function ipc(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\IPC $ipcService): ResponseInterface
|
||||
{
|
||||
|
21
app/src/Controller/Admin/Users.php
Normal file
21
app/src/Controller/Admin/Users.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\Admin;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Alias\View;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Users
|
||||
{
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view,
|
||||
Repository\User $userRepository): ResponseInterface
|
||||
{
|
||||
$users = [];
|
||||
try {
|
||||
$users = $userRepository->fetchAll('name');
|
||||
} catch (EmptyResult) {}
|
||||
return $view->render($response, 'admin.users', compact('users'));
|
||||
}
|
||||
}
|
@ -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