From d3883ba93aed6eb0d4957e78fb961cff52566c20 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sat, 12 Dec 2020 10:10:21 +0100 Subject: [PATCH] Reorganized API exposed entities --- controllers/GenericEntityApiController.php | 18 +++++----- controllers/OpenApiController.php | 9 +++++ grocy.openapi.json | 42 ++++++---------------- services/UserfieldsService.php | 10 +++--- 4 files changed, 34 insertions(+), 45 deletions(-) diff --git a/controllers/GenericEntityApiController.php b/controllers/GenericEntityApiController.php index d1f4033c..cc0de871 100644 --- a/controllers/GenericEntityApiController.php +++ b/controllers/GenericEntityApiController.php @@ -11,7 +11,7 @@ class GenericEntityApiController extends BaseApiController { User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); - if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithNoEdit($args['entity'])) + if ($this->IsValidExposedEntity($args['entity']) && !$this->IsEntityWithNoEdit($args['entity'])) { if ($this->IsEntityWithEditRequiresAdmin($args['entity'])) { @@ -50,7 +50,7 @@ class GenericEntityApiController extends BaseApiController { User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); - if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithNoEdit($args['entity'])) + if ($this->IsValidExposedEntity($args['entity']) && !$this->IsEntityWithNoEdit($args['entity'])) { if ($this->IsEntityWithEditRequiresAdmin($args['entity'])) { @@ -73,7 +73,7 @@ class GenericEntityApiController extends BaseApiController { User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); - if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithNoEdit($args['entity'])) + if ($this->IsValidExposedEntity($args['entity']) && !$this->IsEntityWithNoEdit($args['entity'])) { if ($this->IsEntityWithEditRequiresAdmin($args['entity'])) { @@ -108,7 +108,7 @@ class GenericEntityApiController extends BaseApiController public function GetObject(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { - if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity'])) + if ($this->IsValidExposedEntity($args['entity']) && !$this->IsEntityWithNoListing($args['entity'])) { $userfields = $this->getUserfieldsService()->GetValues($args['entity'], $args['objectId']); @@ -155,7 +155,7 @@ class GenericEntityApiController extends BaseApiController $object->userfields = $userfieldKeyValuePairs; } - if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity'])) + if ($this->IsValidExposedEntity($args['entity']) && !$this->IsEntityWithNoListing($args['entity'])) { return $this->ApiResponse($response, $objects); } @@ -206,12 +206,12 @@ class GenericEntityApiController extends BaseApiController private function IsEntityWithEditRequiresAdmin($entity) { - return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->EntityEditRequiresAdmin->enum); + return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntityEditRequiresAdmin->enum); } - private function IsEntityWithPreventedListing($entity) + private function IsEntityWithNoListing($entity) { - return !in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntityButNoListing->enum); + return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntityNoListing->enum); } private function IsEntityWithNoEdit($entity) @@ -219,7 +219,7 @@ class GenericEntityApiController extends BaseApiController return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntityNoEdit->enum); } - private function IsValidEntity($entity) + private function IsValidExposedEntity($entity) { return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntity->enum); } diff --git a/controllers/OpenApiController.php b/controllers/OpenApiController.php index 300bedc1..5dd21c93 100644 --- a/controllers/OpenApiController.php +++ b/controllers/OpenApiController.php @@ -45,6 +45,15 @@ class OpenApiController extends BaseApiController } } + $spec->components->internalSchemas->ExposedEntity_NotIncludingNotListable = clone $spec->components->internalSchemas->StringEnumTemplate; + foreach ($spec->components->internalSchemas->ExposedEntity->enum as $value) + { + if (!in_array($value, $spec->components->internalSchemas->ExposedEntityNoListing->enum)) + { + array_push($spec->components->internalSchemas->ExposedEntity_NotIncludingNotListable->enum, $value); + } + } + return $this->ApiResponse($response, $spec); } diff --git a/grocy.openapi.json b/grocy.openapi.json index 5a58b371..672d9fb5 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -194,7 +194,7 @@ "required": true, "description": "A valid entity name", "schema": { - "$ref": "#/components/internalSchemas/ExposedEntityButNoListing" + "$ref": "#/components/internalSchemas/ExposedEntity_NotIncludingNotListable" } }, { @@ -367,7 +367,7 @@ "required": true, "description": "A valid entity name", "schema": { - "$ref": "#/components/internalSchemas/ExposedEntityButNoListing" + "$ref": "#/components/internalSchemas/ExposedEntity_NotIncludingNotListable" } }, { @@ -3831,34 +3831,7 @@ "stock_log" ] }, - "ExposedEntityButNoListing": { - "type": "string", - "enum": [ - "products", - "product_barcodes", - "chores", - "batteries", - "locations", - "quantity_units", - "quantity_unit_conversions", - "shopping_list", - "shopping_lists", - "shopping_locations", - "recipes", - "recipes_pos", - "recipes_nestings", - "tasks", - "task_categories", - "product_groups", - "equipment", - "userfields", - "userentities", - "userobjects", - "meal_plan", - "stock_log" - ] - }, - "EntityEditRequiresAdmin": { + "ExposedEntityNoListing": { "type": "string", "enum": [ "api_keys" @@ -3867,7 +3840,14 @@ "ExposedEntityNoEdit": { "type": "string", "enum": [ - "stock_log" + "stock_log", + "api_keys" + ] + }, + "ExposedEntityEditRequiresAdmin": { + "type": "string", + "enum": [ + "api_keys" ] }, "StockTransactionType": { diff --git a/services/UserfieldsService.php b/services/UserfieldsService.php index bed0e326..1cdce8d9 100644 --- a/services/UserfieldsService.php +++ b/services/UserfieldsService.php @@ -26,7 +26,7 @@ class UserfieldsService extends BaseService public function GetAllValues($entity) { - if (!$this->IsValidEntity($entity)) + if (!$this->IsValidExposedEntity($entity)) { throw new \Exception('Entity does not exist or is not exposed'); } @@ -60,7 +60,7 @@ class UserfieldsService extends BaseService public function GetFields($entity) { - if (!$this->IsValidEntity($entity)) + if (!$this->IsValidExposedEntity($entity)) { throw new \Exception('Entity does not exist or is not exposed'); } @@ -70,7 +70,7 @@ class UserfieldsService extends BaseService public function GetValues($entity, $objectId) { - if (!$this->IsValidEntity($entity)) + if (!$this->IsValidExposedEntity($entity)) { throw new \Exception('Entity does not exist or is not exposed'); } @@ -88,7 +88,7 @@ class UserfieldsService extends BaseService public function SetValues($entity, $objectId, $userfields) { - if (!$this->IsValidEntity($entity)) + if (!$this->IsValidExposedEntity($entity)) { throw new \Exception('Entity does not exist or is not exposed'); } @@ -139,7 +139,7 @@ class UserfieldsService extends BaseService return $this->OpenApiSpec; } - private function IsValidEntity($entity) + private function IsValidExposedEntity($entity) { return in_array($entity, $this->GetEntities()); }