Reorganized API exposed entities

This commit is contained in:
Bernd Bestel
2020-12-12 10:10:21 +01:00
parent f07a21b00b
commit d3883ba93a
4 changed files with 34 additions and 45 deletions

View File

@@ -11,7 +11,7 @@ class GenericEntityApiController extends BaseApiController
{ {
User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); 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'])) if ($this->IsEntityWithEditRequiresAdmin($args['entity']))
{ {
@@ -50,7 +50,7 @@ class GenericEntityApiController extends BaseApiController
{ {
User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); 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'])) if ($this->IsEntityWithEditRequiresAdmin($args['entity']))
{ {
@@ -73,7 +73,7 @@ class GenericEntityApiController extends BaseApiController
{ {
User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); 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'])) 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) 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']); $userfields = $this->getUserfieldsService()->GetValues($args['entity'], $args['objectId']);
@@ -155,7 +155,7 @@ class GenericEntityApiController extends BaseApiController
$object->userfields = $userfieldKeyValuePairs; $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); return $this->ApiResponse($response, $objects);
} }
@@ -206,12 +206,12 @@ class GenericEntityApiController extends BaseApiController
private function IsEntityWithEditRequiresAdmin($entity) 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) private function IsEntityWithNoEdit($entity)
@@ -219,7 +219,7 @@ class GenericEntityApiController extends BaseApiController
return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntityNoEdit->enum); 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); return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntity->enum);
} }

View File

@@ -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); return $this->ApiResponse($response, $spec);
} }

View File

@@ -194,7 +194,7 @@
"required": true, "required": true,
"description": "A valid entity name", "description": "A valid entity name",
"schema": { "schema": {
"$ref": "#/components/internalSchemas/ExposedEntityButNoListing" "$ref": "#/components/internalSchemas/ExposedEntity_NotIncludingNotListable"
} }
}, },
{ {
@@ -367,7 +367,7 @@
"required": true, "required": true,
"description": "A valid entity name", "description": "A valid entity name",
"schema": { "schema": {
"$ref": "#/components/internalSchemas/ExposedEntityButNoListing" "$ref": "#/components/internalSchemas/ExposedEntity_NotIncludingNotListable"
} }
}, },
{ {
@@ -3831,34 +3831,7 @@
"stock_log" "stock_log"
] ]
}, },
"ExposedEntityButNoListing": { "ExposedEntityNoListing": {
"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": {
"type": "string", "type": "string",
"enum": [ "enum": [
"api_keys" "api_keys"
@@ -3867,7 +3840,14 @@
"ExposedEntityNoEdit": { "ExposedEntityNoEdit": {
"type": "string", "type": "string",
"enum": [ "enum": [
"stock_log" "stock_log",
"api_keys"
]
},
"ExposedEntityEditRequiresAdmin": {
"type": "string",
"enum": [
"api_keys"
] ]
}, },
"StockTransactionType": { "StockTransactionType": {

View File

@@ -26,7 +26,7 @@ class UserfieldsService extends BaseService
public function GetAllValues($entity) public function GetAllValues($entity)
{ {
if (!$this->IsValidEntity($entity)) if (!$this->IsValidExposedEntity($entity))
{ {
throw new \Exception('Entity does not exist or is not exposed'); throw new \Exception('Entity does not exist or is not exposed');
} }
@@ -60,7 +60,7 @@ class UserfieldsService extends BaseService
public function GetFields($entity) public function GetFields($entity)
{ {
if (!$this->IsValidEntity($entity)) if (!$this->IsValidExposedEntity($entity))
{ {
throw new \Exception('Entity does not exist or is not exposed'); throw new \Exception('Entity does not exist or is not exposed');
} }
@@ -70,7 +70,7 @@ class UserfieldsService extends BaseService
public function GetValues($entity, $objectId) public function GetValues($entity, $objectId)
{ {
if (!$this->IsValidEntity($entity)) if (!$this->IsValidExposedEntity($entity))
{ {
throw new \Exception('Entity does not exist or is not exposed'); 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) 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'); throw new \Exception('Entity does not exist or is not exposed');
} }
@@ -139,7 +139,7 @@ class UserfieldsService extends BaseService
return $this->OpenApiSpec; return $this->OpenApiSpec;
} }
private function IsValidEntity($entity) private function IsValidExposedEntity($entity)
{ {
return in_array($entity, $this->GetEntities()); return in_array($entity, $this->GetEntities());
} }