From 0f6fab06fb4cdd4950c9ff2991fb84e7cc60d437 Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 12 Feb 2024 09:49:44 +0100 Subject: [PATCH] new api 2.0 --- pandora_console/api/v1/public/swagger.json | 215 ------------------ pandora_console/include/functions_events.php | 2 +- .../lib/Modules/Events/Entities/Event.php | 34 +-- .../Validations/EventFilterValidation.php | 47 ++-- .../Events/Validations/EventValidation.php | 51 +++-- .../Services/CreateUserProfileService.php | 2 - 6 files changed, 70 insertions(+), 281 deletions(-) diff --git a/pandora_console/api/v1/public/swagger.json b/pandora_console/api/v1/public/swagger.json index 8774e6db96..6b27d2b4bb 100644 --- a/pandora_console/api/v1/public/swagger.json +++ b/pandora_console/api/v1/public/swagger.json @@ -2081,191 +2081,6 @@ } ] }, - "Event": { - "properties": { - "idEvent": { - "description": "Id event", - "type": "integer", - "readOnly": true, - "nullable": false - }, - "idAgent": { - "description": "Id agent", - "type": "integer", - "default": null, - "nullable": true - }, - "idUser": { - "description": "Id user", - "type": "string", - "default": null, - "nullable": true - }, - "idGroup": { - "description": "Id group", - "type": "integer", - "default": null, - "nullable": true - }, - "status": { - "description": "Event status, the available status are: new, validated, inprocess", - "type": "integer", - "default": "new", - "enum": ["new", "validated", "inprocess"], - "nullable": false - }, - "timestamp": { - "description": "Event registration date", - "type": "string", - "default": null, - "readOnly": true, - "example": "2023-02-21 08:34:16", - "nullable": true - }, - "event": { - "description": "Description event", - "type": "string", - "default": "Event created for api", - "nullable": false - }, - "utimestamp": { - "description": "Event registration date", - "type": "integer", - "default": null, - "readOnly": true, - "example": "1704898868", - "nullable": true - }, - "eventType": { - "description": "Event status, the available status are: going_unknown, unknown, alert_fired, alert_recovered, alert_ceased, alert_manual_validation, recon_host_detected, system, error, new_agent, going_up_critical, going_down_critical, going_up_warning, going_down_warning, going_up_normal, going_down_normal, configuration_change, ncm", - "type": "string", - "default": "unknown", - "enum": [ - "going_unknown", - "unknown", - "alert_fired", - "alert_recovered", - "alert_ceased", - "alert_manual_validation", - "recon_host_detected", - "system", - "error", - "new_agent", - "going_up_critical", - "going_down_critical", - "going_up_warning", - "going_down_warning", - "going_up_normal", - "going_down_normal", - "configuration_change", - "ncm" - ], - "nullable": false - }, - "idAgentModule": { - "description": "Id agent module", - "type": "integer", - "default": null, - "nullable": true - }, - "idAlertAm": { - "description": "Id alert action", - "type": "integer", - "default": null, - "nullable": true - }, - "severity": { - "description": "Event severity, the available severity are: maintenance, informational, normal, warning, critical, minor, major", - "type": "integer", - "default": "maintenance", - "enum": [ - "maintenance", - "informational", - "normal", - "warning", - "critical", - "minor", - "major" - ], - "nullable": false - }, - "tags": { - "description": "Tags", - "type": "string", - "default": null, - "nullable": true - }, - "source": { - "description": "Source", - "type": "string", - "default": null, - "nullable": true - }, - "idExtra": { - "description": "Extra id", - "type": "string", - "default": null, - "nullable": true - }, - "criticalInstructions": { - "description": "Critical instructions", - "type": "string", - "default": null, - "nullable": true - }, - "warningInstructions": { - "description": "Warning instructions", - "type": "string", - "default": null, - "nullable": true - }, - "unknownInstructions": { - "description": "Unknows instructions", - "type": "string", - "default": null, - "nullable": true - }, - "ownerUser": { - "description": "Id user", - "type": "string", - "default": null, - "nullable": true - }, - "ackUtimestamp": { - "description": "Event ack utimestamp", - "type": "integer", - "default": null, - "readOnly": true, - "example": "1704898868", - "nullable": true - }, - "customData": { - "description": "Custom data", - "type": "string", - "default": null, - "nullable": true - }, - "data": { - "description": "Data", - "type": "string", - "default": null, - "nullable": true - }, - "moduleStatus": { - "description": "Module status", - "type": "integer", - "default": null, - "nullable": true - }, - "eventCustomId": { - "description": "Events Custom Id", - "type": "string", - "default": null, - "nullable": true - } - }, - "type": "object" - }, "EventFilter": { "properties": { "idEventFilter": { @@ -3501,16 +3316,6 @@ } } }, - "ResponseEvent": { - "description": "Event object", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Event" - } - } - } - }, "ResponseEventFilter": { "description": "EventFilter object", "content": { @@ -3694,16 +3499,6 @@ "default": 1 } }, - "parameterIdEvent": { - "name": "idEvent", - "in": "path", - "description": "Event id", - "required": true, - "schema": { - "type": "integer", - "default": 1 - } - }, "parameterIdEventFilter": { "name": "idEventFilter", "in": "path", @@ -3847,16 +3642,6 @@ } } }, - "requestBodyEvent": { - "required": true, - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Event" - } - } - } - }, "requestBodyEventFilter": { "required": true, "content": { diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index a3eea8a0fa..82a3a1e64f 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -1929,7 +1929,7 @@ function events_get_all( && $sort_field !== 'server_name' && $sort_field !== 'timestamp' ) { - $sort_field = explode('.', $sort_field)[1]; + $sort_field = (explode('.', $sort_field)[1] ?? $sort_field); if ($sort_field === 'user_comment') { $sort_field = 'comments'; } diff --git a/pandora_console/include/lib/Modules/Events/Entities/Event.php b/pandora_console/include/lib/Modules/Events/Entities/Event.php index 6823dc15c2..2c9b9e8ef7 100644 --- a/pandora_console/include/lib/Modules/Events/Entities/Event.php +++ b/pandora_console/include/lib/Modules/Events/Entities/Event.php @@ -16,7 +16,7 @@ use PandoraFMS\Modules\Shared\Entities\Entity; * property="idEvent", * type="integer", * nullable=false, - * description="Id event", + * description="Id event" * readOnly=true * ), * @OA\Property( @@ -24,14 +24,14 @@ use PandoraFMS\Modules\Shared\Entities\Entity; * type="integer", * nullable=true, * default=null, - * description="Id agent", + * description="Id agent" * ), * @OA\Property( * property="idUser", * type="string", * nullable=true, * default=null, - * description="Id user", + * description="Id user" * ), * @OA\Property( * property="idGroup", @@ -66,7 +66,7 @@ use PandoraFMS\Modules\Shared\Entities\Entity; * type="string", * nullable=false, * default="Event created for api", - * description="Description event", + * description="Description event" * ), * @OA\Property( * property="utimestamp", @@ -109,14 +109,14 @@ use PandoraFMS\Modules\Shared\Entities\Entity; * type="integer", * nullable=true, * default=null, - * description="Id agent module", + * description="Id agent module" * ), * @OA\Property( * property="idAlertAm", * type="integer", * nullable=true, * default=null, - * description="Id alert action", + * description="Id alert action" * ), * @OA\Property( * property="severity", @@ -139,49 +139,49 @@ use PandoraFMS\Modules\Shared\Entities\Entity; * type="string", * nullable=true, * default=null, - * description="Tags", + * description="Tags" * ), * @OA\Property( * property="source", * type="string", * nullable=true, * default=null, - * description="Source", + * description="Source" * ), * @OA\Property( * property="idExtra", * type="string", * nullable=true, * default=null, - * description="Extra id", + * description="Extra id" * ), * @OA\Property( * property="criticalInstructions", * type="string", * nullable=true, * default=null, - * description="Critical instructions", + * description="Critical instructions" * ), * @OA\Property( * property="warningInstructions", * type="string", * nullable=true, * default=null, - * description="Warning instructions", + * description="Warning instructions" * ), * @OA\Property( * property="unknownInstructions", * type="string", * nullable=true, * default=null, - * description="Unknows instructions", + * description="Unknows instructions" * ), * @OA\Property( * property="ownerUser", * type="string", * nullable=true, * default=null, - * description="Id user", + * description="Id user" * ), * @OA\Property( * property="ackUtimestamp", @@ -197,14 +197,14 @@ use PandoraFMS\Modules\Shared\Entities\Entity; * type="string", * nullable=true, * default=null, - * description="Custom data", + * description="Custom data" * ), * @OA\Property( * property="data", * type="string", * nullable=true, * default=null, - * description="Data", + * description="Data" * ), * @OA\Property( * property="moduleStatus", @@ -212,13 +212,14 @@ use PandoraFMS\Modules\Shared\Entities\Entity; * nullable=true, * default=null, * description="Module status", + * readonly=true * ), * @OA\Property( * property="eventCustomId", * type="string", * nullable=true, * default=null, - * description="Events Custom Id", + * description="Events Custom Id" * ) * ) * @@ -296,6 +297,7 @@ final class Event extends Entity 'timestamp' => 1, 'utimestamp' => 1, 'ackUtimestamp' => 1, + 'moduleStatus' => 1, ]; } diff --git a/pandora_console/include/lib/Modules/Events/Filters/Validations/EventFilterValidation.php b/pandora_console/include/lib/Modules/Events/Filters/Validations/EventFilterValidation.php index 29177465db..c54fe0f251 100644 --- a/pandora_console/include/lib/Modules/Events/Filters/Validations/EventFilterValidation.php +++ b/pandora_console/include/lib/Modules/Events/Filters/Validations/EventFilterValidation.php @@ -155,7 +155,7 @@ final class EventFilterValidation new Agent($idAgent); } catch (\Exception $e) { throw new BadRequestException( - __('Invalid id agent, %s', $e->getMessage()) + __('Invalid id agent: %s, %s', $idAgent, $e->getMessage()) ); } } @@ -163,29 +163,30 @@ final class EventFilterValidation protected function validateAgentModule(int $idAgentModule, ?int $idAgent = 0): void { // TODO: create new service for this. - try { - if(empty($idAgent) === false) { - $agent = new Agent($idAgent); - $existModule = $agent->searchModules( - ['id_agente_modulo' => $idAgentModule], - 1 - ); - - if (empty($existModule) === true) { - throw new BadRequestException( - __( - 'Id agent module not exist in agent %s', - io_safe_output($agent->alias()) - ) - ); - } - } else { - new Module($idAgentModule); - } - } catch (\Exception $e) { - throw new BadRequestException( - __('Invalid id agent module, %s', $e->getMessage()) + if(empty($idAgent) === false) { + $agent = new Agent($idAgent); + $existModule = $agent->searchModules( + ['id_agente_modulo' => $idAgentModule], + 1 ); + + if (empty($existModule) === true) { + throw new BadRequestException( + __( + 'Id agent module: %s not exist in agent %s', + $idAgentModule, + io_safe_output($agent->alias()) + ) + ); + } + } else { + try { + new Module($idAgentModule); + } catch (\Exception $e) { + throw new BadRequestException( + __('Invalid id agent module, %s', $e->getMessage()) + ); + } } } diff --git a/pandora_console/include/lib/Modules/Events/Validations/EventValidation.php b/pandora_console/include/lib/Modules/Events/Validations/EventValidation.php index 5a686dd821..f017e35cba 100644 --- a/pandora_console/include/lib/Modules/Events/Validations/EventValidation.php +++ b/pandora_console/include/lib/Modules/Events/Validations/EventValidation.php @@ -94,7 +94,6 @@ final class EventValidation } } - // TODO: readonly ??. if ($event->getModuleStatus() === null) { $event->setModuleStatus(0); } @@ -121,7 +120,7 @@ final class EventValidation new Agent($idAgent); } catch (\Exception $e) { throw new BadRequestException( - __('Invalid id agent, %s', $e->getMessage()) + __('Invalid id agent: %s, %s', $idAgent, $e->getMessage()) ); } } @@ -129,35 +128,39 @@ final class EventValidation protected function validateAgentModule(int $idAgentModule, ?int $idAgent = 0): void { // TODO: create new service for this. - try { - if(empty($idAgent) === false) { - $agent = new Agent($idAgent); - $existModule = $agent->searchModules( - ['id_agente_modulo' => $idAgentModule], - 1 - ); - - if (empty($existModule) === true) { - throw new BadRequestException( - __( - 'Id agent module not exist in agent %s', - io_safe_output($agent->alias()) - ) - ); - } - } else { - new Module($idAgentModule); - } - } catch (\Exception $e) { - throw new BadRequestException( - __('Invalid id agent module, %s', $e->getMessage()) + if(empty($idAgent) === false) { + $agent = new Agent($idAgent); + $existModule = $agent->searchModules( + ['id_agente_modulo' => $idAgentModule], + 1 ); + + if (empty($existModule) === true) { + throw new BadRequestException( + __( + 'Id agent module: %s not exist in agent %s', + $idAgentModule, + io_safe_output($agent->alias()) + ) + ); + } + } else { + try { + new Module($idAgentModule); + } catch (\Exception $e) { + throw new BadRequestException( + __('Invalid id agent module, %s', $e->getMessage()) + ); + } } } protected function validateAlert(int $idAlert): void { // TODO: create new service for this. + if (! (bool) \alerts_get_alert_agent_module($idAlert)) { + throw new BadRequestException(__('Invalid id Alert template')); + } } protected function getCurrentTimestamp(): string diff --git a/pandora_console/include/lib/Modules/Users/UserProfiles/Services/CreateUserProfileService.php b/pandora_console/include/lib/Modules/Users/UserProfiles/Services/CreateUserProfileService.php index ad250d482b..b120283b4e 100644 --- a/pandora_console/include/lib/Modules/Users/UserProfiles/Services/CreateUserProfileService.php +++ b/pandora_console/include/lib/Modules/Users/UserProfiles/Services/CreateUserProfileService.php @@ -26,8 +26,6 @@ final class CreateUserProfileService $profile = $this->getProfileService->__invoke($userProfile->getIdprofile()); - // TODO: Notificaciones. - $this->audit->write( AUDIT_LOG_USER_MANAGEMENT, 'Added profile: '.$profile->getName().' for user: '.$userProfile->getIdUser(),