From 3c20404e712f8e59a372a829088918d57a9a2c0d Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 5 Aug 2021 10:47:55 +0545 Subject: [PATCH] account endpoint custom id --- app/controllers/api/account.php | 25 +++++++++++++++---- tests/e2e/Scopes/Scope.php | 4 +++ tests/e2e/Services/Account/AccountBase.php | 14 +++++++++++ .../Account/AccountCustomClientTest.php | 7 ++++++ .../Account/AccountCustomServerTest.php | 1 + .../Projects/ProjectsConsoleClientTest.php | 7 ++++++ .../Webhooks/WebhooksCustomClientTest.php | 7 ++++++ tests/e2e/Services/Workers/WebhooksTest.php | 1 + 8 files changed, 61 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 8a6f951645..57ccbdc9f9 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -24,6 +24,7 @@ use Utopia\Database\Exception\Duplicate; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; +use Appwrite\Database\Validator\CustomId; $oauthDefaultSuccess = App::getEnv('_APP_HOME').'/auth/oauth2/success'; $oauthDefaultFailure = App::getEnv('_APP_HOME').'/auth/oauth2/failure'; @@ -42,6 +43,7 @@ App::post('/v1/account') ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_USER) ->label('abuse-limit', 10) + ->param('userId', '', new CustomId(), 'Unique Id. Choose your own unique ID or pass the string `unique()` to auto generate it. Valid chars are a-z, A-Z, 0-9, and underscore. Can\'t start with a leading underscore. Max length is 36 chars.') ->param('email', '', new Email(), 'User email.') ->param('password', '', new Password(), 'User password. Must be between 6 to 32 chars.') ->param('name', '', new Text(128), 'User name. Max length: 128 chars.', true) @@ -50,7 +52,7 @@ App::post('/v1/account') ->inject('project') ->inject('dbForInternal') ->inject('audits') - ->action(function ($email, $password, $name, $request, $response, $project, $dbForInternal, $audits) { + ->action(function ($userId, $email, $password, $name, $request, $response, $project, $dbForInternal, $audits) { /** @var Utopia\Swoole\Request $request */ /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Document $project */ @@ -84,7 +86,7 @@ App::post('/v1/account') Authorization::disable(); try { - $userId = $dbForInternal->getId(); + $userId = $userId == 'unique()' ? $dbForInternal->getId() : $userId; $user = $dbForInternal->createDocument('users', new Document([ '$id' => $userId, '$read' => ['role:all'], @@ -137,6 +139,7 @@ App::post('/v1/account/sessions') ->label('sdk.response.model', Response::MODEL_SESSION) ->label('abuse-limit', 10) ->label('abuse-key', 'url:{url},email:{param-email}') + ->param('sessionId', '', new CustomId(), 'Unique Id. Choose your own unique ID or pass the string `unique()` to auto generate it. Valid chars are a-z, A-Z, 0-9, and underscore. Can\'t start with a leading underscore. Max length is 36 chars.') ->param('email', '', new Email(), 'User email.') ->param('password', '', new Password(), 'User password. Must be between 6 to 32 chars.') ->inject('request') @@ -145,7 +148,7 @@ App::post('/v1/account/sessions') ->inject('locale') ->inject('geodb') ->inject('audits') - ->action(function ($email, $password, $request, $response, $dbForInternal, $locale, $geodb, $audits) { + ->action(function ($sessionId, $email, $password, $request, $response, $dbForInternal, $locale, $geodb, $audits) { /** @var Utopia\Swoole\Request $request */ /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Database $dbForInternal */ @@ -178,7 +181,7 @@ App::post('/v1/account/sessions') $secret = Auth::tokenGenerator(); $session = new Document(array_merge( [ - '$id' => $dbForInternal->getId(), + '$id' => $sessionId == 'unique()' ? $dbForInternal->getId() : $sessionId, 'userId' => $profile->getId(), 'provider' => Auth::SESSION_PROVIDER_EMAIL, 'providerUid' => $email, @@ -679,7 +682,19 @@ App::post('/v1/account/sessions/anonymous') ->setAttribute('$read', ['user:'.$user->getId()]) ->setAttribute('$write', ['user:'.$user->getId()]) ); - + it', 50) + ->label('abuse-key', 'ip:{ip}') + ->inject('request') + ->inject('response') + ->inject('locale') + ->inject('user') + ->inject('project') + ->inject('dbForInternal') + ->inject('geodb') + ->inject('audits') + ->action(function ($request, $response, $locale, $user, $project, $dbForInternal, $geodb, $audits) { + /** @var Utopia\Swoole\Request $request */ + /** @var Appwrite\Utopia\Response $respons $user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('sessions', $session, Document::SET_TYPE_APPEND)); diff --git a/tests/e2e/Scopes/Scope.php b/tests/e2e/Scopes/Scope.php index 64332a3e61..71cbd3885a 100644 --- a/tests/e2e/Scopes/Scope.php +++ b/tests/e2e/Scopes/Scope.php @@ -87,6 +87,7 @@ abstract class Scope extends TestCase 'content-type' => 'application/json', 'x-appwrite-project' => 'console', ], [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -99,6 +100,7 @@ abstract class Scope extends TestCase 'content-type' => 'application/json', 'x-appwrite-project' => 'console', ], [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -138,6 +140,7 @@ abstract class Scope extends TestCase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -150,6 +153,7 @@ abstract class Scope extends TestCase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); diff --git a/tests/e2e/Services/Account/AccountBase.php b/tests/e2e/Services/Account/AccountBase.php index 71353af182..a6681d3391 100644 --- a/tests/e2e/Services/Account/AccountBase.php +++ b/tests/e2e/Services/Account/AccountBase.php @@ -20,6 +20,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -42,6 +43,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -54,6 +56,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => '', 'password' => '', ]); @@ -65,6 +68,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => '', ]); @@ -76,6 +80,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => '', 'password' => $password, ]); @@ -106,6 +111,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -123,6 +129,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email.'x', 'password' => $password, ]); @@ -134,6 +141,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password.'x', ]); @@ -145,6 +153,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => '', 'password' => '', ]); @@ -482,6 +491,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => 'new-password', ]); @@ -601,6 +611,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $data['email'], 'password' => $data['password'], 'name' => $data['name'], @@ -844,6 +855,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -911,6 +923,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -993,6 +1006,7 @@ trait AccountBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 60ba506225..e7d866849d 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -68,6 +68,7 @@ class AccountCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -82,6 +83,7 @@ class AccountCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -124,6 +126,7 @@ class AccountCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -147,6 +150,7 @@ class AccountCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -161,6 +165,7 @@ class AccountCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -325,6 +330,7 @@ class AccountCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password ]); @@ -368,6 +374,7 @@ class AccountCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); diff --git a/tests/e2e/Services/Account/AccountCustomServerTest.php b/tests/e2e/Services/Account/AccountCustomServerTest.php index 6574b6603d..fabbc5b77f 100644 --- a/tests/e2e/Services/Account/AccountCustomServerTest.php +++ b/tests/e2e/Services/Account/AccountCustomServerTest.php @@ -26,6 +26,7 @@ class AccountCustomServerTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'], ], [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index 4614b1aabe..6ad03abd65 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -320,6 +320,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $id, ]), [ + 'userIdId' => 'unique()', 'email' => $originalEmail, 'password' => $originalPassword, 'name' => $originalName, @@ -330,6 +331,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $id, ]), [ + 'sessionId' => 'unique()', 'email' => $originalEmail, 'password' => $originalPassword, ]); @@ -372,6 +374,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $id, ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -419,6 +422,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $id, ]), [ + 'sessionId' => 'unique()', 'email' => $originalEmail, 'password' => $originalPassword, ]); @@ -479,6 +483,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $id, ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -504,6 +509,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $id, ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -665,6 +671,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $id, ]), [ + 'teamId' => 'unique()', 'name' => 'Arsenal' ]); diff --git a/tests/e2e/Services/Webhooks/WebhooksCustomClientTest.php b/tests/e2e/Services/Webhooks/WebhooksCustomClientTest.php index 57e9ed2457..2786d2c82d 100644 --- a/tests/e2e/Services/Webhooks/WebhooksCustomClientTest.php +++ b/tests/e2e/Services/Webhooks/WebhooksCustomClientTest.php @@ -27,6 +27,7 @@ class WebhooksCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -77,6 +78,7 @@ class WebhooksCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name, @@ -87,6 +89,7 @@ class WebhooksCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -143,6 +146,7 @@ class WebhooksCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -209,6 +213,7 @@ class WebhooksCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -281,6 +286,7 @@ class WebhooksCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); @@ -339,6 +345,7 @@ class WebhooksCustomClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ]), [ + 'sessionId' => 'unique()', 'email' => $email, 'password' => $password, ]); diff --git a/tests/e2e/Services/Workers/WebhooksTest.php b/tests/e2e/Services/Workers/WebhooksTest.php index 98ed37af07..6688bf8bf4 100644 --- a/tests/e2e/Services/Workers/WebhooksTest.php +++ b/tests/e2e/Services/Workers/WebhooksTest.php @@ -133,6 +133,7 @@ class WebhooksTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, ]), [ + 'userId' => 'unique()', 'email' => $email, 'password' => $password, 'name' => $name,