From 510e8452cf9d40f2976be2abd75c324f8d59c539 Mon Sep 17 00:00:00 2001 From: daniel Date: Wed, 7 Feb 2024 15:44:53 +0100 Subject: [PATCH] new api 2.0 --- .../Modules/Groups/Entities/GroupFilter.php | 26 +++++++++---------- .../Shared/Repositories/RepositoryMySQL.php | 8 +++++- .../lib/Modules/Users/Entities/User.php | 2 +- .../Users/Repositories/UserRepository.php | 2 ++ .../Repositories/UserRepositoryMySQL.php | 24 +++++++++++++++-- .../Users/Services/ExistIdUserService.php | 24 +++++++++++++++++ .../Users/Validations/UserValidation.php | 10 +++++++ 7 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 pandora_console/include/lib/Modules/Users/Services/ExistIdUserService.php diff --git a/pandora_console/include/lib/Modules/Groups/Entities/GroupFilter.php b/pandora_console/include/lib/Modules/Groups/Entities/GroupFilter.php index 7f9af6084f..362ddbb016 100644 --- a/pandora_console/include/lib/Modules/Groups/Entities/GroupFilter.php +++ b/pandora_console/include/lib/Modules/Groups/Entities/GroupFilter.php @@ -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, ]; } diff --git a/pandora_console/include/lib/Modules/Shared/Repositories/RepositoryMySQL.php b/pandora_console/include/lib/Modules/Shared/Repositories/RepositoryMySQL.php index 7b6be03a25..fb5e4717d9 100644 --- a/pandora_console/include/lib/Modules/Shared/Repositories/RepositoryMySQL.php +++ b/pandora_console/include/lib/Modules/Shared/Repositories/RepositoryMySQL.php @@ -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); diff --git a/pandora_console/include/lib/Modules/Users/Entities/User.php b/pandora_console/include/lib/Modules/Users/Entities/User.php index 99fe282f7d..e3f0b329aa 100644 --- a/pandora_console/include/lib/Modules/Users/Entities/User.php +++ b/pandora_console/include/lib/Modules/Users/Entities/User.php @@ -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", diff --git a/pandora_console/include/lib/Modules/Users/Repositories/UserRepository.php b/pandora_console/include/lib/Modules/Users/Repositories/UserRepository.php index c4a3b39c60..80e5100e5c 100644 --- a/pandora_console/include/lib/Modules/Users/Repositories/UserRepository.php +++ b/pandora_console/include/lib/Modules/Users/Repositories/UserRepository.php @@ -21,4 +21,6 @@ interface UserRepository public function update(User $user): User; public function delete(string $id): void; + + public function getExistUser(string $idUser): User; } diff --git a/pandora_console/include/lib/Modules/Users/Repositories/UserRepositoryMySQL.php b/pandora_console/include/lib/Modules/Users/Repositories/UserRepositoryMySQL.php index 9aae38fc34..6253a6221a 100644 --- a/pandora_console/include/lib/Modules/Users/Repositories/UserRepositoryMySQL.php +++ b/pandora_console/include/lib/Modules/Users/Repositories/UserRepositoryMySQL.php @@ -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 diff --git a/pandora_console/include/lib/Modules/Users/Services/ExistIdUserService.php b/pandora_console/include/lib/Modules/Users/Services/ExistIdUserService.php new file mode 100644 index 0000000000..029b40177c --- /dev/null +++ b/pandora_console/include/lib/Modules/Users/Services/ExistIdUserService.php @@ -0,0 +1,24 @@ +UserRepository->getExistUser($idUser); + return true; + } catch (NotFoundException) { + return false; + } + } +} diff --git a/pandora_console/include/lib/Modules/Users/Validations/UserValidation.php b/pandora_console/include/lib/Modules/Users/Validations/UserValidation.php index 135441facc..5fe41e57d9 100644 --- a/pandora_console/include/lib/Modules/Users/Validations/UserValidation.php +++ b/pandora_console/include/lib/Modules/Users/Validations/UserValidation.php @@ -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')); }