new api 2.0

This commit is contained in:
daniel 2024-02-07 15:44:53 +01:00
parent c0a2a4fdeb
commit 510e8452cf
7 changed files with 79 additions and 17 deletions

View File

@ -51,19 +51,19 @@ final class GroupFilter extends FilterAbstract
public function fieldsTranslate(): array
{
return [
'idGroup' => GroupDataMapper::ID_GROUP,
'name' => GroupDataMapper::NAME,
'icon' => GroupDataMapper::ICON,
'parent' => GroupDataMapper::PARENT,
'isPropagate' => GroupDataMapper::IS_PROPAGATE,
'isAlertEnabled' => GroupDataMapper::IS_DISABLED,
'customId' => GroupDataMapper::CUSTOM_ID,
'idSkin' => GroupDataMapper::ID_SKIN,
'description' => GroupDataMapper::DESCRIPTION,
'contact' => GroupDataMapper::CONTACT,
'other' => GroupDataMapper::OTHER,
'password' => GroupDataMapper::PASSWORD,
'maxAgents' => GroupDataMapper::MAX_AGENTS,
'idGroup' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::ID_GROUP,
'name' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::NAME,
'icon' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::ICON,
'parent' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::PARENT,
'isPropagate' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::IS_PROPAGATE,
'isAlertEnabled' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::IS_DISABLED,
'customId' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::CUSTOM_ID,
'idSkin' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::ID_SKIN,
'description' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::DESCRIPTION,
'contact' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::CONTACT,
'other' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::OTHER,
'password' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::PASSWORD,
'maxAgents' => GroupDataMapper::TABLE_NAME.'.'.GroupDataMapper::MAX_AGENTS,
];
}

View File

@ -150,7 +150,13 @@ class RepositoryMySQL extends Repository
protected function dbFormatWhereClauseSQL(array $values, $prefix = ''): string
{
ob_start();
$result = \db_format_array_where_clause_sql($values, 'AND', $prefix);
$values_prefix = [];
foreach ($values as $key => $value) {
$values_prefix[$prefix.$key] = $value;
}
$result = \db_format_array_where_clause_sql($values_prefix, 'AND');
$error = ob_get_clean();
if ($result === false && empty($error) === false) {
throw new Exception($error);

View File

@ -16,7 +16,7 @@ use PandoraFMS\Modules\Users\Validators\UserValidator;
* property="idUser",
* type="string",
* nullable=false,
* description="Id user, not "
* description="Id user"
* ),
* @OA\Property(
* property="fullName",

View File

@ -21,4 +21,6 @@ interface UserRepository
public function update(User $user): User;
public function delete(string $id): void;
public function getExistUser(string $idUser): User;
}

View File

@ -85,10 +85,30 @@ class UserRepositoryMySQL extends RepositoryMySQL implements UserRepository
return $this->userDataMapper->fromDatabase($result);
}
public function getExistUser(string $idUser): User
{
try {
$sql = sprintf('SELECT * FROM `tusuario` WHERE `id_user` = "%s"', $idUser);
$result = $this->dbGetRowSql($sql);
} catch (\Throwable $th) {
// Capture errors mysql.
throw new InvalidArgumentException(
strip_tags($th->getMessage()),
HttpCodesEnum::INTERNAL_SERVER_ERROR
);
}
if (empty($result) === true) {
throw new NotFoundException(__('%s not found', $this->userDataMapper->getStringNameClass()));
}
return $this->userDataMapper->fromDatabase($result);
}
public function create(User $user): User
{
$id = $this->__create($user, $this->userDataMapper);
return $user->setIdUser($id);
$this->__create($user, $this->userDataMapper);
return $user;
}
public function update(User $user): User

View File

@ -0,0 +1,24 @@
<?php
namespace PandoraFMS\Modules\Users\Services;
use PandoraFMS\Modules\Users\Repositories\UserRepository;
use PandoraFMS\Modules\Shared\Exceptions\NotFoundException;
final class ExistIdUserService
{
public function __construct(
private UserRepository $UserRepository,
) {
}
public function __invoke(string $idUser): bool
{
try {
$this->UserRepository->getExistUser($idUser);
return true;
} catch (NotFoundException) {
return false;
}
}
}

View File

@ -12,6 +12,7 @@ use PandoraFMS\Modules\Shared\Services\Timestamp;
use PandoraFMS\Modules\Users\Entities\User;
use PandoraFMS\Modules\Users\Enums\UserHomeScreenEnum;
use PandoraFMS\Modules\Users\Services\CheckOldPasswordUserService;
use PandoraFMS\Modules\Users\Services\ExistIdUserService;
use PandoraFMS\Modules\Users\Services\GetUserService;
use PandoraFMS\Modules\Users\Services\ValidatePasswordUserService;
@ -21,6 +22,7 @@ final class UserValidation
private Config $config,
private Timestamp $timestamp,
private GetUserService $getUserService,
private ExistIdUserService $existIdUserService,
private CheckOldPasswordUserService $checkOldPasswordUserService,
private ValidatePasswordUserService $validatePasswordUserService,
private GetEventFilterService $getEventFilterService
@ -32,6 +34,14 @@ final class UserValidation
$isAdmin = $this->isAdmin($this->config->get('id_user'));
$this->validateIdUser($user);
if ($oldUser === null || $oldUser->getIdUser() !== $user->getIdUser()) {
if($this->existIdUserService->__invoke($user->getIdUser()) === true) {
throw new BadRequestException(
__('Id user %s is already exists', $user->getIdUser())
);
}
}
if ($isAdmin === false && $user->getIsAdmin() === true) {
throw new ForbiddenACLException(__('User by non administrator user'));
}