1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00

Adding Internal Ids Projects, Webhooks, Keys, domains, Platforms

This commit is contained in:
fogelito 2022-06-16 10:50:52 +03:00
parent fce6f152e8
commit ddbe1edf6d
4 changed files with 109 additions and 26 deletions

View file

@ -357,6 +357,17 @@ $collections = [
'$id' => 'projects', '$id' => 'projects',
'name' => 'Projects', 'name' => 'Projects',
'attributes' => [ 'attributes' => [
[
'$id' => 'teamInternalId',
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[ [
'$id' => 'teamId', '$id' => 'teamId',
'type' => Database::VAR_STRING, 'type' => Database::VAR_STRING,
@ -594,6 +605,17 @@ $collections = [
'$id' => 'platforms', '$id' => 'platforms',
'name' => 'platforms', 'name' => 'platforms',
'attributes' => [ 'attributes' => [
[
'$id' => 'projectInternalId',
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[ [
'$id' => 'projectId', '$id' => 'projectId',
'type' => Database::VAR_STRING, 'type' => Database::VAR_STRING,
@ -684,6 +706,13 @@ $collections = [
], ],
], ],
'indexes' => [ 'indexes' => [
[
'$id' => '_key_project_internal_id',
'type' => Database::INDEX_KEY,
'attributes' => ['projectInternalId'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[ [
'$id' => '_key_project', '$id' => '_key_project',
'type' => Database::INDEX_KEY, 'type' => Database::INDEX_KEY,
@ -699,6 +728,17 @@ $collections = [
'$id' => 'domains', '$id' => 'domains',
'name' => 'domains', 'name' => 'domains',
'attributes' => [ 'attributes' => [
[
'$id' => 'projectInternalId',
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[ [
'$id' => 'projectId', '$id' => 'projectId',
'type' => Database::VAR_STRING, 'type' => Database::VAR_STRING,
@ -778,6 +818,13 @@ $collections = [
], ],
], ],
'indexes' => [ 'indexes' => [
[
'$id' => '_key_project_internal_id',
'type' => Database::INDEX_KEY,
'attributes' => ['projectInternalId'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[ [
'$id' => '_key_project', '$id' => '_key_project',
'type' => Database::INDEX_KEY, 'type' => Database::INDEX_KEY,
@ -793,6 +840,17 @@ $collections = [
'$id' => 'keys', '$id' => 'keys',
'name' => 'keys', 'name' => 'keys',
'attributes' => [ 'attributes' => [
[
'$id' => 'projectInternalId',
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[ [
'$id' => 'projectId', '$id' => 'projectId',
'type' => Database::VAR_STRING, 'type' => Database::VAR_STRING,
@ -839,6 +897,13 @@ $collections = [
], ],
], ],
'indexes' => [ 'indexes' => [
[
'$id' => '_key_project_internal_id',
'type' => Database::INDEX_KEY,
'attributes' => ['projectInternalId'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[ [
'$id' => '_key_project', '$id' => '_key_project',
'type' => Database::INDEX_KEY, 'type' => Database::INDEX_KEY,
@ -854,6 +919,17 @@ $collections = [
'$id' => 'webhooks', '$id' => 'webhooks',
'name' => 'webhooks', 'name' => 'webhooks',
'attributes' => [ 'attributes' => [
[
'$id' => 'projectInternalId',
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[ [
'$id' => 'projectId', '$id' => 'projectId',
'type' => Database::VAR_STRING, 'type' => Database::VAR_STRING,

View file

@ -77,14 +77,17 @@ App::post('/v1/projects')
} }
$projectId = ($projectId == 'unique()') ? $dbForConsole->getId() : $projectId; $projectId = ($projectId == 'unique()') ? $dbForConsole->getId() : $projectId;
if ($projectId === 'console') { if ($projectId === 'console') {
throw new Exception("'console' is a reserved project.", 400, Exception::PROJECT_RESERVED_PROJECT); throw new Exception("'console' is a reserved project.", 400, Exception::PROJECT_RESERVED_PROJECT);
} }
$project = $dbForConsole->createDocument('projects', new Document([ $project = $dbForConsole->createDocument('projects', new Document([
'$id' => $projectId == 'unique()' ? $dbForConsole->getId() : $projectId, '$id' => $projectId,
'$read' => ['team:' . $teamId], '$read' => ['team:' . $teamId],
'$write' => ['team:' . $teamId . '/owner', 'team:' . $teamId . '/developer'], '$write' => ['team:' . $teamId . '/owner', 'team:' . $teamId . '/developer'],
'name' => $name, 'name' => $name,
'teamInternalId' => $team->getInternalId(),
'teamId' => $team->getId(), 'teamId' => $team->getId(),
'description' => $description, 'description' => $description,
'logo' => $logo, 'logo' => $logo,
@ -108,7 +111,7 @@ App::post('/v1/projects')
/** @var array $collections */ /** @var array $collections */
$collections = Config::getParam('collections', []); $collections = Config::getParam('collections', []);
$dbForProject->setNamespace("_{$project->getId()}"); $dbForProject->setNamespace("_{$project->getInternalId()}");
$dbForProject->create('appwrite'); $dbForProject->create('appwrite');
$audit = new Audit($dbForProject); $audit = new Audit($dbForProject);
@ -267,7 +270,7 @@ App::get('/v1/projects/:projectId/usage')
], ],
]; ];
$dbForProject->setNamespace("_{$projectId}"); $dbForProject->setNamespace("_{$project->getInternalId()}");
$metrics = [ $metrics = [
'requests', 'requests',
@ -586,6 +589,7 @@ App::post('/v1/projects/:projectId/webhooks')
'$id' => $dbForConsole->getId(), '$id' => $dbForConsole->getId(),
'$read' => ['role:all'], '$read' => ['role:all'],
'$write' => ['role:all'], '$write' => ['role:all'],
'projectInternalId' => $project->getInternalId(),
'projectId' => $project->getId(), 'projectId' => $project->getId(),
'name' => $name, 'name' => $name,
'events' => $events, 'events' => $events,
@ -625,7 +629,7 @@ App::get('/v1/projects/:projectId/webhooks')
} }
$webhooks = $dbForConsole->find('webhooks', [ $webhooks = $dbForConsole->find('webhooks', [
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
], 5000); ], 5000);
$response->dynamic(new Document([ $response->dynamic(new Document([
@ -658,7 +662,7 @@ App::get('/v1/projects/:projectId/webhooks/:webhookId')
$webhook = $dbForConsole->findOne('webhooks', [ $webhook = $dbForConsole->findOne('webhooks', [
new Query('_uid', Query::TYPE_EQUAL, [$webhookId]), new Query('_uid', Query::TYPE_EQUAL, [$webhookId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($webhook === false || $webhook->isEmpty()) { if ($webhook === false || $webhook->isEmpty()) {
@ -700,7 +704,7 @@ App::put('/v1/projects/:projectId/webhooks/:webhookId')
$webhook = $dbForConsole->findOne('webhooks', [ $webhook = $dbForConsole->findOne('webhooks', [
new Query('_uid', Query::TYPE_EQUAL, [$webhookId]), new Query('_uid', Query::TYPE_EQUAL, [$webhookId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($webhook === false || $webhook->isEmpty()) { if ($webhook === false || $webhook->isEmpty()) {
@ -746,7 +750,7 @@ App::delete('/v1/projects/:projectId/webhooks/:webhookId')
$webhook = $dbForConsole->findOne('webhooks', [ $webhook = $dbForConsole->findOne('webhooks', [
new Query('_uid', Query::TYPE_EQUAL, [$webhookId]), new Query('_uid', Query::TYPE_EQUAL, [$webhookId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($webhook === false || $webhook->isEmpty()) { if ($webhook === false || $webhook->isEmpty()) {
@ -789,6 +793,7 @@ App::post('/v1/projects/:projectId/keys')
'$id' => $dbForConsole->getId(), '$id' => $dbForConsole->getId(),
'$read' => ['role:all'], '$read' => ['role:all'],
'$write' => ['role:all'], '$write' => ['role:all'],
'projectInternalId' => $project->getInternalId(),
'projectId' => $project->getId(), 'projectId' => $project->getId(),
'name' => $name, 'name' => $name,
'scopes' => $scopes, 'scopes' => $scopes,
@ -825,7 +830,7 @@ App::get('/v1/projects/:projectId/keys')
} }
$keys = $dbForConsole->find('keys', [ $keys = $dbForConsole->find('keys', [
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]), new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()]),
], 5000); ], 5000);
$response->dynamic(new Document([ $response->dynamic(new Document([
@ -858,7 +863,7 @@ App::get('/v1/projects/:projectId/keys/:keyId')
$key = $dbForConsole->findOne('keys', [ $key = $dbForConsole->findOne('keys', [
new Query('_uid', Query::TYPE_EQUAL, [$keyId]), new Query('_uid', Query::TYPE_EQUAL, [$keyId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($key === false || $key->isEmpty()) { if ($key === false || $key->isEmpty()) {
@ -894,7 +899,7 @@ App::put('/v1/projects/:projectId/keys/:keyId')
$key = $dbForConsole->findOne('keys', [ $key = $dbForConsole->findOne('keys', [
new Query('_uid', Query::TYPE_EQUAL, [$keyId]), new Query('_uid', Query::TYPE_EQUAL, [$keyId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($key === false || $key->isEmpty()) { if ($key === false || $key->isEmpty()) {
@ -936,7 +941,7 @@ App::delete('/v1/projects/:projectId/keys/:keyId')
$key = $dbForConsole->findOne('keys', [ $key = $dbForConsole->findOne('keys', [
new Query('_uid', Query::TYPE_EQUAL, [$keyId]), new Query('_uid', Query::TYPE_EQUAL, [$keyId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($key === false || $key->isEmpty()) { if ($key === false || $key->isEmpty()) {
@ -981,6 +986,7 @@ App::post('/v1/projects/:projectId/platforms')
'$id' => $dbForConsole->getId(), '$id' => $dbForConsole->getId(),
'$read' => ['role:all'], '$read' => ['role:all'],
'$write' => ['role:all'], '$write' => ['role:all'],
'projectInternalId' => $project->getInternalId(),
'projectId' => $project->getId(), 'projectId' => $project->getId(),
'type' => $type, 'type' => $type,
'name' => $name, 'name' => $name,
@ -1054,7 +1060,7 @@ App::get('/v1/projects/:projectId/platforms/:platformId')
$platform = $dbForConsole->findOne('platforms', [ $platform = $dbForConsole->findOne('platforms', [
new Query('_uid', Query::TYPE_EQUAL, [$platformId]), new Query('_uid', Query::TYPE_EQUAL, [$platformId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($platform === false || $platform->isEmpty()) { if ($platform === false || $platform->isEmpty()) {
@ -1091,7 +1097,7 @@ App::put('/v1/projects/:projectId/platforms/:platformId')
$platform = $dbForConsole->findOne('platforms', [ $platform = $dbForConsole->findOne('platforms', [
new Query('_uid', Query::TYPE_EQUAL, [$platformId]), new Query('_uid', Query::TYPE_EQUAL, [$platformId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($platform === false || $platform->isEmpty()) { if ($platform === false || $platform->isEmpty()) {
@ -1136,7 +1142,7 @@ App::delete('/v1/projects/:projectId/platforms/:platformId')
$platform = $dbForConsole->findOne('platforms', [ $platform = $dbForConsole->findOne('platforms', [
new Query('_uid', Query::TYPE_EQUAL, [$platformId]), new Query('_uid', Query::TYPE_EQUAL, [$platformId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($platform === false || $platform->isEmpty()) { if ($platform === false || $platform->isEmpty()) {
@ -1176,7 +1182,7 @@ App::post('/v1/projects/:projectId/domains')
$document = $dbForConsole->findOne('domains', [ $document = $dbForConsole->findOne('domains', [
new Query('domain', Query::TYPE_EQUAL, [$domain]), new Query('domain', Query::TYPE_EQUAL, [$domain]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]), new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()]),
]); ]);
if ($document && !$document->isEmpty()) { if ($document && !$document->isEmpty()) {
@ -1195,6 +1201,7 @@ App::post('/v1/projects/:projectId/domains')
'$id' => $dbForConsole->getId(), '$id' => $dbForConsole->getId(),
'$read' => ['role:all'], '$read' => ['role:all'],
'$write' => ['role:all'], '$write' => ['role:all'],
'projectInternalId' => $project->getInternalId(),
'projectId' => $project->getId(), 'projectId' => $project->getId(),
'updated' => \time(), 'updated' => \time(),
'domain' => $domain->get(), 'domain' => $domain->get(),
@ -1234,7 +1241,7 @@ App::get('/v1/projects/:projectId/domains')
} }
$domains = $dbForConsole->find('domains', [ $domains = $dbForConsole->find('domains', [
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
], 5000); ], 5000);
$response->dynamic(new Document([ $response->dynamic(new Document([
@ -1267,7 +1274,7 @@ App::get('/v1/projects/:projectId/domains/:domainId')
$domain = $dbForConsole->findOne('domains', [ $domain = $dbForConsole->findOne('domains', [
new Query('_uid', Query::TYPE_EQUAL, [$domainId]), new Query('_uid', Query::TYPE_EQUAL, [$domainId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($domain === false || $domain->isEmpty()) { if ($domain === false || $domain->isEmpty()) {
@ -1301,7 +1308,7 @@ App::patch('/v1/projects/:projectId/domains/:domainId/verification')
$domain = $dbForConsole->findOne('domains', [ $domain = $dbForConsole->findOne('domains', [
new Query('_uid', Query::TYPE_EQUAL, [$domainId]), new Query('_uid', Query::TYPE_EQUAL, [$domainId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($domain === false || $domain->isEmpty()) { if ($domain === false || $domain->isEmpty()) {
@ -1361,7 +1368,7 @@ App::delete('/v1/projects/:projectId/domains/:domainId')
$domain = $dbForConsole->findOne('domains', [ $domain = $dbForConsole->findOne('domains', [
new Query('_uid', Query::TYPE_EQUAL, [$domainId]), new Query('_uid', Query::TYPE_EQUAL, [$domainId]),
new Query('projectId', Query::TYPE_EQUAL, [$project->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$project->getInternalId()])
]); ]);
if ($domain === false || $domain->isEmpty()) { if ($domain === false || $domain->isEmpty()) {

View file

@ -282,7 +282,7 @@ Database::addFilter(
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('platforms', [ ->find('platforms', [
new Query('projectId', Query::TYPE_EQUAL, [$document->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$document->getInternalId()])
], APP_LIMIT_SUBQUERY); ], APP_LIMIT_SUBQUERY);
} }
); );
@ -295,7 +295,7 @@ Database::addFilter(
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('domains', [ ->find('domains', [
new Query('projectId', Query::TYPE_EQUAL, [$document->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$document->getInternalId()])
], APP_LIMIT_SUBQUERY); ], APP_LIMIT_SUBQUERY);
} }
); );
@ -308,7 +308,7 @@ Database::addFilter(
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('keys', [ ->find('keys', [
new Query('projectId', Query::TYPE_EQUAL, [$document->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$document->getInternalId()])
], APP_LIMIT_SUBQUERY); ], APP_LIMIT_SUBQUERY);
} }
); );
@ -321,7 +321,7 @@ Database::addFilter(
function (mixed $value, Document $document, Database $database) { function (mixed $value, Document $document, Database $database) {
return $database return $database
->find('webhooks', [ ->find('webhooks', [
new Query('projectId', Query::TYPE_EQUAL, [$document->getId()]) new Query('projectInternalId', Query::TYPE_EQUAL, [$document->getInternalId()])
], APP_LIMIT_SUBQUERY); ], APP_LIMIT_SUBQUERY);
} }
); );
@ -868,12 +868,12 @@ App::setResource('console', function () {
]); ]);
}, []); }, []);
App::setResource('dbForProject', function ($db, $cache, $project) { App::setResource('dbForProject', function ($db, $cache, Document $project) {
$cache = new Cache(new RedisCache($cache)); $cache = new Cache(new RedisCache($cache));
$database = new Database(new MariaDB($db), $cache); $database = new Database(new MariaDB($db), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_{$project->getId()}"); $database->setNamespace("_{$project->getInternalId()}");
return $database; return $database;
}, ['db', 'cache', 'project']); }, ['db', 'cache', 'project']);

View file

@ -373,7 +373,7 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server,
$cache = new Cache(new RedisCache($redis)); $cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($db), $cache); $database = new Database(new MariaDB($db), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_{$project->getId()}"); $database->setNamespace("_{$project->getInternalId()}");
/* /*
* Project Check * Project Check