new api 2.0

This commit is contained in:
daniel 2024-01-29 16:09:08 +01:00
parent daa2a09e82
commit b3dd6cbb4c
15 changed files with 282 additions and 155 deletions

View File

@ -1,5 +1,7 @@
<?php
use PandoraFMS\Modules\Events\Repositories\EventRepository;
use PandoraFMS\Modules\Events\Repositories\EventRepositoryMySQL;
use PandoraFMS\Modules\Shared\Repositories\Repository;
use PandoraFMS\Modules\Shared\Repositories\RepositoryMySQL;
use PandoraFMS\Modules\Users\Repositories\UserRepository;
@ -9,10 +11,10 @@ use Slim\App;
use Slim\Factory\AppFactory;
return [
'settings' => 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);
},
];

View File

@ -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 ',

View File

@ -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;
}
}

View File

@ -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,
];
}

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -1,87 +0,0 @@
<?php
namespace PandoraFMS\Modules\Events\Entities;
use PandoraFMS\Modules\Events\Validators\EventValidator;
use PandoraFMS\Modules\Shared\Core\FilterAbstract;
final class EventFilter extends FilterAbstract
{
private ?string $freeSearch = null;
public function __construct()
{
$this->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];
}
}

View File

@ -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;
}

View File

@ -0,0 +1,168 @@
<?php
namespace PandoraFMS\Modules\Events\Repositories;
use InvalidArgumentException;
use PandoraFMS\Core\Config;
use PandoraFMS\Modules\EventFilters\Entities\EventFilter;
use PandoraFMS\Modules\Events\Entities\Event;
use PandoraFMS\Modules\Events\Entities\EventDataMapper;
use PandoraFMS\Modules\Shared\Enums\HttpCodesEnum;
use PandoraFMS\Modules\Shared\Exceptions\NotFoundException;
use PandoraFMS\Modules\Shared\Repositories\RepositoryMySQL;
class EventRepositoryMySQL extends RepositoryMySQL implements EventRepository
{
public function __construct(
private EventDataMapper $eventDataMapper,
private Config $config
) {
}
/**
* @return Event[],
*/
public function list(EventFilter $eventFilter): array
{
try {
$fields = ['te.*'];
$filter = $eventFilter->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;
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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