From b3dd6cbb4cd73cec91437b93bdb46b797d8d0ceb Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 29 Jan 2024 16:09:08 +0100 Subject: [PATCH] new api 2.0 --- pandora_console/api/v1/config/container.php | 13 +- pandora_console/include/functions_events.php | 7 + .../EventFilters/Entities/EventFilter.php | 41 +++++ .../Entities/EventFilterFilter.php | 40 ++++- .../Events/Actions/ListEventAction.php | 2 +- .../Controllers/DeleteEventController.php | 2 +- .../Events/Controllers/GetEventController.php | 2 +- .../Controllers/ListEventController.php | 4 +- .../Controllers/UpdateEventController.php | 2 +- .../Modules/Events/Entities/EventFilter.php | 87 --------- .../Events/Repositories/EventRepository.php | 59 +----- .../Repositories/EventRepositoryMySQL.php | 168 ++++++++++++++++++ .../Events/Services/CountEventService.php | 2 +- .../Events/Services/GetEventService.php | 6 +- .../Events/Services/ListEventService.php | 2 +- 15 files changed, 282 insertions(+), 155 deletions(-) delete mode 100644 pandora_console/include/lib/Modules/Events/Entities/EventFilter.php create mode 100644 pandora_console/include/lib/Modules/Events/Repositories/EventRepositoryMySQL.php diff --git a/pandora_console/api/v1/config/container.php b/pandora_console/api/v1/config/container.php index 5968078210..8f917f758a 100644 --- a/pandora_console/api/v1/config/container.php +++ b/pandora_console/api/v1/config/container.php @@ -1,5 +1,7 @@ function () { + 'settings' => function () { return include __DIR__.'/settings.php'; }, - App::class => function (ContainerInterface $container) { + App::class => function (ContainerInterface $container) { AppFactory::setContainer($container); $app = AppFactory::create(); @@ -33,10 +35,13 @@ return [ return $app; }, - Repository::class => function (ContainerInterface $container) { + Repository::class => function (ContainerInterface $container) { return $container->get(RepositoryMySQL::class); }, - UserRepository::class => function (ContainerInterface $container) { + UserRepository::class => function (ContainerInterface $container) { return $container->get(UserRepositoryMySQL::class); }, + EventRepository::class => function (ContainerInterface $container) { + return $container->get(EventRepositoryMySQL::class); + }, ]; diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index e328f4cd91..3523ae16c1 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -826,6 +826,13 @@ function events_get_all( $sql_filters = get_filter_date($filter); + if (isset($filter['id_event']) === true && $filter['id_event'] > 0) { + $sql_filters[] = sprintf( + ' AND te.id_evento = %d ', + $filter['id_event'] + ); + } + if (isset($filter['id_agent']) === true && $filter['id_agent'] > 0) { $sql_filters[] = sprintf( ' AND te.id_agente = %d ', diff --git a/pandora_console/include/lib/Modules/EventFilters/Entities/EventFilter.php b/pandora_console/include/lib/Modules/EventFilters/Entities/EventFilter.php index 17ac0e9d0c..ba8c792823 100644 --- a/pandora_console/include/lib/Modules/EventFilters/Entities/EventFilter.php +++ b/pandora_console/include/lib/Modules/EventFilters/Entities/EventFilter.php @@ -9,6 +9,9 @@ use PandoraFMS\Modules\EventFilters\Enums\EventFilterStatusEnum; use PandoraFMS\Modules\EventFilters\Validators\EventFilterValidator; use PandoraFMS\Modules\Events\Enums\EventTypeEnum; use PandoraFMS\Modules\Shared\Entities\Entity; +use PandoraFMS\Modules\Shared\Traits\GroupByFilterTrait; +use PandoraFMS\Modules\Shared\Traits\OrderFilterTrait; +use PandoraFMS\Modules\Shared\Traits\PaginationFilterTrait; /** * @OA\Schema( @@ -392,6 +395,12 @@ use PandoraFMS\Modules\Shared\Entities\Entity; */ final class EventFilter extends Entity { + use PaginationFilterTrait; + use OrderFilterTrait; + use GroupByFilterTrait; + + private ?int $idEvent = null; + private ?int $idEventFilter = null; private ?int $idGroupFilter = null; private ?string $name = null; @@ -437,10 +446,28 @@ final class EventFilter extends Entity return ['idEventFilter' => 1]; } + public function toTranslateFilters(): array + { + $eventFilterFilter = new EventFilterFilter(); + $filter_translate = $eventFilterFilter->fieldsTranslate(); + + $result = []; + foreach ($this->toArray() as $key => $value) { + if (isset($filter_translate[$key]) === true + && $value !== null + ) { + $result[$filter_translate[$key]] = $value; + } + } + + return $result; + } + public function jsonSerialize(): mixed { return [ 'idEventFilter' => $this->getIdEventFilter(), + 'idEvent' => $this->getIdEvent(), 'idGroupFilter' => $this->getIdGroupFilter(), 'name' => $this->getName(), 'idGroup' => $this->getIdGroup(), @@ -485,6 +512,10 @@ final class EventFilter extends Entity EventFilterValidator::INTEGER, EventFilterValidator::GREATERTHAN, ], + 'idEvent' => [ + EventFilterValidator::INTEGER, + EventFilterValidator::GREATERTHAN, + ], 'idGroupFilter' => [ EventFilterValidator::INTEGER, EventFilterValidator::GREATEREQUALTHAN, @@ -921,4 +952,14 @@ final class EventFilter extends Entity $this->regex = $regex; return $this; } + + public function getIdEvent(): ?int + { + return $this->idEvent; + } + public function setIdEvent(?int $idEvent): self + { + $this->idEvent = $idEvent; + return $this; + } } diff --git a/pandora_console/include/lib/Modules/EventFilters/Entities/EventFilterFilter.php b/pandora_console/include/lib/Modules/EventFilters/Entities/EventFilterFilter.php index 4279dc6b6a..3ea52fc105 100644 --- a/pandora_console/include/lib/Modules/EventFilters/Entities/EventFilterFilter.php +++ b/pandora_console/include/lib/Modules/EventFilters/Entities/EventFilterFilter.php @@ -51,8 +51,44 @@ final class EventFilterFilter extends FilterAbstract public function fieldsTranslate(): array { return [ - 'idEventFilter' => EventFilterDataMapper::ID_FILTER, - 'name' => EventFilterDataMapper::NAME, + 'idEventFilter' => EventFilterDataMapper::ID_FILTER, + 'idEvent' => 'id_event', + 'name' => EventFilterDataMapper::NAME, + 'idEventFilter' => EventFilterDataMapper::ID_FILTER, + 'idGroupFilter' => EventFilterDataMapper::ID_GROUP_FILTER, + 'name' => EventFilterDataMapper::NAME, + 'idGroup' => EventFilterDataMapper::ID_GROUP, + 'eventType' => EventFilterDataMapper::EVENT_TYPE, + 'severity' => EventFilterDataMapper::SEVERITY, + 'status' => EventFilterDataMapper::STATUS, + 'search' => EventFilterDataMapper::SEARCH, + 'isNotSearch' => EventFilterDataMapper::NOT_SEARCH, + 'textAgent' => EventFilterDataMapper::TEXT_AGENT, + 'idAgent' => EventFilterDataMapper::ID_AGENT, + 'idAgentModule' => EventFilterDataMapper::ID_AGENT_MODULE, + 'pagination' => EventFilterDataMapper::PAGINATION, + 'slice' => EventFilterDataMapper::SLICE, + 'idUserAck' => EventFilterDataMapper::ID_USER_ACK, + 'groupBy' => EventFilterDataMapper::ORDER_BY, + 'tagWith' => EventFilterDataMapper::TAG_WITH, + 'tagWithout' => EventFilterDataMapper::TAG_WITHOUT, + 'filterOnlyAlert' => EventFilterDataMapper::FILTER_ONLY_ALERT, + 'searchSecondaryGroups' => EventFilterDataMapper::SEARCH_SECONDARY_GROUPS, + 'searchRecursiveGroups' => EventFilterDataMapper::SEARCH_RECURSIVE_GROUPS, + 'dateFrom' => EventFilterDataMapper::DATE_FROM, + 'dateTo' => EventFilterDataMapper::DATE_TO, + 'source' => EventFilterDataMapper::SOURCE, + 'idExtra' => EventFilterDataMapper::ID_EXTRA, + 'userComment' => EventFilterDataMapper::USER_COMMENT, + 'idSourceEvent' => EventFilterDataMapper::ID_SOURCE_EVENT, + 'serverId' => EventFilterDataMapper::SERVER_ID, + 'timeFrom' => EventFilterDataMapper::TIME_FROM, + 'timeTo' => EventFilterDataMapper::TIME_TO, + 'customData' => EventFilterDataMapper::CUSTOM_DATA, + 'customDataFilterType' => EventFilterDataMapper::CUSTOM_DATA_FILTER_TYPE, + 'ownerUser' => EventFilterDataMapper::OWNER_USER, + 'privateFilterUser' => EventFilterDataMapper::PRIVATE_FILTER_USER, + 'regex' => EventFilterDataMapper::REGEX, ]; } diff --git a/pandora_console/include/lib/Modules/Events/Actions/ListEventAction.php b/pandora_console/include/lib/Modules/Events/Actions/ListEventAction.php index 96e9048c3d..84b7f64407 100644 --- a/pandora_console/include/lib/Modules/Events/Actions/ListEventAction.php +++ b/pandora_console/include/lib/Modules/Events/Actions/ListEventAction.php @@ -2,7 +2,7 @@ namespace PandoraFMS\Modules\Events\Actions; -use PandoraFMS\Modules\Events\Entities\EventFilter; +use PandoraFMS\Modules\EventFilters\Entities\EventFilter; use PandoraFMS\Modules\Events\Services\CountEventService; use PandoraFMS\Modules\Events\Services\ListEventService; diff --git a/pandora_console/include/lib/Modules/Events/Controllers/DeleteEventController.php b/pandora_console/include/lib/Modules/Events/Controllers/DeleteEventController.php index 523a501380..dfcd3103d8 100644 --- a/pandora_console/include/lib/Modules/Events/Controllers/DeleteEventController.php +++ b/pandora_console/include/lib/Modules/Events/Controllers/DeleteEventController.php @@ -39,7 +39,7 @@ final class DeleteEventController extends Controller $idEvent = $this->getParam($request, 'idEvent'); $event = $this->getEventAction->__invoke($idEvent); - //$this->acl->validate(0, 'UM', ' tried to manage event'); + $this->acl->validate(0, 'EM', ' tried to manage event'); $result = $this->deleteEventAction->__invoke($event); return $this->getResponse($response, $result); diff --git a/pandora_console/include/lib/Modules/Events/Controllers/GetEventController.php b/pandora_console/include/lib/Modules/Events/Controllers/GetEventController.php index 1449236201..cf26c067c9 100644 --- a/pandora_console/include/lib/Modules/Events/Controllers/GetEventController.php +++ b/pandora_console/include/lib/Modules/Events/Controllers/GetEventController.php @@ -36,7 +36,7 @@ final class GetEventController extends Controller { $idEvent = $this->getParam($request, 'idEvent'); - //$this->acl->validate(0, 'UM', ' tried to manage event'); + $this->acl->validate(0, 'ER', ' tried to manage event'); $result = $this->getEventAction->__invoke($idEvent); return $this->getResponse($response, $result); diff --git a/pandora_console/include/lib/Modules/Events/Controllers/ListEventController.php b/pandora_console/include/lib/Modules/Events/Controllers/ListEventController.php index d94896b255..e5f266c3c7 100644 --- a/pandora_console/include/lib/Modules/Events/Controllers/ListEventController.php +++ b/pandora_console/include/lib/Modules/Events/Controllers/ListEventController.php @@ -3,7 +3,7 @@ namespace PandoraFMS\Modules\Events\Controllers; use PandoraFMS\Modules\Events\Actions\ListEventAction; -use PandoraFMS\Modules\Events\Entities\EventFilter; +use PandoraFMS\Modules\EventFilters\Entities\EventFilter; use PandoraFMS\Modules\Shared\Controllers\Controller; use PandoraFMS\Modules\Shared\Services\ValidateAclSystem; @@ -66,7 +66,7 @@ final class ListEventController extends Controller // @var EventFilter $eventFilter. $eventFilter = $this->fromRequest($request, EventFilter::class); - //$this->acl->validate(0, 'UM', ' tried to manage event'); + $this->acl->validate(0, 'ER', ' tried to read event'); $result = $this->listEventAction->__invoke($eventFilter); return $this->getResponse($response, $result); diff --git a/pandora_console/include/lib/Modules/Events/Controllers/UpdateEventController.php b/pandora_console/include/lib/Modules/Events/Controllers/UpdateEventController.php index b2a509a0eb..dd2828653a 100644 --- a/pandora_console/include/lib/Modules/Events/Controllers/UpdateEventController.php +++ b/pandora_console/include/lib/Modules/Events/Controllers/UpdateEventController.php @@ -44,7 +44,7 @@ final class UpdateEventController extends Controller $params = $this->extractParams($request); $event->fromArray($params); - //$this->acl->validate(0, 'UM', ' tried to manage event'); + $this->acl->validate(0, 'EW', ' tried to write event'); $result = $this->updateEventAction->__invoke($event, $oldEvent); return $this->getResponse($response, $result); diff --git a/pandora_console/include/lib/Modules/Events/Entities/EventFilter.php b/pandora_console/include/lib/Modules/Events/Entities/EventFilter.php deleted file mode 100644 index c7e9dc877e..0000000000 --- a/pandora_console/include/lib/Modules/Events/Entities/EventFilter.php +++ /dev/null @@ -1,87 +0,0 @@ -setDefaultFieldOrder(EventDataMapper::UTIMESTAMP); - $this->setDefaultDirectionOrder($this::ASC); - $this->setEntityFilter(new Event()); - } - - public function fieldsTranslate(): array - { - return [ - 'idEvent' => EventDataMapper::ID_EVENT, - 'utimestamp' => EventDataMapper::UTIMESTAMP, - ]; - } - - public function fieldsReadOnly(): array - { - return []; - } - - public function jsonSerialize(): mixed - { - return [ - 'freeSearch' => $this->getFreeSearch(), - ]; - } - - public function getValidations(): array - { - $validations = []; - if($this->getEntityFilter() !== null) { - $validations = $this->getEntityFilter()->getValidations(); - } - $validations['freeSearch'] = EventValidator::STRING; - return $validations; - } - - public function validateFields(array $filters): array - { - return (new EventValidator())->validate($filters); - } - - /** - * Get the value of freeSearch. - * - * @return ?string - */ - public function getFreeSearch(): ?string - { - return $this->freeSearch; - } - - /** - * Set the value of freeSearch. - * - * @param ?string $freeSearch - * - */ - public function setFreeSearch(?string $freeSearch): self - { - $this->freeSearch = $freeSearch; - return $this; - } - - /** - * Get the value of fieldsFreeSearch. - * - * @return ?array - */ - public function getFieldsFreeSearch(): ?array - { - return [EventDataMapper::UTIMESTAMP]; - } - -} diff --git a/pandora_console/include/lib/Modules/Events/Repositories/EventRepository.php b/pandora_console/include/lib/Modules/Events/Repositories/EventRepository.php index d77b78a0b4..86efa57791 100644 --- a/pandora_console/include/lib/Modules/Events/Repositories/EventRepository.php +++ b/pandora_console/include/lib/Modules/Events/Repositories/EventRepository.php @@ -2,64 +2,23 @@ namespace PandoraFMS\Modules\Events\Repositories; +use PandoraFMS\Modules\EventFilters\Entities\EventFilter; use PandoraFMS\Modules\Events\Entities\Event; -use PandoraFMS\Modules\Events\Entities\EventDataMapper; -use PandoraFMS\Modules\Events\Entities\EventFilter; -use PandoraFMS\Modules\Shared\Repositories\Repository; -class EventRepository +interface EventRepository { - public function __construct( - private Repository $repository, - private EventDataMapper $eventDataMapper - ) { - } - /** * @return Event[], - */ - public function list(EventFilter $eventFilter): array - { - return $this->repository->__list( - $eventFilter, - $this->eventDataMapper - ); - } + */ + public function list(EventFilter $eventFilter): array; - public function count(EventFilter $eventFilter): int - { - return $this->repository->__count( - $eventFilter, - $this->eventDataMapper - ); - } + public function count(EventFilter $eventFilter): int; - public function getOne(EventFilter $eventFilter): Event - { - return $this->repository->__getOne( - $eventFilter, - $this->eventDataMapper - ); - } + public function getOne(EventFilter $eventFilter): Event; - public function create(Event $event): Event - { - $id = $this->repository->__create($event, $this->eventDataMapper); - return $event->setIdEvent($id); - } + public function create(Event $event): Event; - public function update(Event $event): Event - { - return $this->repository->__update( - $event, - $this->eventDataMapper, - $event->getIdEvent() - ); - } - - public function delete(int $id): void - { - $this->repository->__delete($id, $this->eventDataMapper); - } + public function update(Event $event): Event; + public function delete(string $id): void; } diff --git a/pandora_console/include/lib/Modules/Events/Repositories/EventRepositoryMySQL.php b/pandora_console/include/lib/Modules/Events/Repositories/EventRepositoryMySQL.php new file mode 100644 index 0000000000..d901189701 --- /dev/null +++ b/pandora_console/include/lib/Modules/Events/Repositories/EventRepositoryMySQL.php @@ -0,0 +1,168 @@ +toTranslateFilters(); + $offset = ($eventFilter->getPage() ?? null); + $limit = ($eventFilter->getSizePage() ?? null); + $order = ($eventFilter->getSortDirection() ?? null); + $sort_field = ($eventFilter->getSortField() ?? null); + + $list = $this->getEvents( + $fields, + $filter, + $offset, + $limit, + $order, + $sort_field + ); + + } catch (\Throwable $th) { + // Capture errors mysql. + throw new InvalidArgumentException( + strip_tags($th->getMessage()), + HttpCodesEnum::INTERNAL_SERVER_ERROR + ); + } + + if (is_array($list) === false) { + throw new NotFoundException(__('%s not found', $this->eventDataMapper->getStringNameClass())); + } + + $result = []; + foreach ($list as $fields) { + $result[] = $this->eventDataMapper->fromDatabase($fields); + } + + return $result; + } + + public function count(EventFilter $eventFilter): int + { + try { + $fields = ['count']; + $filter = $eventFilter->toTranslateFilters(); + $count = $this->getEvents( + $fields, + $filter + ); + + if (empty($count) === false + && isset($count[0]) === true + && isset($count[0]['nitems']) === true + && empty($count[0]['nitems']) === false + ) { + $count = $count[0]['nitems']; + } + } catch (\Throwable $th) { + // Capture errors mysql. + throw new InvalidArgumentException( + strip_tags($th->getMessage()), + HttpCodesEnum::INTERNAL_SERVER_ERROR + ); + } + + return (int) $count; + } + + public function getOne(EventFilter $eventFilter): Event + { + try { + $fields = ['te.*']; + $filter = $eventFilter->toTranslateFilters(); + $result = $this->getEvents( + $fields, + $filter + ); + + if (empty($result) === false + && isset($result[0]) === true + ) { + $result = $result[0]; + } + } 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->eventDataMapper->getStringNameClass())); + } + + return $this->eventDataMapper->fromDatabase($result); + } + + public function create(Event $event): Event + { + $this->__create($event, $this->eventDataMapper); + return $event; + } + + public function update(Event $event): Event + { + return $this->__update( + $event, + $this->eventDataMapper, + $event->getIdEvent() + ); + } + + public function delete(string $id): void + { + $this->__delete($id, $this->eventDataMapper); + } + + public function getEvents( + $fields, + $filter, + $offset = null, + $limit = null, + $order = null, + $sort_field = null + ): array { + // TODO: XXX. + $history = true; + + ob_start(); + require_once $this->config->get('homedir').'/include/functions_events.php'; + $list = \events_get_all( + $fields, + $filter, + $offset, + $limit, + $order, + $sort_field, + $history + ); + ob_get_clean(); + + return $list; + } +} diff --git a/pandora_console/include/lib/Modules/Events/Services/CountEventService.php b/pandora_console/include/lib/Modules/Events/Services/CountEventService.php index 5b88ba7b29..97581db74a 100644 --- a/pandora_console/include/lib/Modules/Events/Services/CountEventService.php +++ b/pandora_console/include/lib/Modules/Events/Services/CountEventService.php @@ -2,7 +2,7 @@ namespace PandoraFMS\Modules\Events\Services; -use PandoraFMS\Modules\Events\Entities\EventFilter; +use PandoraFMS\Modules\EventFilters\Entities\EventFilter; use PandoraFMS\Modules\Events\Repositories\EventRepository; final class CountEventService diff --git a/pandora_console/include/lib/Modules/Events/Services/GetEventService.php b/pandora_console/include/lib/Modules/Events/Services/GetEventService.php index f8ee9bba76..175a224423 100644 --- a/pandora_console/include/lib/Modules/Events/Services/GetEventService.php +++ b/pandora_console/include/lib/Modules/Events/Services/GetEventService.php @@ -3,7 +3,7 @@ namespace PandoraFMS\Modules\Events\Services; use PandoraFMS\Modules\Events\Entities\Event; -use PandoraFMS\Modules\Events\Entities\EventFilter; +use PandoraFMS\Modules\EventFilters\Entities\EventFilter; use PandoraFMS\Modules\Events\Repositories\EventRepository; final class GetEventService @@ -16,9 +16,7 @@ final class GetEventService public function __invoke(int $idEvent): Event { $eventFilter = new EventFilter(); - /** @var Event $entityFilter */ - $entityFilter = $eventFilter->getEntityFilter(); - $entityFilter->setIdEvent($idEvent); + $eventFilter->setIdEvent($idEvent); return $this->eventRepository->getOne($eventFilter); } diff --git a/pandora_console/include/lib/Modules/Events/Services/ListEventService.php b/pandora_console/include/lib/Modules/Events/Services/ListEventService.php index 4bdb5f2092..a3d516cc21 100644 --- a/pandora_console/include/lib/Modules/Events/Services/ListEventService.php +++ b/pandora_console/include/lib/Modules/Events/Services/ListEventService.php @@ -2,7 +2,7 @@ namespace PandoraFMS\Modules\Events\Services; -use PandoraFMS\Modules\Events\Entities\EventFilter; +use PandoraFMS\Modules\EventFilters\Entities\EventFilter; use PandoraFMS\Modules\Events\Repositories\EventRepository; final class ListEventService