From fdc6aa707a44774f92561c5a270ddc023a915dc6 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Tue, 13 Aug 2024 11:13:40 +0200 Subject: [PATCH 1/3] Add validation service --- lib/Controller/PublicationsController.php | 5 +- lib/Service/ValidationService.php | 92 +++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 lib/Service/ValidationService.php diff --git a/lib/Controller/PublicationsController.php b/lib/Controller/PublicationsController.php index 3111b16e..d4983142 100644 --- a/lib/Controller/PublicationsController.php +++ b/lib/Controller/PublicationsController.php @@ -9,6 +9,7 @@ use OCA\OpenCatalogi\Service\ElasticSearchService; use OCA\OpenCatalogi\Service\ObjectService; use OCA\OpenCatalogi\Service\SearchService; +use OCA\OpenCatalogi\Service\ValidationService; use OCP\AppFramework\Controller; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http\TemplateResponse; @@ -202,7 +203,7 @@ public function show(string|int $id, ObjectService $objectService): JSONResponse * @NoAdminRequired * @NoCSRFRequired */ - public function create(ObjectService $objectService, ElasticSearchService $elasticSearchService): JSONResponse + public function create(ObjectService $objectService, ElasticSearchService $elasticSearchService, ValidationService $validationService): JSONResponse { $data = $this->request->getParams(); @@ -214,6 +215,8 @@ public function create(ObjectService $objectService, ElasticSearchService $elast } } + $data = $validationService->validatePublication($data); + if($this->config->hasKey($this->appName, 'mongoStorage') === false || $this->config->getValueString($this->appName, 'mongoStorage') !== '1' ) { diff --git a/lib/Service/ValidationService.php b/lib/Service/ValidationService.php new file mode 100644 index 00000000..19269e30 --- /dev/null +++ b/lib/Service/ValidationService.php @@ -0,0 +1,92 @@ +appName = 'opencatalogi'; + + $this->mongodbConfig = [ + 'base_uri' => $this->config->getValueString(app: $this->appName, key: 'mongodbLocation'), + 'headers' => ['api-key' => $this->config->getValueString(app: $this->appName, key: 'mongodbKey')], + 'mongodbCluster' => $this->config->getValueString(app: $this->appName, key:'mongodbCluster') + ]; + + } + + /** + * @return array The mongodb config. + */ + public function getMongodbConfig(): array + { + return $this->mongodbConfig; + } + + /** + * Fetches a catalog from either the local database or mongodb + * + * @param string $id The id of the catalog to be fetched. + * @return array The JSON Serialised catalog. + * + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function getCatalog (string $id): array + { + if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') === false + || $this->config->getValueString(app: $this->appName, key: 'mongoStorage') !== '1' + ) { + $filter = ['id' => $id, '_schema' => 'catalog']; + + return $this->objectService->findObject(filters: $filter, config: $this->getMongodbConfig()); + } + + return $this->catalogMapper->find(id: $id)->jsonSerialize(); + } + + /** + * Validates a publication against the rules set for the publication. + * + * @param array $publication The publication to be validated. + * @return array The publication after it has been validated. + * + * @throws OCSBadRequestException Thrown if the object does not validate + */ + public function validatePublication(array $publication): array + { + $catalogId = $publication['catalog']; + $metadata = $publication['metadata']; + + $catalog = $this->getCatalog($catalogId); + + if(in_array(needle: $metadata, haystack: $catalog['metadata']) === false) { + throw new OCSBadRequestException(message: 'Given metadata object not present in catalog'); + } + + return $publication; + } + +} From 9fd944727272307801f44b3965e2ead330269519 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Tue, 13 Aug 2024 11:53:15 +0200 Subject: [PATCH 2/3] Fixes from local tests --- lib/Controller/PublicationsController.php | 7 ++++++- lib/Service/ValidationService.php | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/Controller/PublicationsController.php b/lib/Controller/PublicationsController.php index d4983142..7c86e908 100644 --- a/lib/Controller/PublicationsController.php +++ b/lib/Controller/PublicationsController.php @@ -14,6 +14,7 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\IAppConfig; use OCP\IRequest; use Symfony\Component\Uid\Uuid; @@ -215,7 +216,11 @@ public function create(ObjectService $objectService, ElasticSearchService $elast } } - $data = $validationService->validatePublication($data); + try { + $data = $validationService->validatePublication($data); + } catch (OCSBadRequestException $exception) { + return new JSONResponse(data: ['message' => $exception->getMessage()], statusCode: 400); + } if($this->config->hasKey($this->appName, 'mongoStorage') === false || $this->config->getValueString($this->appName, 'mongoStorage') !== '1' diff --git a/lib/Service/ValidationService.php b/lib/Service/ValidationService.php index 19269e30..8f68ff69 100644 --- a/lib/Service/ValidationService.php +++ b/lib/Service/ValidationService.php @@ -56,8 +56,8 @@ public function getMongodbConfig(): array */ public function getCatalog (string $id): array { - if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') === false - || $this->config->getValueString(app: $this->appName, key: 'mongoStorage') !== '1' + if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') !== false + || $this->config->getValueString(app: $this->appName, key: 'mongoStorage') === '1' ) { $filter = ['id' => $id, '_schema' => 'catalog']; @@ -77,7 +77,7 @@ public function getCatalog (string $id): array */ public function validatePublication(array $publication): array { - $catalogId = $publication['catalog']; + $catalogId = $publication['catalogi']; $metadata = $publication['metadata']; $catalog = $this->getCatalog($catalogId); From 8a8d3bf0f5591fa7f5da3c58364f568925cfa6b5 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 14 Aug 2024 16:33:00 +0200 Subject: [PATCH 3/3] Updates on listing and metadata --- lib/Db/Listing.php | 26 ++++++++++++++------------ lib/Db/MetaData.php | 1 + lib/Service/ValidationService.php | 6 +++++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/Db/Listing.php b/lib/Db/Listing.php index a26a5a48..a617ef92 100644 --- a/lib/Db/Listing.php +++ b/lib/Db/Listing.php @@ -9,19 +9,20 @@ class Listing extends Entity implements JsonSerializable { - protected ?string $title = null; - protected ?string $reference = null; - protected ?string $summary = null; - protected ?string $description = null; - protected ?string $search = null; - protected ?string $directory = null; - protected ?string $metadata = null; - protected ?string $catalogId = null; - protected ?string $status = null; + protected ?string $title = null; + protected ?string $reference = null; + protected ?string $summary = null; + protected ?string $description = null; + protected ?string $search = null; + protected ?string $directory = null; + protected ?string $metadata = null; + protected ?string $catalogId = null; + protected ?string $status = null; + protected ?int $statusCode = null; protected ?DateTime $lastSync = null; - protected ?bool $default = false; - protected ?bool $available = false; - protected ?string $organisation = null; + protected ?bool $default = false; + protected ?bool $available = false; + protected ?string $organisation = null; public function __construct() { $this->addType(fieldName: 'title', type: 'string'); @@ -32,6 +33,7 @@ public function __construct() { $this->addType(fieldName: 'metadata', type: 'string'); $this->addType(fieldName: 'catalogId', type: 'string'); $this->addType(fieldName: 'status', type: 'string'); + $this->addType(fieldName: 'statusCode', type: 'integer'); $this->addType(fieldName: 'lastSync', type: 'datetime'); $this->addType(fieldName: 'default', type: 'boolean'); $this->addType(fieldName: 'available', type: 'boolean'); diff --git a/lib/Db/MetaData.php b/lib/Db/MetaData.php index 1f7d3433..206d0a4d 100644 --- a/lib/Db/MetaData.php +++ b/lib/Db/MetaData.php @@ -14,6 +14,7 @@ class MetaData extends Entity implements JsonSerializable protected ?string $description = null; protected ?array $required = []; protected ?array $properties = []; + protected ?array $archive = []; protected ?string $source = null; public function __construct() { diff --git a/lib/Service/ValidationService.php b/lib/Service/ValidationService.php index 8f68ff69..66a6bbec 100644 --- a/lib/Service/ValidationService.php +++ b/lib/Service/ValidationService.php @@ -78,14 +78,18 @@ public function getCatalog (string $id): array public function validatePublication(array $publication): array { $catalogId = $publication['catalogi']; - $metadata = $publication['metadata']; + $metadata = $publication['metaData']; $catalog = $this->getCatalog($catalogId); +// var_dump($catalog['metadata'], $metadata, in_array(needle: $metadata, haystack: $catalog['metadata'])); + if(in_array(needle: $metadata, haystack: $catalog['metadata']) === false) { throw new OCSBadRequestException(message: 'Given metadata object not present in catalog'); } +// var_dump($publication); + return $publication; }