From 256f83b7ea0b6e6884be89db79a5b9dab81283e6 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Thu, 8 Sep 2022 22:18:36 +0000 Subject: [PATCH 1/3] Fix and so they are datetime instead of string --- app/controllers/api/databases.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 38489c8331..4f2f287c93 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1557,7 +1557,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes') $oldAttributes[] = [ 'key' => '$id', - 'type' => 'string', + 'type' => Database::VAR_STRING, 'status' => 'available', 'required' => true, 'array' => false, @@ -1567,7 +1567,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes') $oldAttributes[] = [ 'key' => '$createdAt', - 'type' => 'string', + 'type' => Database::VAR_DATETIME, 'status' => 'available', 'signed' => false, 'required' => false, @@ -1578,7 +1578,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes') $oldAttributes[] = [ 'key' => '$updatedAt', - 'type' => 'string', + 'type' => Database::VAR_DATETIME, 'status' => 'available', 'signed' => false, 'required' => false, From 7132e3a7f6dba5e699bec370118a578053cf8f0f Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 9 Sep 2022 11:58:54 +1200 Subject: [PATCH 2/3] Assign member role for all of a users confirmed team memberships --- composer.json | 2 +- composer.lock | 29 ++++-- src/Appwrite/Auth/Auth.php | 9 +- tests/unit/Auth/AuthTest.php | 94 +++++++++++-------- .../unit/Messaging/MessagingChannelsTest.php | 22 +++-- 5 files changed, 91 insertions(+), 65 deletions(-) diff --git a/composer.json b/composer.json index 4598a51ea5..416dc21114 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/cache": "0.6.*", "utopia-php/cli": "0.13.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "0.25.*", + "utopia-php/database": "dev-feat-member-helper as 0.25.0", "utopia-php/locale": "0.4.*", "utopia-php/registry": "0.5.*", "utopia-php/preloader": "0.2.*", diff --git a/composer.lock b/composer.lock index 8245baac5f..16dc6f3a8e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "aca68a1c1c7d762cabd02ce02cf40df4", + "content-hash": "d537356d18e9d5735f6f08e7f361a2f5", "packages": [ { "name": "adhocore/jwt", @@ -2060,16 +2060,16 @@ }, { "name": "utopia-php/database", - "version": "0.25.1", + "version": "dev-feat-member-helper", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "9d013ce3c111d1477d7986483f1003dcab2b9d14" + "reference": "214fd7ebdec7bdf4fb2714dc885e7291e6ae3c4f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/9d013ce3c111d1477d7986483f1003dcab2b9d14", - "reference": "9d013ce3c111d1477d7986483f1003dcab2b9d14", + "url": "https://api.github.com/repos/utopia-php/database/zipball/214fd7ebdec7bdf4fb2714dc885e7291e6ae3c4f", + "reference": "214fd7ebdec7bdf4fb2714dc885e7291e6ae3c4f", "shasum": "" }, "require": { @@ -2118,9 +2118,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.25.1" + "source": "https://github.com/utopia-php/database/tree/feat-member-helper" }, - "time": "2022-09-07T14:47:52+00:00" + "time": "2022-09-08T22:49:17+00:00" }, { "name": "utopia-php/domains", @@ -5358,9 +5358,18 @@ "time": "2022-08-12T06:47:24+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "utopia-php/database", + "version": "dev-feat-member-helper", + "alias": "0.25.0", + "alias_normalized": "0.25.0.0" + } + ], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "utopia-php/database": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { @@ -5384,5 +5393,5 @@ "platform-overrides": { "php": "8.0" }, - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } diff --git a/src/Appwrite/Auth/Auth.php b/src/Appwrite/Auth/Auth.php index 37a2ea8d39..c290d19862 100644 --- a/src/Appwrite/Auth/Auth.php +++ b/src/Appwrite/Auth/Auth.php @@ -431,11 +431,14 @@ class Auth continue; } - if (isset($node['teamId']) && isset($node['roles'])) { + if (isset($node['$id']) && isset($node['teamId'])) { $roles[] = Role::team($node['teamId'])->toString(); + $roles[] = Role::member($node['$id'])->toString(); - foreach ($node['roles'] as $nodeRole) { // Set all team roles - $roles[] = Role::team($node['teamId'], $nodeRole)->toString(); + if (isset($node['roles'])) { + foreach ($node['roles'] as $nodeRole) { // Set all team roles + $roles[] = Role::team($node['teamId'], $nodeRole)->toString(); + } } } } diff --git a/tests/unit/Auth/AuthTest.php b/tests/unit/Auth/AuthTest.php index 4cd180b956..1480fbb23f 100644 --- a/tests/unit/Auth/AuthTest.php +++ b/tests/unit/Auth/AuthTest.php @@ -353,16 +353,58 @@ class AuthTest extends TestCase '$id' => ID::custom('123'), 'memberships' => [ [ - 'confirm' => true, + '$id' => ID::custom('456'), 'teamId' => ID::custom('abc'), + 'confirm' => true, 'roles' => [ 'administrator', 'moderator' ] ], [ - 'confirm' => true, + '$id' => ID::custom('abc'), 'teamId' => ID::custom('def'), + 'confirm' => true, + 'roles' => [ + 'guest' + ] + ] + ] + ]); + + $roles = Auth::getRoles($user); + + $this->assertCount(9, $roles); + $this->assertContains(Role::users()->toString(), $roles); + $this->assertContains(Role::user(ID::custom('123'))->toString(), $roles); + $this->assertContains(Role::team(ID::custom('abc'))->toString(), $roles); + $this->assertContains(Role::team(ID::custom('abc'), 'administrator')->toString(), $roles); + $this->assertContains(Role::team(ID::custom('abc'), 'moderator')->toString(), $roles); + $this->assertContains(Role::team(ID::custom('def'))->toString(), $roles); + $this->assertContains(Role::team(ID::custom('def'), 'guest')->toString(), $roles); + $this->assertContains(Role::member(ID::custom('456'))->toString(), $roles); + $this->assertContains(Role::member(ID::custom('abc'))->toString(), $roles); + } + + public function testPrivilegedUserRoles(): void + { + Authorization::setRole(Auth::USER_ROLE_OWNER); + $user = new Document([ + '$id' => ID::custom('123'), + 'memberships' => [ + [ + '$id' => ID::custom('def'), + 'teamId' => ID::custom('abc'), + 'confirm' => true, + 'roles' => [ + 'administrator', + 'moderator' + ] + ], + [ + '$id' => ID::custom('abc'), + 'teamId' => ID::custom('def'), + 'confirm' => true, 'roles' => [ 'guest' ] @@ -373,42 +415,6 @@ class AuthTest extends TestCase $roles = Auth::getRoles($user); $this->assertCount(7, $roles); - $this->assertContains(Role::users()->toString(), $roles); - $this->assertContains(Role::user(ID::custom('123'))->toString(), $roles); - $this->assertContains(Role::team(ID::custom('abc'))->toString(), $roles); - $this->assertContains(Role::team(ID::custom('abc'), 'administrator')->toString(), $roles); - $this->assertContains(Role::team(ID::custom('abc'), 'moderator')->toString(), $roles); - $this->assertContains(Role::team(ID::custom('def'))->toString(), $roles); - $this->assertContains(Role::team(ID::custom('def'), 'guest')->toString(), $roles); - } - - public function testPrivilegedUserRoles(): void - { - Authorization::setRole(Auth::USER_ROLE_OWNER); - $user = new Document([ - '$id' => ID::custom('123'), - 'memberships' => [ - [ - 'confirm' => true, - 'teamId' => ID::custom('abc'), - 'roles' => [ - 'administrator', - 'moderator' - ] - ], - [ - 'confirm' => true, - 'teamId' => ID::custom('def'), - 'roles' => [ - 'guest' - ] - ] - ] - ]); - - $roles = Auth::getRoles($user); - - $this->assertCount(5, $roles); $this->assertNotContains(Role::users()->toString(), $roles); $this->assertNotContains(Role::user(ID::custom('123'))->toString(), $roles); $this->assertContains(Role::team(ID::custom('abc'))->toString(), $roles); @@ -416,6 +422,8 @@ class AuthTest extends TestCase $this->assertContains(Role::team(ID::custom('abc'), 'moderator')->toString(), $roles); $this->assertContains(Role::team(ID::custom('def'))->toString(), $roles); $this->assertContains(Role::team(ID::custom('def'), 'guest')->toString(), $roles); + $this->assertContains(Role::member(ID::custom('def'))->toString(), $roles); + $this->assertContains(Role::member(ID::custom('abc'))->toString(), $roles); } public function testAppUserRoles(): void @@ -425,16 +433,18 @@ class AuthTest extends TestCase '$id' => ID::custom('123'), 'memberships' => [ [ - 'confirm' => true, + '$id' => ID::custom('def'), 'teamId' => ID::custom('abc'), + 'confirm' => true, 'roles' => [ 'administrator', 'moderator' ] ], [ - 'confirm' => true, + '$id' => ID::custom('abc'), 'teamId' => ID::custom('def'), + 'confirm' => true, 'roles' => [ 'guest' ] @@ -444,7 +454,7 @@ class AuthTest extends TestCase $roles = Auth::getRoles($user); - $this->assertCount(5, $roles); + $this->assertCount(7, $roles); $this->assertNotContains(Role::users()->toString(), $roles); $this->assertNotContains(Role::user(ID::custom('123'))->toString(), $roles); $this->assertContains(Role::team(ID::custom('abc'))->toString(), $roles); @@ -452,5 +462,7 @@ class AuthTest extends TestCase $this->assertContains(Role::team(ID::custom('abc'), 'moderator')->toString(), $roles); $this->assertContains(Role::team(ID::custom('def'))->toString(), $roles); $this->assertContains(Role::team(ID::custom('def'), 'guest')->toString(), $roles); + $this->assertContains(Role::member(ID::custom('def'))->toString(), $roles); + $this->assertContains(Role::member(ID::custom('abc'))->toString(), $roles); } } diff --git a/tests/unit/Messaging/MessagingChannelsTest.php b/tests/unit/Messaging/MessagingChannelsTest.php index 26a1106688..dd2ec04401 100644 --- a/tests/unit/Messaging/MessagingChannelsTest.php +++ b/tests/unit/Messaging/MessagingChannelsTest.php @@ -54,8 +54,9 @@ class MessagingChannelsTest extends TestCase '$id' => ID::custom('user' . $this->connectionsCount), 'memberships' => [ [ - 'confirm' => true, + '$id' => ID::custom('member' . $i), 'teamId' => ID::custom('team' . $i), + 'confirm' => true, 'roles' => [ empty($index % 2) ? Auth::USER_ROLE_ADMIN @@ -122,11 +123,11 @@ class MessagingChannelsTest extends TestCase * Check for correct amount of subscriptions: * - XXX users * - XXX teams - * - XXX team roles (2 roles per team) + * - XXX team roles (3 roles per team) * - 1 guests * - 1 users */ - $this->assertCount(($this->connectionsAuthenticated + (3 * $this->connectionsPerChannel) + 2), $this->realtime->subscriptions['1']); + $this->assertCount(($this->connectionsAuthenticated + (4 * $this->connectionsPerChannel) + 2), $this->realtime->subscriptions['1']); /** * Check for connections @@ -138,7 +139,7 @@ class MessagingChannelsTest extends TestCase $this->realtime->unsubscribe(-1); $this->assertCount($this->connectionsTotal, $this->realtime->connections); - $this->assertCount(($this->connectionsAuthenticated + (3 * $this->connectionsPerChannel) + 2), $this->realtime->subscriptions['1']); + $this->assertCount(($this->connectionsAuthenticated + (4 * $this->connectionsPerChannel) + 2), $this->realtime->subscriptions['1']); for ($i = 0; $i < $this->connectionsCount; $i++) { $this->realtime->unsubscribe($i); @@ -259,6 +260,7 @@ class MessagingChannelsTest extends TestCase for ($i = 0; $i < $this->connectionsPerChannel; $i++) { $permissions[] = Role::team(ID::custom('team' . $i))->toString(); + $permissions[] = Role::member(ID::custom('member' . $i))->toString(); } $event = [ 'project' => '1', @@ -284,13 +286,13 @@ class MessagingChannelsTest extends TestCase $this->assertStringEndsWith($index, $receiver); } + $role = empty($index % 2) + ? Auth::USER_ROLE_ADMIN + : 'member'; + $permissions = [ - Role::team( - ID::custom('team' . $index), - (empty($index % 2) - ? Auth::USER_ROLE_ADMIN - : 'member') - )->toString() + Role::team(ID::custom('team' . $index), $role)->toString(), + Role::member(ID::custom('member' . $index))->toString() ]; $event = [ From 6ac236ed228c87ea260bf63d1eec95a62e494d46 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 9 Sep 2022 16:08:24 +1200 Subject: [PATCH 3/3] Update DB --- composer.json | 2 +- composer.lock | 27 +++++++++------------------ 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 416dc21114..4598a51ea5 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/cache": "0.6.*", "utopia-php/cli": "0.13.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "dev-feat-member-helper as 0.25.0", + "utopia-php/database": "0.25.*", "utopia-php/locale": "0.4.*", "utopia-php/registry": "0.5.*", "utopia-php/preloader": "0.2.*", diff --git a/composer.lock b/composer.lock index 16dc6f3a8e..1f61b8aef6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d537356d18e9d5735f6f08e7f361a2f5", + "content-hash": "aca68a1c1c7d762cabd02ce02cf40df4", "packages": [ { "name": "adhocore/jwt", @@ -2060,16 +2060,16 @@ }, { "name": "utopia-php/database", - "version": "dev-feat-member-helper", + "version": "0.25.2", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "214fd7ebdec7bdf4fb2714dc885e7291e6ae3c4f" + "reference": "140bbedf1c4d622990fb94d26681fcca235cd5b9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/214fd7ebdec7bdf4fb2714dc885e7291e6ae3c4f", - "reference": "214fd7ebdec7bdf4fb2714dc885e7291e6ae3c4f", + "url": "https://api.github.com/repos/utopia-php/database/zipball/140bbedf1c4d622990fb94d26681fcca235cd5b9", + "reference": "140bbedf1c4d622990fb94d26681fcca235cd5b9", "shasum": "" }, "require": { @@ -2118,9 +2118,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/feat-member-helper" + "source": "https://github.com/utopia-php/database/tree/0.25.2" }, - "time": "2022-09-08T22:49:17+00:00" + "time": "2022-09-09T03:58:01+00:00" }, { "name": "utopia-php/domains", @@ -5358,18 +5358,9 @@ "time": "2022-08-12T06:47:24+00:00" } ], - "aliases": [ - { - "package": "utopia-php/database", - "version": "dev-feat-member-helper", - "alias": "0.25.0", - "alias_normalized": "0.25.0.0" - } - ], + "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "utopia-php/database": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": {