From 3ef0b6e7cb3333de4b3c2668d24fc5e5c4a73256 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 30 May 2021 12:48:51 +0545 Subject: [PATCH 1/4] endpoint to update user email verification --- app/controllers/api/users.php | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 03543b7cd3..1d14ec1126 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -8,6 +8,7 @@ use Utopia\Validator\WhiteList; use Appwrite\Network\Validator\Email; use Utopia\Validator\Text; use Utopia\Validator\Range; +use Utopia\Validator\Boolean; use Utopia\Audit\Audit; use Utopia\Audit\Adapters\MySQL as AuditAdapter; use Appwrite\Auth\Auth; @@ -368,6 +369,43 @@ App::patch('/v1/users/:userId/status') $response->dynamic($user, Response::MODEL_USER); }); +App::patch('/v1/users/:userId/verification') + ->desc('Update Email Verification') + ->groups(['api', 'users']) + ->label('event', 'users.update.verification') + ->label('scope', 'users.write') + ->label('sdk.auth', [APP_AUTH_TYPE_KEY]) + ->label('sdk.namespace', 'users') + ->label('sdk.method', 'updateVerification') + ->label('sdk.description', '/docs/references/users/update-user-verification.md') + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_USER) + ->param('userId', '', new UID(), 'User unique ID.') + ->param('status', false, new Boolean(), 'User Email Verification Status.') + ->inject('response') + ->inject('projectDB') + ->action(function ($userId, $status, $response, $projectDB) { + /** @var Appwrite\Utopia\Response $response */ + /** @var Appwrite\Database\Database $projectDB */ + + $user = $projectDB->getDocument($userId); + + if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) { + throw new Exception('User not found', 404); + } + + $user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [ + 'emailVerification' => $status, + ])); + + if (false === $user) { + throw new Exception('Failed saving user to DB', 500); + } + + $response->dynamic($user, Response::MODEL_USER); + }); + App::patch('/v1/users/:userId/prefs') ->desc('Update User Preferences') ->groups(['api', 'users']) From deeeba7477c2b8e909dea21db399cf5d90f53db2 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 30 May 2021 12:50:54 +0545 Subject: [PATCH 2/4] reference --- docs/references/users/update-user-verification.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/references/users/update-user-verification.md diff --git a/docs/references/users/update-user-verification.md b/docs/references/users/update-user-verification.md new file mode 100644 index 0000000000..750097b54b --- /dev/null +++ b/docs/references/users/update-user-verification.md @@ -0,0 +1 @@ +Update the user email verification status by its unique ID. \ No newline at end of file From 20e4745786d2d5a9884891283d785b44a1e0dbdf Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 31 May 2021 11:35:43 +0545 Subject: [PATCH 3/4] fixed param name --- app/controllers/api/users.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 1d14ec1126..1f628e223c 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -382,10 +382,10 @@ App::patch('/v1/users/:userId/verification') ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_USER) ->param('userId', '', new UID(), 'User unique ID.') - ->param('status', false, new Boolean(), 'User Email Verification Status.') + ->param('emailVerification', false, new Boolean(), 'User Email Verification Status.') ->inject('response') ->inject('projectDB') - ->action(function ($userId, $status, $response, $projectDB) { + ->action(function ($userId, $emailVerification, $response, $projectDB) { /** @var Appwrite\Utopia\Response $response */ /** @var Appwrite\Database\Database $projectDB */ @@ -396,7 +396,7 @@ App::patch('/v1/users/:userId/verification') } $user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [ - 'emailVerification' => $status, + 'emailVerification' => $emailVerification, ])); if (false === $user) { From 0409dd5c32c26bdcde7596edea45153a97d8892c Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 31 May 2021 11:35:52 +0545 Subject: [PATCH 4/4] added test for verification update --- tests/e2e/Services/Users/UsersBase.php | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index ebefa2abb9..be64204749 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -122,6 +122,35 @@ trait UsersBase return $data; } + /** + * @depends testGetUser + */ + public function testUpdateEmailVerification(array $data):array + { + /** + * Test for SUCCESS + */ + $user = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/verification', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'emailVerification' => true, + ]); + + $this->assertEquals($user['headers']['status-code'], 200); + $this->assertEquals($user['body']['emailVerification'], true); + + $user = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals($user['headers']['status-code'], 200); + $this->assertEquals($user['body']['emailVerification'], true); + + return $data; + } + /** * @depends testGetUser */