From 841d260b5fd31fe3c776a06e1c12e872d3b70992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 29 Apr 2025 11:31:06 +0200 Subject: [PATCH 1/2] PlatformDB migration preparations --- app/config/collections/platform.php | 2 +- app/controllers/general.php | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index ce8be6a874..e53194c513 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1148,7 +1148,7 @@ return [ 'format' => '', 'size' => 32, 'signed' => true, - 'required' => true, + 'required' => false, 'default' => null, 'array' => false, 'filters' => [], diff --git a/app/controllers/general.php b/app/controllers/general.php index 889571fe62..939573261e 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -149,7 +149,17 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw $dbForProject = $getProjectDB($project); /** @var Document $deployment */ - $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $rule->getAttribute('deploymentId'))); + if(!empty($rule->getAttribute('deploymentId', ''))) { + $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $rule->getAttribute('deploymentId'))); + } else { + // 1.6.x DB schema compatibility + // TODO: Make sure deploymentId is never empty, and remove this code + $resource = $rule->getAttribute('deploymentResourceType', '') === 'function' ? + Authorization::skip(fn () => $dbForProject->getDocument('functions', $rule->getAttribute('deploymentResourceId', ''))) : + Authorization::skip(fn () => $dbForProject->getDocument('sites', $rule->getAttribute('deploymentResourceId', ''))); + $deploymentId = $resource->getAttribute('deploymentId', $resource->getAttribute('deployment', '')); + $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $deploymentId)); + } if ($deployment->getAttribute('resourceType', '') === 'functions') { $type = 'function'; From 7ece755abf4b5341db8fb6d721e40c1b7e9a3e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 29 Apr 2025 11:56:51 +0200 Subject: [PATCH 2/2] Improve code quality --- app/controllers/general.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 939573261e..dbc07f950b 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -149,17 +149,31 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw $dbForProject = $getProjectDB($project); /** @var Document $deployment */ - if(!empty($rule->getAttribute('deploymentId', ''))) { - $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $rule->getAttribute('deploymentId'))); + if (!empty($rule->getAttribute('deploymentId', ''))) { + $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $rule->getAttribute('deploymentId'))); } else { // 1.6.x DB schema compatibility // TODO: Make sure deploymentId is never empty, and remove this code - $resource = $rule->getAttribute('deploymentResourceType', '') === 'function' ? - Authorization::skip(fn () => $dbForProject->getDocument('functions', $rule->getAttribute('deploymentResourceId', ''))) : - Authorization::skip(fn () => $dbForProject->getDocument('sites', $rule->getAttribute('deploymentResourceId', ''))); - $deploymentId = $resource->getAttribute('deploymentId', $resource->getAttribute('deployment', '')); - $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $deploymentId)); - } + + // Check if site or function; should never be site, but better safe than sorry + // Attempts to use attribute from both schemas (1.6 and 1.7) + $resourceType = $rule->getAttribute('deploymentResourceType', $rule->getAttribute('resourceType', '')); + + // ID of site or function + $resourceId = $rule->getAttribute('deploymentResourceId', ''); + + // Document of site or function + $resource = $resourceType === 'function' ? + Authorization::skip(fn () => $dbForProject->getDocument('functions', $resourceId)) : + Authorization::skip(fn () => $dbForProject->getDocument('sites', $resourceId)); + + // ID of active deployments + // Attempts to use attribute from both schemas (1.6 and 1.7) + $activeDeploymentId = $resource->getAttribute('deploymentId', $resource->getAttribute('deployment', '')); + + // Get deployment document, as intended originally + $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $activeDeploymentId)); + } if ($deployment->getAttribute('resourceType', '') === 'functions') { $type = 'function';