diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index d44d9b725c..e919df8e1a 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -6,7 +6,7 @@ use Utopia\Database\Helpers\ID; $providers = Config::getParam('oAuthProviders', []); -return [ +$platformCollections = [ 'projects' => [ '$collection' => ID::custom(Database::METADATA), '$id' => ID::custom('projects'), @@ -643,6 +643,39 @@ return [ 'array' => false, 'filters' => [], ], + [ + '$id' => 'resourceType', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'resourceId', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'resourceInternalId', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('name'), 'type' => Database::VAR_STRING, @@ -718,6 +751,13 @@ return [ 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => '_key_resource', + 'type' => Database::INDEX_KEY, + 'attributes' => ['resourceType', 'resourceInternalId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], [ '$id' => '_key_accessedAt', 'type' => Database::INDEX_KEY, @@ -1903,3 +1943,31 @@ return [ 'indexes' => [] ], ]; + +// Organization API keys subquery +$platformCollections['teams']['attributes'][] = [ + '$id' => ID::custom('keys'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['subQueryOrganizationKeys'], +]; + +// Account API keys subquery +$platformCollections['users']['attributes'][] = [ + '$id' => ID::custom('keys'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['subQueryAccountKeys'], +]; + +return $platformCollections; diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index c4d703d744..a49f594301 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -1499,6 +1499,9 @@ App::post('/v1/projects/:projectId/keys') ], 'projectInternalId' => $project->getSequence(), 'projectId' => $project->getId(), + 'resourceInternalId' => $project->getSequence(), + 'resourceId' => $project->getId(), + 'resourceType' => 'projects', 'name' => $name, 'scopes' => $scopes, 'expire' => $expire, @@ -1546,7 +1549,13 @@ App::get('/v1/projects/:projectId/keys') } $keys = $dbForPlatform->find('keys', [ - Query::equal('projectInternalId', [$project->getSequence()]), + Query::or([ + Query::equal('projectInternalId', [$project->getSequence()]), + Query::and([ + Query::equal('resourceType', ['projects']), + Query::equal('resourceInternalId', [$project->getSequence()]), + ]) + ]), Query::limit(5000), ]); @@ -1587,7 +1596,13 @@ App::get('/v1/projects/:projectId/keys/:keyId') $key = $dbForPlatform->findOne('keys', [ Query::equal('$id', [$keyId]), - Query::equal('projectInternalId', [$project->getSequence()]), + Query::or([ + Query::equal('projectInternalId', [$project->getSequence()]), + Query::and([ + Query::equal('resourceType', ['projects']), + Query::equal('resourceInternalId', [$project->getSequence()]), + ]) + ]) ]); if ($key->isEmpty()) { @@ -1631,7 +1646,13 @@ App::put('/v1/projects/:projectId/keys/:keyId') $key = $dbForPlatform->findOne('keys', [ Query::equal('$id', [$keyId]), - Query::equal('projectInternalId', [$project->getSequence()]), + Query::or([ + Query::equal('projectInternalId', [$project->getSequence()]), + Query::and([ + Query::equal('resourceType', ['projects']), + Query::equal('resourceInternalId', [$project->getSequence()]), + ]) + ]) ]); if ($key->isEmpty()) { @@ -1682,7 +1703,13 @@ App::delete('/v1/projects/:projectId/keys/:keyId') $key = $dbForPlatform->findOne('keys', [ Query::equal('$id', [$keyId]), - Query::equal('projectInternalId', [$project->getSequence()]), + Query::or([ + Query::equal('projectInternalId', [$project->getSequence()]), + Query::and([ + Query::equal('resourceType', ['projects']), + Query::equal('resourceInternalId', [$project->getSequence()]), + ]) + ]) ]); if ($key->isEmpty()) { diff --git a/app/init/database/filters.php b/app/init/database/filters.php index c4cfd1ac81..49c13c9a0b 100644 --- a/app/init/database/filters.php +++ b/app/init/database/filters.php @@ -136,7 +136,13 @@ Database::addFilter( function (mixed $value, Document $document, Database $database) { return $database ->find('keys', [ - Query::equal('projectInternalId', [$document->getSequence()]), + Query::or([ + Query::equal('projectInternalId', [$document->getSequence()]), + Query::and([ + Query::equal('resourceType', ['projects']), + Query::equal('resourceInternalId', [$document->getSequence()]), + ]) + ]), Query::limit(APP_LIMIT_SUBQUERY), ]); } diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 5729bdc2c7..dfd9aebbf5 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -565,7 +565,13 @@ class Deletes extends Action // Delete Keys $this->deleteByGroup('keys', [ - Query::equal('projectInternalId', [$projectInternalId]), + Query::or([ + Query::equal('projectInternalId', [$projectInternalId]), + Query::and([ + Query::equal('resourceType', ['projects']), + Query::equal('resourceInternalId', [$projectInternalId]), + ]) + ]), Query::orderAsc() ], $dbForPlatform); diff --git a/src/Appwrite/Platform/Workers/StatsResources.php b/src/Appwrite/Platform/Workers/StatsResources.php index e465f9cca2..967dbc59a4 100644 --- a/src/Appwrite/Platform/Workers/StatsResources.php +++ b/src/Appwrite/Platform/Workers/StatsResources.php @@ -111,7 +111,13 @@ class StatsResources extends Action Query::equal('projectInternalId', [$project->getSequence()]) ]); $keys = $dbForPlatform->count('keys', [ - Query::equal('projectInternalId', [$project->getSequence()]) + Query::or([ + Query::equal('projectInternalId', [$project->getSequence()]), + Query::and([ + Query::equal('resourceType', ['projects']), + Query::equal('resourceInternalId', [$project->getSequence()]), + ]) + ]), ]); $domains = $dbForPlatform->count('rules', [