From b252fe847f30154b7ea6536d0a267e1fac34fd64 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 6 Apr 2023 00:54:44 +1200 Subject: [PATCH] Add custom delete restricted error --- app/config/errors.php | 5 +++++ app/controllers/api/databases.php | 18 ++++++++++++------ composer.lock | 8 ++++---- src/Appwrite/Extend/Exception.php | 1 + 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 10e1b308f2..95fc449cc5 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -413,6 +413,11 @@ return [ 'description' => 'Remote document is newer than local.', 'code' => 409, ], + Exception::DOCUMENT_DELETE_RESTRICTED => [ + 'name' => Exception::DOCUMENT_DELETE_RESTRICTED, + 'description' => 'Document cannot be deleted because it is referenced by another document.', + 'code' => 403, + ], /** Attributes */ Exception::ATTRIBUTE_NOT_FOUND => [ diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 930392785c..7372c12423 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -30,6 +30,7 @@ use Utopia\Database\Validator\UID; use Utopia\Database\Exception\Authorization as AuthorizationException; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Limit as LimitException; +use Utopia\Database\Exception\Restricted as RestrictedException; use Utopia\Database\Exception\Structure as StructureException; use Utopia\Locale\Locale; use Appwrite\Auth\Auth; @@ -2273,7 +2274,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/attributes/:key } if ($relatedAttribute->getAttribute('status') === 'available') { - $relatedAttribute = $dbForProject->updateDocument('attributes', $relatedAttribute->getId(), $relatedAttribute->setAttribute('status', 'deleting')); + $dbForProject->updateDocument('attributes', $relatedAttribute->getId(), $relatedAttribute->setAttribute('status', 'deleting')); } $dbForProject->deleteCachedDocument('database_' . $db->getInternalId(), $options['relatedCollection']); @@ -3538,11 +3539,16 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu $checkPermissions($collection, $document); - Authorization::skip(fn () => $dbForProject->withRequestTimestamp($requestTimestamp, fn() => - $dbForProject->deleteDocument( - 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), - $documentId - ))); + Authorization::skip(fn () => $dbForProject->withRequestTimestamp($requestTimestamp, function () use ($dbForProject, $database, $collection, $documentId) { + try { + $dbForProject->deleteDocument( + 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), + $documentId + ); + } catch (RestrictedException) { + throw new Exception(Exception::DOCUMENT_DELETE_RESTRICTED); + } + })); $dbForProject->deleteCachedDocument( 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), diff --git a/composer.lock b/composer.lock index c13706c0a2..3b0ca66b43 100644 --- a/composer.lock +++ b/composer.lock @@ -2116,12 +2116,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "280d9da9421f5b5648260bb31ea8ee3ed3679685" + "reference": "2f9c43c0b907f6134a6f0213057a40588439c10e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/280d9da9421f5b5648260bb31ea8ee3ed3679685", - "reference": "280d9da9421f5b5648260bb31ea8ee3ed3679685", + "url": "https://api.github.com/repos/utopia-php/database/zipball/2f9c43c0b907f6134a6f0213057a40588439c10e", + "reference": "2f9c43c0b907f6134a6f0213057a40588439c10e", "shasum": "" }, "require": { @@ -2166,7 +2166,7 @@ "issues": "https://github.com/utopia-php/database/issues", "source": "https://github.com/utopia-php/database/tree/feat-relationships" }, - "time": "2023-04-05T09:08:19+00:00" + "time": "2023-04-05T12:42:37+00:00" }, { "name": "utopia-php/domains", diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 56aa2d3b91..83574b5d05 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -136,6 +136,7 @@ class Exception extends \Exception public const DOCUMENT_MISSING_PAYLOAD = 'document_missing_payload'; public const DOCUMENT_ALREADY_EXISTS = 'document_already_exists'; public const DOCUMENT_UPDATE_CONFLICT = 'document_update_conflict'; + public const DOCUMENT_DELETE_RESTRICTED = 'document_delete_restricted'; /** Attribute */ public const ATTRIBUTE_NOT_FOUND = 'attribute_not_found';