From 667997c7ca6c445001d56f70205b6cf13c6b7343 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 9 Oct 2024 22:50:29 +1300 Subject: [PATCH] Merge pull request #52 from utopia-php/validate-uid Validate UID --- src/Migration/Destinations/Appwrite.php | 37 ++++++++++++++++++++++++- src/Migration/Sources/Appwrite.php | 10 +++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 4123d87..dc04945 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -25,6 +25,7 @@ use Utopia\Database\Query; use Utopia\Database\Validator\Index as IndexValidator; use Utopia\Database\Validator\Structure; +use Utopia\Database\Validator\UID; use Utopia\Migration\Destination; use Utopia\Migration\Exception; use Utopia\Migration\Resource; @@ -302,7 +303,7 @@ public function importDatabaseResource(Resource $resource, bool $isLast): Resour /** * @throws AuthorizationException * @throws StructureException - * @throws DatabaseException + * @throws DatabaseException|Exception */ protected function createDatabase(Database $resource): bool { @@ -310,6 +311,17 @@ protected function createDatabase(Database $resource): bool ? ID::unique() : $resource->getId(); + $validator = new UID(); + + if ($validator->isValid($resourceId)) { + throw new Exception( + resourceName: $resource->getName(), + resourceGroup: $resource->getGroup(), + resourceId: $resource->getId(), + message: $validator->getDescription(), + ); + } + $resource->setId($resourceId); $database = $this->database->createDocument('databases', new UtopiaDocument([ @@ -354,6 +366,17 @@ protected function createCollection(Collection $resource): bool ? ID::unique() : $resource->getId(); + $validator = new UID(); + + if ($validator->isValid($resourceId)) { + throw new Exception( + resourceName: $resource->getName(), + resourceGroup: $resource->getGroup(), + resourceId: $resource->getId(), + message: $validator->getDescription(), + ); + } + $resource->setId($resourceId); $database = $this->database->getDocument( @@ -866,9 +889,21 @@ protected function createIndex(Index $resource): bool * @throws AuthorizationException * @throws DatabaseException * @throws StructureException + * @throws Exception */ protected function createDocument(Document $resource, bool $isLast): bool { + $validator = new UID(); + + if ($validator->isValid($resource->getId())) { + throw new Exception( + resourceName: $resource->getName(), + resourceGroup: $resource->getGroup(), + resourceId: $resource->getId(), + message: $validator->getDescription(), + ); + } + // Check if document has already been created $exists = \array_key_exists( $resource->getId(), diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index 44392d5..ca14951 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -541,6 +541,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void previous: $e ) ); + + return; } try { @@ -557,6 +559,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void previous: $e ) ); + + return; } try { @@ -573,6 +577,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void previous: $e ) ); + + return; } try { @@ -589,6 +595,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void previous: $e ) ); + + return; } try { @@ -605,6 +613,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void previous: $e ) ); + + return; } }