diff --git a/pandora_console/api/v1/config/routes.php b/pandora_console/api/v1/config/routes.php index 47cc933529..aa77df22c3 100644 --- a/pandora_console/api/v1/config/routes.php +++ b/pandora_console/api/v1/config/routes.php @@ -3,7 +3,9 @@ use Slim\App; return function (App $app) { - (include __DIR__.'/../../../include/lib/Modules/Users/routes.php')($app); - (include __DIR__.'/../../../include/lib/Modules/Profiles/routes.php')($app); (include __DIR__.'/../../../include/lib/Modules/Events/routes.php')($app); + (include __DIR__.'/../../../include/lib/Modules/Groups/routes.php')($app); + (include __DIR__.'/../../../include/lib/Modules/Profiles/routes.php')($app); + (include __DIR__.'/../../../include/lib/Modules/Tags/routes.php')($app); + (include __DIR__.'/../../../include/lib/Modules/Users/routes.php')($app); }; diff --git a/pandora_console/api/v1/public/swagger.json b/pandora_console/api/v1/public/swagger.json index 7360862ddb..fa4b812025 100644 --- a/pandora_console/api/v1/public/swagger.json +++ b/pandora_console/api/v1/public/swagger.json @@ -1,8 +1,8 @@ { "openapi": "3.0.0", "info": { - "title": "Nueva API de Pandora FMS", - "description": "Lorem ipsum dolor sit amet consectetur, adipisicing elit. Libero, quibusdam esse commodi rem nisi cumque quos ut, exercitationem recusandae ipsam fuga qui veritatis non temporibus perferendis earum amet cupiditate eum nam corrupti! Dicta tempora, debitis molestiae corrupti sequi asperiores libero perferendis ut aperiam laboriosam repudiandae neque, rem quidem consectetur. Magnam illum perferendis aspernatur quibusdam esse? Libero eius veritatis quae perspiciatis. Sit recusandae aspernatur possimus autem! Corporis ipsa voluptatem placeat quasi praesentium esse doloremque magni, error, cumque vel, consequatur quam saepe iusto accusantium tempore ab dignissimos alias sint officia fuga voluptas. Vel repudiandae dicta ipsum repellat reprehenderit. Molestiae, ullam dolorum voluptatem necessitatibus itaque officiis ducimus consectetur aut facilis atque aliquid reiciendis voluptas sit incidunt, repellendus soluta quod obcaecati unde quas. Error officiis cumque vero minima amet modi enim, placeat consectetur cupiditate, fugiat odit sunt a earum natus dicta, labore id dolor! Quis laboriosam a quasi fuga! Ullam consectetur, voluptates repellat eveniet delectus officia nostrum amet obcaecati adipisci natus voluptas explicabo dolores similique doloribus. Rerum voluptatibus aperiam quidem necessitatibus, sint dignissimos natus delectus dolorem dicta sunt eum doloribus eligendi similique a at in repellat fuga voluptatem atque consectetur, ratione sit! Magni tenetur quos laborum, excepturi eveniet laboriosam optio aperiam eaque sit iusto.", + "title": "API Pandora FMS", + "description": "Documentacion para desarrolladores de la nueva api de pandora fms.", "termsOfService": "https://example.com/terms/", "contact": { "name": "Pandorafms", @@ -460,6 +460,218 @@ ] } }, + "/group": { + "post": { + "tags": ["Groups"], + "summary": "Creates a new groups", + "operationId": "2c799105ff37ce09e5aec019cd500615", + "requestBody": { + "$ref": "#/components/requestBodies/requestBodyGroup" + }, + "responses": { + "200": { + "$ref": "#/components/responses/ResponseGroup" + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + } + }, + "/group/{idGroup}": { + "get": { + "tags": ["Groups"], + "summary": "Show group", + "operationId": "e617d2dd05b5da29729a629b722e489e", + "parameters": [ + { + "$ref": "#/components/parameters/parameterIdGroup" + } + ], + "responses": { + "200": { + "$ref": "#/components/responses/ResponseGroup" + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + }, + "put": { + "tags": ["Groups"], + "summary": "Updates an group", + "operationId": "1b9f27089e1734250037a2542a63216e", + "parameters": [ + { + "$ref": "#/components/parameters/parameterIdGroup" + } + ], + "requestBody": { + "$ref": "#/components/requestBodies/requestBodyGroup" + }, + "responses": { + "200": { + "$ref": "#/components/responses/ResponseGroup" + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + }, + "delete": { + "tags": ["Groups"], + "summary": "Deletes an group object.", + "operationId": "d61a992170621de9cc9a2cb61f90de3f", + "parameters": [ + { + "$ref": "#/components/parameters/parameterIdGroup" + } + ], + "responses": { + "200": { + "$ref": "#/components/responses/successfullyDeleted" + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + } + }, + "/group/list": { + "post": { + "tags": ["Groups"], + "summary": "List groups", + "operationId": "3bbc68353ef79c71c178426336f21b93", + "parameters": [ + { + "$ref": "#/components/parameters/parameterPage" + }, + { + "$ref": "#/components/parameters/parameterSizePage" + }, + { + "$ref": "#/components/parameters/parameterSortField" + }, + { + "$ref": "#/components/parameters/parameterSortDirection" + } + ], + "requestBody": { + "$ref": "#/components/requestBodies/requestBodyGroupFilter" + }, + "responses": { + "200": { + "description": "List Groups Object", + "content": { + "application/json": { + "schema": { + "properties": { + "paginationData": { + "$ref": "#/components/schemas/paginationData" + }, + "data": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Group" + } + } + }, + "type": "object" + } + } + } + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + } + }, "/profile": { "post": { "tags": ["Profiles"], @@ -672,6 +884,218 @@ ] } }, + "/tag": { + "post": { + "tags": ["Tags"], + "summary": "Creates a new tags", + "operationId": "c2511f535e2073c2e91f1d9c71a594a5", + "requestBody": { + "$ref": "#/components/requestBodies/requestBodyTag" + }, + "responses": { + "200": { + "$ref": "#/components/responses/ResponseTag" + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + } + }, + "/tag/{idTag}": { + "get": { + "tags": ["Tags"], + "summary": "Show tag", + "operationId": "fe2e12539c1e64bf9962ae0c83c6a2ca", + "parameters": [ + { + "$ref": "#/components/parameters/parameterIdTag" + } + ], + "responses": { + "200": { + "$ref": "#/components/responses/ResponseTag" + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + }, + "put": { + "tags": ["Tags"], + "summary": "Updates an tag", + "operationId": "80bf6897a82194c8dc44eaf21b6edc73", + "parameters": [ + { + "$ref": "#/components/parameters/parameterIdTag" + } + ], + "requestBody": { + "$ref": "#/components/requestBodies/requestBodyTag" + }, + "responses": { + "200": { + "$ref": "#/components/responses/ResponseTag" + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + }, + "delete": { + "tags": ["Tags"], + "summary": "Deletes an tag object.", + "operationId": "9f4c209e4eb50cad9c3db451ddeb32e1", + "parameters": [ + { + "$ref": "#/components/parameters/parameterIdTag" + } + ], + "responses": { + "200": { + "$ref": "#/components/responses/successfullyDeleted" + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + } + }, + "/tag/list": { + "post": { + "tags": ["Tags"], + "summary": "List tags", + "operationId": "86fbbb0adcd5db93622153640b12f246", + "parameters": [ + { + "$ref": "#/components/parameters/parameterPage" + }, + { + "$ref": "#/components/parameters/parameterSizePage" + }, + { + "$ref": "#/components/parameters/parameterSortField" + }, + { + "$ref": "#/components/parameters/parameterSortDirection" + } + ], + "requestBody": { + "$ref": "#/components/requestBodies/requestBodyTagFilter" + }, + "responses": { + "200": { + "description": "List Incidence object", + "content": { + "application/json": { + "schema": { + "properties": { + "paginationData": { + "$ref": "#/components/schemas/paginationData" + }, + "data": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Tag" + } + } + }, + "type": "object" + } + } + } + }, + "400": { + "$ref": "#/components/responses/BadRequest" + }, + "401": { + "$ref": "#/components/responses/Unauthorized" + }, + "403": { + "$ref": "#/components/responses/Forbidden" + }, + "404": { + "$ref": "#/components/responses/NotFound" + }, + "500": { + "$ref": "#/components/responses/InternalServerError" + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + } + }, "/user": { "post": { "tags": ["Users"], @@ -1351,6 +1775,113 @@ } ] }, + "Group": { + "properties": { + "idGroup": { + "description": "Id group", + "type": "integer", + "readOnly": true, + "nullable": false + }, + "name": { + "description": "Name group", + "type": "string", + "default": null, + "nullable": true + }, + "icon": { + "description": "Path icon, by default: without-group@groups.svg", + "type": "string", + "default": null, + "readOnly": true, + "nullable": true + }, + "parent": { + "description": "Id Group parent", + "type": "integer", + "default": null, + "nullable": true + }, + "isPropagate": { + "description": "Group propagate", + "type": "boolean", + "default": "false", + "nullable": false + }, + "isAlertEnabled": { + "description": "Group is alert enabled", + "type": "boolean", + "default": "true", + "nullable": false + }, + "customId": { + "description": "Custom id", + "type": "string", + "default": null, + "nullable": true + }, + "idSkin": { + "description": "Id skin", + "type": "integer", + "default": null, + "nullable": true + }, + "description": { + "description": "Description", + "type": "string", + "default": null, + "nullable": true + }, + "contact": { + "description": "Contact", + "type": "string", + "default": null, + "nullable": true + }, + "other": { + "description": "Other things", + "type": "string", + "default": null, + "nullable": true + }, + "password": { + "type": "string", + "default": null, + "writeOnly": true, + "nullable": true + }, + "maxAgents": { + "description": "Maximum number of agents per group", + "type": "integer", + "default": 0, + "nullable": false + } + }, + "type": "object" + }, + "GroupFilter": { + "type": "object", + "allOf": [ + { + "$ref": "#/components/schemas/Group" + }, + { + "properties": { + "idGroup": { + "default": null, + "readOnly": false + }, + "freeSearch": { + "description": "Find word in name field.", + "type": "string", + "default": null, + "nullable": true + } + }, + "type": "object" + } + ] + }, "Profile": { "properties": { "idProfile": { @@ -1565,6 +2096,72 @@ }, "type": "object" }, + "Tag": { + "properties": { + "idTag": { + "description": "Id Tag", + "type": "integer", + "readOnly": true, + "nullable": false + }, + "name": { + "description": "Name of the tag", + "type": "string", + "default": null, + "nullable": false + }, + "description": { + "description": "Description of the tag", + "type": "string", + "default": null, + "nullable": true + }, + "url": { + "description": "Url of the tag", + "type": "string", + "default": null, + "nullable": true + }, + "phone": { + "description": "Phone of the tag", + "type": "string", + "default": null, + "nullable": true + }, + "previousName": { + "description": "Previous name of the tag", + "type": "string", + "default": null, + "readOnly": true, + "nullable": true, + "deprecated": true + } + }, + "type": "object" + }, + "TagFilter": { + "type": "object", + "allOf": [ + { + "$ref": "#/components/schemas/Tag" + }, + { + "properties": { + "idTag": { + "default": null, + "readOnly": false + }, + "freeSearch": { + "description": "Find word in name field.", + "type": "string", + "default": null, + "nullable": true + } + }, + "type": "object" + } + ] + }, "User": { "properties": { "idUser": { @@ -2066,6 +2663,16 @@ } } }, + "ResponseGroup": { + "description": "Group object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Group" + } + } + } + }, "ResponseProfile": { "description": "Profile object", "content": { @@ -2177,6 +2784,16 @@ } } }, + "ResponseTag": { + "description": "Tag object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Tag" + } + } + } + }, "ResponseUser": { "description": "User object", "content": { @@ -2219,6 +2836,16 @@ "default": 1 } }, + "parameterIdGroup": { + "name": "idGroup", + "in": "path", + "description": "Group id", + "required": true, + "schema": { + "type": "integer", + "default": 1 + } + }, "parameterIdProfile": { "name": "idProfile", "in": "path", @@ -2270,6 +2897,16 @@ "enum": ["ascending", "descending"] } }, + "parameterIdTag": { + "name": "idTag", + "in": "path", + "description": "Tag id", + "required": true, + "schema": { + "type": "integer", + "default": 1 + } + }, "parameterIdUser": { "name": "idUser", "in": "path", @@ -2332,6 +2969,26 @@ } } }, + "requestBodyGroup": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Group" + } + } + } + }, + "requestBodyGroupFilter": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GroupFilter" + } + } + } + }, "requestBodyProfile": { "required": true, "content": { @@ -2352,6 +3009,26 @@ } } }, + "requestBodyTag": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Tag" + } + } + } + }, + "requestBodyTagFilter": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/TagFilter" + } + } + } + }, "requestBodyUser": { "required": true, "content": { @@ -2405,30 +3082,24 @@ }, "tags": [ { - "name": "Users", - "description": "API Endpoints of users" + "name": "Events", + "description": "API Endpoints of events" + }, + { + "name": "Groups", + "description": "API Endpoints of Groups" }, { "name": "Profiles", "description": "API Endpoints of profiles" }, { - "name": "Events", - "description": "API Endpoints of events" - } - ], - "x-tagGroups": [ + "name": "Tags", + "description": "API Endpoints of tags" + }, { "name": "Users", - "tags": ["Users"] - }, - { - "name": "Profiles", - "tags": ["Profiles"] - }, - { - "name": "Events", - "tags": ["Events"] + "description": "API Endpoints of users" } ] } diff --git a/pandora_console/include/lib/Modules/Events/Entities/EventFilter.php b/pandora_console/include/lib/Modules/Events/Entities/EventFilter.php index 75e58b2c15..63417a695d 100644 --- a/pandora_console/include/lib/Modules/Events/Entities/EventFilter.php +++ b/pandora_console/include/lib/Modules/Events/Entities/EventFilter.php @@ -2,8 +2,8 @@ namespace PandoraFMS\Modules\Events\Entities; +use PandoraFMS\Modules\Events\Validators\EventValidator; use PandoraFMS\Modules\Shared\Core\FilterAbstract; -use PandoraFMS\Modules\Shared\Validators\Validator; /** * @OA\Schema( @@ -74,13 +74,13 @@ final class EventFilter extends FilterAbstract if($this->getEntityFilter() !== null) { $validations = $this->getEntityFilter()->getValidations(); } - $validations['freeSearch'] = Validator::STRING; + $validations['freeSearch'] = EventValidator::STRING; return $validations; } public function validateFields(array $filters): array { - return (new Validator())->validate($filters); + return (new EventValidator())->validate($filters); } /** diff --git a/pandora_console/include/lib/Modules/Events/Validations/EventValidation.php b/pandora_console/include/lib/Modules/Events/Validations/EventValidation.php index c3037331f2..48f2bf76b4 100644 --- a/pandora_console/include/lib/Modules/Events/Validations/EventValidation.php +++ b/pandora_console/include/lib/Modules/Events/Validations/EventValidation.php @@ -6,6 +6,7 @@ use PandoraFMS\Core\Config; use PandoraFMS\Modules\Events\Entities\Event; use PandoraFMS\Modules\Events\Enums\EventSeverityEnum; use PandoraFMS\Modules\Events\Enums\EventStatusEnum; +use PandoraFMS\Modules\Groups\Services\GetGroupService; use PandoraFMS\Modules\Shared\Exceptions\BadRequestException; use PandoraFMS\Modules\Shared\Services\Timestamp; use PandoraFMS\Modules\Shared\Services\ValidateAclSystem; @@ -17,7 +18,8 @@ final class EventValidation private ValidateAclSystem $acl, private Config $config, private Timestamp $timestamp, - private GetUserService $getUserService + private GetUserService $getUserService, + private GetGroupService $getGroupService ) { } @@ -107,10 +109,7 @@ final class EventValidation protected function validateGroup(int $idGroup): void { - // TODO: create new service for this. - if (! (bool) \groups_get_users($idGroup)) { - throw new BadRequestException(__('Invalid id group')); - } + $this->getGroupService->__invoke($idGroup); } protected function validateAgent(int $idAgent): void diff --git a/pandora_console/include/lib/Modules/Groups/Actions/CreateGroupAction.php b/pandora_console/include/lib/Modules/Groups/Actions/CreateGroupAction.php new file mode 100644 index 0000000000..490a2c9530 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Actions/CreateGroupAction.php @@ -0,0 +1,19 @@ +createGroupService->__invoke($group); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Actions/DeleteGroupAction.php b/pandora_console/include/lib/Modules/Groups/Actions/DeleteGroupAction.php new file mode 100644 index 0000000000..79997862cd --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Actions/DeleteGroupAction.php @@ -0,0 +1,19 @@ +deleteGroupService->__invoke($group); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Actions/GetGroupAction.php b/pandora_console/include/lib/Modules/Groups/Actions/GetGroupAction.php new file mode 100644 index 0000000000..fc70b8cd52 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Actions/GetGroupAction.php @@ -0,0 +1,19 @@ +getGroupService->__invoke($idGroup); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Actions/ListGroupAction.php b/pandora_console/include/lib/Modules/Groups/Actions/ListGroupAction.php new file mode 100644 index 0000000000..94f2fb074c --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Actions/ListGroupAction.php @@ -0,0 +1,28 @@ +getPage(), + $groupFilter->getSizePage(), + $this->countGroupService->__invoke($groupFilter), + $this->listGroupService->__invoke($groupFilter) + ))->toArray(); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Actions/UpdateGroupAction.php b/pandora_console/include/lib/Modules/Groups/Actions/UpdateGroupAction.php new file mode 100644 index 0000000000..ee4b4d9e69 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Actions/UpdateGroupAction.php @@ -0,0 +1,19 @@ +updateGroupService->__invoke($group, $oldGroup); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Controllers/CreateGroupController.php b/pandora_console/include/lib/Modules/Groups/Controllers/CreateGroupController.php new file mode 100644 index 0000000000..cc6faccc23 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Controllers/CreateGroupController.php @@ -0,0 +1,47 @@ +fromRequest($request, Group::class); + + $this->acl->validate(0, 'UM', ' tried to manage user'); + + $result = $this->createGroupAction->__invoke($group); + + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Controllers/DeleteGroupController.php b/pandora_console/include/lib/Modules/Groups/Controllers/DeleteGroupController.php new file mode 100644 index 0000000000..5cc8f39316 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Controllers/DeleteGroupController.php @@ -0,0 +1,47 @@ +getParam($request, 'idGroup'); + $group = $this->getGroupAction->__invoke($idGroup); + + $this->acl->validate(0, 'UM', ' tried to manage user'); + + $result = $this->deleteGroupAction->__invoke($group); + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Controllers/GetGroupController.php b/pandora_console/include/lib/Modules/Groups/Controllers/GetGroupController.php new file mode 100644 index 0000000000..dfe202ce75 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Controllers/GetGroupController.php @@ -0,0 +1,42 @@ +getParam($request, 'idGroup'); + + $result = $this->getGroupAction->__invoke($idGroup); + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Controllers/ListGroupController.php b/pandora_console/include/lib/Modules/Groups/Controllers/ListGroupController.php new file mode 100644 index 0000000000..1df772c42d --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Controllers/ListGroupController.php @@ -0,0 +1,72 @@ +fromRequest($request, GroupFilter::class); + + $result = $this->listGroupAction->__invoke($groupFilter); + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Controllers/UpdateGroupController.php b/pandora_console/include/lib/Modules/Groups/Controllers/UpdateGroupController.php new file mode 100644 index 0000000000..5af1d042e7 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Controllers/UpdateGroupController.php @@ -0,0 +1,52 @@ +getParam($request, 'idGroup'); + $group = $this->getGroupAction->__invoke($idGroup); + + $oldGroup = clone $group; + $params = $this->extractParams($request); + $group->fromArray($params); + + $this->acl->validate(0, 'UM', ' tried to manage user'); + + $result = $this->updateGroupAction->__invoke($group, $oldGroup); + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Entities/Group.php b/pandora_console/include/lib/Modules/Groups/Entities/Group.php new file mode 100644 index 0000000000..7ce38faf36 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Entities/Group.php @@ -0,0 +1,349 @@ + 1]; + } + + public function jsonSerialize(): mixed + { + return [ + 'idGroup' => $this->getIdGroup(), + 'name' => $this->getName(), + 'icon' => $this->getIcon(), + 'parent' => $this->getParent(), + 'isPropagate' => $this->getIsPropagate(), + 'isAlertEnabled' => $this->getIsAlertEnabled(), + 'customId' => $this->getCustomId(), + 'idSkin' => $this->getIdSkin(), + 'description' => $this->getDescription(), + 'contact' => $this->getContact(), + 'other' => $this->getOther(), + 'maxAgents' => $this->getMaxAgents(), + ]; + } + + public function getValidations(): array + { + return [ + 'idGroup' => [ + Validator::INTEGER, + Validator::GREATERTHAN, + ], + 'name' => Validator::STRING, + 'icon' => Validator::STRING, + 'parent' => [ + Validator::INTEGER, + Validator::GREATEREQUALTHAN, + ], + 'isPropagate' => Validator::BOOLEAN, + 'isAlertEnabled' => Validator::BOOLEAN, + 'customId' => Validator::STRING, + 'idSkin' => [ + Validator::INTEGER, + Validator::GREATEREQUALTHAN, + ], + 'description' => Validator::STRING, + 'contact' => Validator::STRING, + 'other' => Validator::STRING, + 'maxAgents' => [ + Validator::INTEGER, + Validator::GREATEREQUALTHAN, + ], + ]; + } + + public function validateFields(array $filters): array + { + return (new Validator())->validate($filters); + } + + public function getIdGroup(): ?int + { + return $this->idGroup; + } + public function setIdGroup(?int $idGroup): self + { + $this->idGroup = $idGroup; + return $this; + } + + public function getName(): ?string + { + return $this->name; + } + public function setName(?string $name): self + { + $this->name = $name; + return $this; + } + + public function getIcon(): ?string + { + return $this->icon; + } + public function setIcon(?string $icon): self + { + $this->icon = $icon; + return $this; + } + + public function getParent(): ?int + { + return $this->parent; + } + public function setParent(?int $parent): self + { + $this->parent = $parent; + return $this; + } + + public function getIsPropagate(): ?bool + { + return $this->isPropagate; + } + public function setIsPropagate(?bool $isPropagate): self + { + $this->isPropagate = $isPropagate; + return $this; + } + + public function getIsAlertEnabled(): ?bool + { + return $this->isAlertEnabled; + } + public function setIsAlertEnabled(?bool $isAlertEnabled): self + { + $this->isAlertEnabled = $isAlertEnabled; + return $this; + } + + public function getCustomId(): ?string + { + return $this->customId; + } + public function setCustomId(?string $customId): self + { + $this->customId = $customId; + return $this; + } + + public function getIdSkin(): ?int + { + return $this->idSkin; + } + public function setIdSkin(?int $idSkin): self + { + $this->idSkin = $idSkin; + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + public function setDescription(?string $description): self + { + $this->description = $description; + return $this; + } + + public function getContact(): ?string + { + return $this->contact; + } + public function setContact(?string $contact): self + { + $this->contact = $contact; + return $this; + } + + public function getOther(): ?string + { + return $this->other; + } + public function setOther(?string $other): self + { + $this->other = $other; + return $this; + } + + public function getPassword(): ?string + { + return $this->password; + } + public function setPassword(?string $password): self + { + $this->password = $password; + return $this; + } + + public function getMaxAgents(): ?int + { + return $this->maxAgents; + } + public function setMaxAgents(?int $maxAgents): self + { + $this->maxAgents = $maxAgents; + return $this; + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Entities/GroupDataMapper.php b/pandora_console/include/lib/Modules/Groups/Entities/GroupDataMapper.php new file mode 100644 index 0000000000..e15b5bbd1a --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Entities/GroupDataMapper.php @@ -0,0 +1,80 @@ +builder->build(new Group(), [ + 'idGroup' => $data[self::ID_GROUP], + 'name' => $this->repository->safeOutput($data[self::NAME]), + 'icon' => $data[self::ICON], + 'parent' => $data[self::PARENT], + 'isPropagate' => $data[self::IS_PROPAGATE], + 'isAlertEnabled' => $data[self::IS_DISABLED], + 'customId' => $data[self::CUSTOM_ID], + 'idSkin' => $data[self::ID_SKIN], + 'description' => $this->repository->safeOutput($data[self::DESCRIPTION]), + 'contact' => $this->repository->safeOutput($data[self::CONTACT]), + 'other' => $this->repository->safeOutput($data[self::OTHER]), + 'password' => $data[self::PASSWORD], + 'maxAgents' => $data[self::MAX_AGENTS], + ]); + } + + public function toDatabase(MappeableInterface $data): array + { + /** @var Group $data */ + return [ + self::ID_GROUP => $data->getIdGroup(), + self::NAME => $this->repository->safeInput($data->getName()), + self::ICON => $data->getIcon(), + self::PARENT => $data->getParent(), + self::IS_PROPAGATE => $data->getIsPropagate(), + self::IS_DISABLED => $data->getIsAlertEnabled(), + self::CUSTOM_ID => $data->getCustomId(), + self::ID_SKIN => $data->getIdSkin(), + self::DESCRIPTION => $this->repository->safeInput($data->getDescription()), + self::CONTACT => $this->repository->safeInput($data->getContact()), + self::OTHER => $this->repository->safeInput($data->getOther()), + self::PASSWORD => $this->repository->safeInput($data->getPassword()), + self::MAX_AGENTS => $data->getMaxAgents(), + ]; + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Entities/GroupFilter.php b/pandora_console/include/lib/Modules/Groups/Entities/GroupFilter.php new file mode 100644 index 0000000000..ba9e1a652a --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Entities/GroupFilter.php @@ -0,0 +1,128 @@ +setDefaultFieldOrder(GroupDataMapper::NAME); + $this->setDefaultDirectionOrder($this::ASC); + $this->setEntityFilter(new Group()); + } + + 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, + ]; + } + + public function fieldsReadOnly(): array + { + return ['password' => 1]; + } + + public function jsonSerialize(): mixed + { + return [ + 'freeSearch' => $this->getFreeSearch(), + ]; + } + + public function getValidations(): array + { + $validations = []; + if($this->getEntityFilter() !== null) { + $validations = $this->getEntityFilter()->getValidations(); + } + $validations['freeSearch'] = Validator::STRING; + return $validations; + } + + public function validateFields(array $filters): array + { + return (new Validator())->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 [GroupDataMapper::NAME, GroupDataMapper::DESCRIPTION]; + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Repositories/GroupRepository.php b/pandora_console/include/lib/Modules/Groups/Repositories/GroupRepository.php new file mode 100644 index 0000000000..c60a569c8f --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Repositories/GroupRepository.php @@ -0,0 +1,65 @@ +repository->__list( + $groupFilter, + $this->groupDataMapper + ); + } + + public function count(GroupFilter $groupFilter): int + { + return $this->repository->__count( + $groupFilter, + $this->groupDataMapper + ); + } + + public function getOne(GroupFilter $groupFilter): Group + { + return $this->repository->__getOne( + $groupFilter, + $this->groupDataMapper + ); + } + + public function create(Group $group): Group + { + $id = $this->repository->__create($group, $this->groupDataMapper); + return $group->setIdGroup($id); + } + + public function update(Group $group): Group + { + return $this->repository->__update( + $group, + $this->groupDataMapper, + $group->getIdGroup() + ); + } + + public function delete(int $id): void + { + $this->repository->__delete($id, $this->groupDataMapper); + } + +} diff --git a/pandora_console/include/lib/Modules/Groups/Services/CountGroupService.php b/pandora_console/include/lib/Modules/Groups/Services/CountGroupService.php new file mode 100644 index 0000000000..4b8cccc95f --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Services/CountGroupService.php @@ -0,0 +1,19 @@ +groupRepository->count($groupFilter); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Services/CreateGroupService.php b/pandora_console/include/lib/Modules/Groups/Services/CreateGroupService.php new file mode 100644 index 0000000000..f2f0d0c2da --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Services/CreateGroupService.php @@ -0,0 +1,33 @@ +groupValidation->__invoke($group); + + $group = $this->groupRepository->create($group); + + $this->audit->write( + AUDIT_LOG_USER_MANAGEMENT, + 'Create group '.$group->getIdGroup(), + json_encode($group->toArray()) + ); + + return $group; + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Services/DeleteGroupService.php b/pandora_console/include/lib/Modules/Groups/Services/DeleteGroupService.php new file mode 100644 index 0000000000..66e1658b4c --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Services/DeleteGroupService.php @@ -0,0 +1,42 @@ +getIdGroup(); + + // TODO: XXX + db_process_sql_update( + 'tgrupo', + ['parent' => $group->getParent()], + ['parent' => $idGroup] + ); + + // TODO: XXX + db_process_sql_delete( + 'tgroup_stat', + ['id_group' => $idGroup] + ); + + $this->groupRepository->delete($idGroup); + + $this->audit->write( + AUDIT_LOG_USER_MANAGEMENT, + 'Deleted group '.$idGroup + ); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Services/ExistNameGroupService.php b/pandora_console/include/lib/Modules/Groups/Services/ExistNameGroupService.php new file mode 100644 index 0000000000..29c15d7c23 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Services/ExistNameGroupService.php @@ -0,0 +1,31 @@ +getEntityFilter(); + $entityFilter->setName($name); + + try { + $this->GroupRepository->getOne($GroupFilter); + return true; + } catch (NotFoundException) { + return false; + } + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Services/GetGroupService.php b/pandora_console/include/lib/Modules/Groups/Services/GetGroupService.php new file mode 100644 index 0000000000..6734e1221d --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Services/GetGroupService.php @@ -0,0 +1,25 @@ +getEntityFilter(); + $entityFilter->setIdGroup($idGroup); + + return $this->groupRepository->getOne($groupFilter); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Services/ListGroupService.php b/pandora_console/include/lib/Modules/Groups/Services/ListGroupService.php new file mode 100644 index 0000000000..48acb34e37 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Services/ListGroupService.php @@ -0,0 +1,19 @@ +groupRepository->list($groupFilter); + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Services/UpdateGroupService.php b/pandora_console/include/lib/Modules/Groups/Services/UpdateGroupService.php new file mode 100644 index 0000000000..6c98677a74 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Services/UpdateGroupService.php @@ -0,0 +1,33 @@ +groupValidation->__invoke($group, $oldGroup); + + $group = $this->groupRepository->update($group); + + $this->audit->write( + AUDIT_LOG_USER_MANAGEMENT, + 'Update group '.$group->getIdGroup(), + json_encode($group->toArray()) + ); + + return $group; + } +} diff --git a/pandora_console/include/lib/Modules/Groups/Validations/GroupValidation.php b/pandora_console/include/lib/Modules/Groups/Validations/GroupValidation.php new file mode 100644 index 0000000000..c8a5081d80 --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/Validations/GroupValidation.php @@ -0,0 +1,80 @@ +getName()) { + throw new BadRequestException(__('Name is missing')); + } + + if ($oldGroup === null || $oldGroup->getName() !== $group->getName()) { + if($this->existNameGroupService->__invoke($group->getName()) === true) { + throw new BadRequestException( + __('Name %s is already exists', $group->getName()) + ); + } + } + + if ($oldGroup === null) { + $group->setIcon('without-group@groups.svg'); + } + + if ($group->getIsPropagate() === null) { + $group->setIsPropagate(false); + } + + if ($group->getIsAlertEnabled() === null) { + $group->setIsAlertEnabled(true); + } + + if ($group->getParent() === null) { + $group->setParent(0); + } + + if (empty($group->getParent()) === false) { + $this->validateGroup($group->getParent()); + $this->acl->validate($group->getParent(), 'AR', ' tried to read group'); + } + + if ($group->getIdSkin() === null) { + $group->setIdSkin(0); + } + + if (empty($group->getIdSkin()) === false) { + $this->validateSkin($group->getIdSkin()); + } + + if ($group->getMaxAgents() === null) { + $group->setMaxAgents(0); + } + } + + protected function validateGroup(int $idGroup): void + { + $this->getGroupService->__invoke($idGroup); + } + + protected function validateSkin(int $idSkin): void + { + // TODO: create new service for this. + if (! (bool) \skins_search_skin_id($idSkin)) { + throw new BadRequestException(__('Invalid id skin')); + } + } +} diff --git a/pandora_console/include/lib/Modules/Groups/routes.php b/pandora_console/include/lib/Modules/Groups/routes.php new file mode 100644 index 0000000000..b05d265b1b --- /dev/null +++ b/pandora_console/include/lib/Modules/Groups/routes.php @@ -0,0 +1,16 @@ +map(['GET', 'POST'], '/group/list', ListGroupController::class); + $app->get('/group/{idGroup}', GetGroupController::class); + $app->post('/group', CreateGroupController::class); + $app->put('/group/{idGroup}', UpdateGroupController::class); + $app->delete('/group/{idGroup}', DeleteGroupController::class); +}; diff --git a/pandora_console/include/lib/Modules/Shared/Core/DataMapperAbstract.php b/pandora_console/include/lib/Modules/Shared/Core/DataMapperAbstract.php index ce31f1ad85..cf06d654f7 100644 --- a/pandora_console/include/lib/Modules/Shared/Core/DataMapperAbstract.php +++ b/pandora_console/include/lib/Modules/Shared/Core/DataMapperAbstract.php @@ -83,8 +83,11 @@ abstract class DataMapperAbstract { $strname = [ 'PandoraFMS\\Modules\\Users\\Entities\\User' => 'User', - 'PandoraFMS\\Modules\\Users\\Entities\\Profile' => 'Profile', 'PandoraFMS\\Modules\\Users\\UserProfiles\\Entities\\UserProfile' => 'UserProfile', + 'PandoraFMS\\Modules\\Profiles\\Entities\\Profile' => 'Profile', + 'PandoraFMS\\Modules\\Events\\Entities\\Event' => 'Event', + 'PandoraFMS\\Modules\\Groups\\Entities\\Group' => 'Group', + 'PandoraFMS\\Modules\\Tags\\Entities\\Tag' => 'Tag', ]; $result = ($strname[$this->getClassName()] ?? ''); diff --git a/pandora_console/include/lib/Modules/Shared/Documentation/OpenApi.php b/pandora_console/include/lib/Modules/Shared/Documentation/OpenApi.php index 8ae4675254..89f6b1bef5 100644 --- a/pandora_console/include/lib/Modules/Shared/Documentation/OpenApi.php +++ b/pandora_console/include/lib/Modules/Shared/Documentation/OpenApi.php @@ -6,8 +6,8 @@ use OpenApi\Annotations as OA; /** * @OA\Info( - * title="Nueva API de Pandora FMS", - * description="Lorem ipsum dolor sit amet consectetur, adipisicing elit. Libero, quibusdam esse commodi rem nisi cumque quos ut, exercitationem recusandae ipsam fuga qui veritatis non temporibus perferendis earum amet cupiditate eum nam corrupti! Dicta tempora, debitis molestiae corrupti sequi asperiores libero perferendis ut aperiam laboriosam repudiandae neque, rem quidem consectetur. Magnam illum perferendis aspernatur quibusdam esse? Libero eius veritatis quae perspiciatis. Sit recusandae aspernatur possimus autem! Corporis ipsa voluptatem placeat quasi praesentium esse doloremque magni, error, cumque vel, consequatur quam saepe iusto accusantium tempore ab dignissimos alias sint officia fuga voluptas. Vel repudiandae dicta ipsum repellat reprehenderit. Molestiae, ullam dolorum voluptatem necessitatibus itaque officiis ducimus consectetur aut facilis atque aliquid reiciendis voluptas sit incidunt, repellendus soluta quod obcaecati unde quas. Error officiis cumque vero minima amet modi enim, placeat consectetur cupiditate, fugiat odit sunt a earum natus dicta, labore id dolor! Quis laboriosam a quasi fuga! Ullam consectetur, voluptates repellat eveniet delectus officia nostrum amet obcaecati adipisci natus voluptas explicabo dolores similique doloribus. Rerum voluptatibus aperiam quidem necessitatibus, sint dignissimos natus delectus dolorem dicta sunt eum doloribus eligendi similique a at in repellat fuga voluptatem atque consectetur, ratione sit! Magni tenetur quos laborum, excepturi eveniet laboriosam optio aperiam eaque sit iusto.", + * title="API Pandora FMS", + * description="Documentacion para desarrolladores de la nueva api de pandora fms.", * termsOfService="https://example.com/terms/", * @OA\Contact( * name="Pandorafms", @@ -37,36 +37,25 @@ use OpenApi\Annotations as OA; * description="PandoraFMS API Server" * ), * @OA\Tag( - * name="Users", - * description="API Endpoints of users" + * name="Events", + * description="API Endpoints of events" + * ), + * @OA\Tag( + * name="Groups", + * description="API Endpoints of Groups" * ), * @OA\Tag( * name="Profiles", * description="API Endpoints of profiles" * ), * @OA\Tag( - * name="Events", - * description="API Endpoints of events" + * name="Tags", + * description="API Endpoints of tags" * ), - * @OA\OpenApi( - * x={ - * "tagGroups"= { - * { - * "name"="Users", - * "tags"={"Users"} - * }, - * { - * "name"="Profiles", - * "tags"={"Profiles"} - * }, - * { - * "name"="Events", - * "tags"={"Events"} - * }, - * } - * } + * @OA\Tag( + * name="Users", + * description="API Endpoints of users" * ), - * * @OA\Parameter( * parameter="parameterPage", * name="page", diff --git a/pandora_console/include/lib/Modules/Tags/Actions/CreateTagAction.php b/pandora_console/include/lib/Modules/Tags/Actions/CreateTagAction.php new file mode 100644 index 0000000000..da61340873 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Actions/CreateTagAction.php @@ -0,0 +1,19 @@ +createTagService->__invoke($tag); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Actions/DeleteTagAction.php b/pandora_console/include/lib/Modules/Tags/Actions/DeleteTagAction.php new file mode 100644 index 0000000000..282f18b1a2 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Actions/DeleteTagAction.php @@ -0,0 +1,19 @@ +deleteTagService->__invoke($tag); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Actions/GetTagAction.php b/pandora_console/include/lib/Modules/Tags/Actions/GetTagAction.php new file mode 100644 index 0000000000..514c296229 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Actions/GetTagAction.php @@ -0,0 +1,19 @@ +getTagService->__invoke($idTag); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Actions/ListTagAction.php b/pandora_console/include/lib/Modules/Tags/Actions/ListTagAction.php new file mode 100644 index 0000000000..efed126c6a --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Actions/ListTagAction.php @@ -0,0 +1,28 @@ +getPage(), + $tagFilter->getSizePage(), + $this->countTagService->__invoke($tagFilter), + $this->listTagService->__invoke($tagFilter) + ))->toArray(); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Actions/UpdateTagAction.php b/pandora_console/include/lib/Modules/Tags/Actions/UpdateTagAction.php new file mode 100644 index 0000000000..62fa12bc12 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Actions/UpdateTagAction.php @@ -0,0 +1,19 @@ +updateTagService->__invoke($tag, $oldTag); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Controllers/CreateTagController.php b/pandora_console/include/lib/Modules/Tags/Controllers/CreateTagController.php new file mode 100644 index 0000000000..5aeea46ba7 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Controllers/CreateTagController.php @@ -0,0 +1,47 @@ +fromRequest($request, Tag::class); + + $this->acl->validate(0, 'UM', ' tried to manage tag'); + + $result = $this->createTagAction->__invoke($tag); + + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Controllers/DeleteTagController.php b/pandora_console/include/lib/Modules/Tags/Controllers/DeleteTagController.php new file mode 100644 index 0000000000..d2b44719fd --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Controllers/DeleteTagController.php @@ -0,0 +1,47 @@ +getParam($request, 'idTag'); + $tag = $this->getTagAction->__invoke($idTag); + + $this->acl->validate(0, 'UM', ' tried to manage tag'); + + $result = $this->deleteTagAction->__invoke($tag); + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Controllers/GetTagController.php b/pandora_console/include/lib/Modules/Tags/Controllers/GetTagController.php new file mode 100644 index 0000000000..f450a14b63 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Controllers/GetTagController.php @@ -0,0 +1,44 @@ +getParam($request, 'idTag'); + + $this->acl->validate(0, 'UM', ' tried to manage tag'); + + $result = $this->getTagAction->__invoke($idTag); + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Controllers/ListTagController.php b/pandora_console/include/lib/Modules/Tags/Controllers/ListTagController.php new file mode 100644 index 0000000000..bb8b4058e2 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Controllers/ListTagController.php @@ -0,0 +1,74 @@ +fromRequest($request, TagFilter::class); + + $this->acl->validate(0, 'UM', ' tried to manage tag'); + + $result = $this->listTagAction->__invoke($tagFilter); + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Controllers/UpdateTagController.php b/pandora_console/include/lib/Modules/Tags/Controllers/UpdateTagController.php new file mode 100644 index 0000000000..416ab76434 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Controllers/UpdateTagController.php @@ -0,0 +1,52 @@ +getParam($request, 'idTag'); + $tag = $this->getTagAction->__invoke($idTag); + + $oldTag = clone $tag; + $params = $this->extractParams($request); + $tag->fromArray($params); + + $this->acl->validate(0, 'UM', ' tried to manage tag'); + + $result = $this->updateTagAction->__invoke($tag, $oldTag); + return $this->getResponse($response, $result); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Entities/Tag.php b/pandora_console/include/lib/Modules/Tags/Entities/Tag.php new file mode 100644 index 0000000000..943e11d398 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Entities/Tag.php @@ -0,0 +1,218 @@ + 1, + 'previousName' => 1, + ]; + } + + public function jsonSerialize(): mixed + { + return [ + 'idTag' => $this->getIdTag(), + 'name' => $this->getName(), + 'description' => $this->getDescription(), + 'url' => $this->getUrl(), + 'mail' => $this->getMail(), + 'phone' => $this->getPhone(), + ]; + } + + public function getValidations(): array + { + return [ + 'idTag' => [ + Validator::INTEGER, + Validator::GREATERTHAN, + ], + 'name' => Validator::STRING, + 'description' => Validator::STRING, + 'url' => Validator::STRING, + 'mail' => Validator::MAIL, + 'phone' => Validator::STRING, + 'previousName' => Validator::STRING, + ]; + } + + public function validateFields(array $filters): array + { + return (new Validator())->validate($filters); + } + + public function getIdTag(): ?int + { + return $this->idTag; + } + public function setIdTag(?int $idTag): self + { + $this->idTag = $idTag; + return $this; + } + + public function getName(): ?string + { + return $this->name; + } + public function setName(?string $name): self + { + $this->name = $name; + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + public function setDescription(?string $description): self + { + $this->description = $description; + return $this; + } + + public function getUrl(): ?string + { + return $this->url; + } + public function setUrl(?string $url): self + { + $this->url = $url; + return $this; + } + + public function getMail(): ?string + { + return $this->mail; + } + public function setMail(?string $mail): self + { + $this->mail = $mail; + return $this; + } + + public function getPhone(): ?string + { + return $this->phone; + } + public function setPhone(?string $phone): self + { + $this->phone = $phone; + return $this; + } + + public function getPreviousName(): ?string + { + return $this->previousName; + } + public function setPreviousName(?string $previousName): self + { + $this->previousName = $previousName; + return $this; + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Entities/TagDataMapper.php b/pandora_console/include/lib/Modules/Tags/Entities/TagDataMapper.php new file mode 100644 index 0000000000..065d1a9a50 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Entities/TagDataMapper.php @@ -0,0 +1,62 @@ +builder->build(new Tag(), [ + 'idTag' => $data[self::ID_TAG], + 'name' => $this->repository->safeOutput($data[self::NAME]), + 'description' => $this->repository->safeOutput($data[self::DESCRIPTION]), + 'url' => $this->repository->safeOutput($data[self::URL]), + 'mail' => $this->repository->safeOutput($data[self::MAIL]), + 'phone' => $this->repository->safeOutput($data[self::PHONE]), + 'previousName' => $this->repository->safeOutput($data[self::PREVIOUS_NAME]), + ]); + } + + public function toDatabase(MappeableInterface $data): array + { + /** @var Tag $data */ + return [ + self::ID_TAG => $data->getIdTag(), + self::NAME => $this->repository->safeInput($data->getName()), + self::DESCRIPTION => $this->repository->safeInput($data->getDescription()), + self::URL => $this->repository->safeInput($data->getUrl()), + self::MAIL => $this->repository->safeInput($data->getMail()), + self::PHONE => $this->repository->safeInput($data->getPhone()), + self::PREVIOUS_NAME => $this->repository->safeInput($data->getPreviousName()), + ]; + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Entities/TagFilter.php b/pandora_console/include/lib/Modules/Tags/Entities/TagFilter.php new file mode 100644 index 0000000000..c5e20b773f --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Entities/TagFilter.php @@ -0,0 +1,123 @@ +setDefaultFieldOrder(TagDataMapper::NAME); + $this->setDefaultDirectionOrder($this::ASC); + $this->setEntityFilter(new Tag()); + } + + public function fieldsTranslate(): array + { + return [ + 'idTag' => TagDataMapper::ID_TAG, + 'name' => TagDataMapper::NAME, + 'description' => TagDataMapper::DESCRIPTION, + 'url' => TagDataMapper::URL, + 'mail' => TagDataMapper::MAIL, + 'phone' => TagDataMapper::PHONE, + 'previousName' => TagDataMapper::PREVIOUS_NAME, + ]; + } + + public function fieldsReadOnly(): array + { + return ['previousName' => 1]; + } + + public function jsonSerialize(): mixed + { + return [ + 'freeSearch' => $this->getFreeSearch(), + ]; + } + + public function getValidations(): array + { + $validations = []; + if($this->getEntityFilter() !== null) { + $validations = $this->getEntityFilter()->getValidations(); + } + $validations['freeSearch'] = Validator::STRING; + return $validations; + } + + public function validateFields(array $filters): array + { + return (new Validator())->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 [TagDataMapper::NAME, TagDataMapper::DESCRIPTION]; + } + +} diff --git a/pandora_console/include/lib/Modules/Tags/Repositories/TagRepository.php b/pandora_console/include/lib/Modules/Tags/Repositories/TagRepository.php new file mode 100644 index 0000000000..2f7a420898 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Repositories/TagRepository.php @@ -0,0 +1,65 @@ +repository->__list( + $tagFilter, + $this->tagDataMapper + ); + } + + public function count(TagFilter $tagFilter): int + { + return $this->repository->__count( + $tagFilter, + $this->tagDataMapper + ); + } + + public function getOne(TagFilter $tagFilter): Tag + { + return $this->repository->__getOne( + $tagFilter, + $this->tagDataMapper + ); + } + + public function create(Tag $tag): Tag + { + $id = $this->repository->__create($tag, $this->tagDataMapper); + return $tag->setIdTag($id); + } + + public function update(Tag $tag): Tag + { + return $this->repository->__update( + $tag, + $this->tagDataMapper, + $tag->getIdTag() + ); + } + + public function delete(int $id): void + { + $this->repository->__delete($id, $this->tagDataMapper); + } + +} diff --git a/pandora_console/include/lib/Modules/Tags/Services/CountTagService.php b/pandora_console/include/lib/Modules/Tags/Services/CountTagService.php new file mode 100644 index 0000000000..bac59b61c4 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Services/CountTagService.php @@ -0,0 +1,19 @@ +tagRepository->count($tagFilter); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Services/CreateTagService.php b/pandora_console/include/lib/Modules/Tags/Services/CreateTagService.php new file mode 100644 index 0000000000..6a67a5e3d9 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Services/CreateTagService.php @@ -0,0 +1,33 @@ +tagValidation->__invoke($tag); + + $tag = $this->tagRepository->create($tag); + + $this->audit->write( + AUDIT_LOG_TAG_MANAGEMENT, + 'Create tag '.$tag->getName(), + json_encode($tag->toArray()) + ); + + return $tag; + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Services/DeleteTagService.php b/pandora_console/include/lib/Modules/Tags/Services/DeleteTagService.php new file mode 100644 index 0000000000..617891eebf --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Services/DeleteTagService.php @@ -0,0 +1,29 @@ +getIdTag(); + $nameTag = $tag->getName(); + $this->tagRepository->delete($idTag); + + $this->audit->write( + AUDIT_LOG_TAG_MANAGEMENT, + 'Deleted tag '.$nameTag + ); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Services/ExistNameTagService.php b/pandora_console/include/lib/Modules/Tags/Services/ExistNameTagService.php new file mode 100644 index 0000000000..0ec14844a8 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Services/ExistNameTagService.php @@ -0,0 +1,31 @@ +getEntityFilter(); + $entityFilter->setName($name); + + try { + $this->tagRepository->getOne($tagFilter); + return true; + } catch (NotFoundException) { + return false; + } + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Services/GetTagService.php b/pandora_console/include/lib/Modules/Tags/Services/GetTagService.php new file mode 100644 index 0000000000..ef3bf6ed04 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Services/GetTagService.php @@ -0,0 +1,25 @@ +getEntityFilter(); + $entityFilter->setIdTag($idTag); + + return $this->tagRepository->getOne($tagFilter); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Services/ListTagService.php b/pandora_console/include/lib/Modules/Tags/Services/ListTagService.php new file mode 100644 index 0000000000..14daf812a2 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Services/ListTagService.php @@ -0,0 +1,19 @@ +tagRepository->list($tagFilter); + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Services/UpdateTagService.php b/pandora_console/include/lib/Modules/Tags/Services/UpdateTagService.php new file mode 100644 index 0000000000..2f3770c948 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Services/UpdateTagService.php @@ -0,0 +1,33 @@ +tagValidation->__invoke($tag, $oldTag); + + $tag = $this->tagRepository->update($tag); + + $this->audit->write( + AUDIT_LOG_TAG_MANAGEMENT, + 'Update tag '.$tag->getName(), + json_encode($tag->toArray()) + ); + + return $tag; + } +} diff --git a/pandora_console/include/lib/Modules/Tags/Validations/TagValidation.php b/pandora_console/include/lib/Modules/Tags/Validations/TagValidation.php new file mode 100644 index 0000000000..1bc998a990 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/Validations/TagValidation.php @@ -0,0 +1,34 @@ +getName()) { + throw new BadRequestException(__('Name is missing')); + } + + if($oldTag === null || $oldTag->getName() !== $tag->getName()) { + if($this->existNameTagService->__invoke($tag->getName()) === true) { + throw new BadRequestException( + __('Name %s is already exists', $tag->getName()) + ); + } + } + + //if($tag->getIsAgentView() === null) { + // $tag->setIsAgentView(false); + //} + } +} diff --git a/pandora_console/include/lib/Modules/Tags/routes.php b/pandora_console/include/lib/Modules/Tags/routes.php new file mode 100644 index 0000000000..c4b6e75ea6 --- /dev/null +++ b/pandora_console/include/lib/Modules/Tags/routes.php @@ -0,0 +1,16 @@ +map(['GET', 'POST'], '/tag/list', ListTagController::class); + $app->get('/tag/{idTag}', GetTagController::class); + $app->post('/tag', CreateTagController::class); + $app->put('/tag/{idTag}', UpdateTagController::class); + $app->delete('/tag/{idTag}', DeleteTagController::class); +}; diff --git a/pandora_console/include/lib/Modules/Users/UserProfiles/Validations/UserProfileValidation.php b/pandora_console/include/lib/Modules/Users/UserProfiles/Validations/UserProfileValidation.php index cc104d34a0..c003af3339 100644 --- a/pandora_console/include/lib/Modules/Users/UserProfiles/Validations/UserProfileValidation.php +++ b/pandora_console/include/lib/Modules/Users/UserProfiles/Validations/UserProfileValidation.php @@ -2,11 +2,12 @@ namespace PandoraFMS\Modules\Users\UserProfiles\Validations; -//use PandoraFMS\Modules\Groups\Services\GetGroupService; +use PandoraFMS\Modules\Groups\Services\GetGroupService; use PandoraFMS\Modules\Profiles\Services\GetProfileService; use PandoraFMS\Modules\Shared\Exceptions\BadRequestException; use PandoraFMS\Modules\Shared\Services\Config; use PandoraFMS\Modules\Shared\Services\ValidateAclSystem; +use PandoraFMS\Modules\Tags\Services\GetTagService; use PandoraFMS\Modules\Users\Services\GetUserService; use PandoraFMS\Modules\Users\UserProfiles\Entities\UserProfile; use PandoraFMS\Modules\Users\UserProfiles\Services\ExistUserProfileService; @@ -14,10 +15,11 @@ use PandoraFMS\Modules\Users\UserProfiles\Services\ExistUserProfileService; final class UserProfileValidation { public function __construct( - //private GetGroupService $getGroupService, + private GetGroupService $getGroupService, private GetUserService $getUserService, private GetProfileService $getProfileService, private ExistUserProfileService $existUserProfileService, + private GetTagService $getTagService, private ValidateAclSystem $acl, private Config $config ) { @@ -82,9 +84,9 @@ final class UserProfileValidation $this->getProfileService->__invoke($idProfile); } - private function validateGroup(int $idGroup): void + protected function validateGroup(int $idGroup): void { - //$this->getGroupService->__invoke($idGroup); + $this->getGroupService->__invoke($idGroup); } protected function validatePolicy(int $idPolicy): void @@ -97,11 +99,8 @@ final class UserProfileValidation protected function validateTags(array $tags): void { - // TODO: create new service for this. foreach ($tags as $tag) { - if (! (bool) \tags_get_name($tag)) { - throw new BadRequestException(__('Invalid id tag:, %', $tag)); - } + $this->getTagService->__invoke((int) $tag); } } }