From 4142ffdb708257f81048e57fae64f0c82904dd75 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 19 Jan 2023 17:26:41 +0530 Subject: [PATCH 1/2] feat: update account codes --- app/config/errors.php | 9 +++++++-- app/console | 2 +- app/controllers/api/account.php | 6 +++++- src/Appwrite/Extend/Exception.php | 4 +++- tests/e2e/Services/Account/AccountConsoleClientTest.php | 4 ++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index a071b0cb7e..74e2de7db2 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -88,6 +88,11 @@ return [ 'description' => 'The request cannot be fulfilled with the current protocol. Please check the value of the _APP_OPTIONS_FORCE_HTTPS environment variable.', 'code' => 500, ], + Exception::GENERAL_CODES_DISABLED => [ + 'name' => Exception::GENERAL_CODES_DISABLED, + 'description' => 'Invitation codes are disabled on this server. Please contact the server administrator.', + 'code' => 500, + ], /** User Errors */ Exception::USER_COUNT_EXCEEDED => [ @@ -125,8 +130,8 @@ return [ 'description' => 'Console registration is restricted to specific emails. Contact your administrator for more information.', 'code' => 401, ], - Exception::USER_CODE_INVALID => [ - 'name' => Exception::USER_CODE_INVALID, + Exception::USER_INVALID_CODE => [ + 'name' => Exception::USER_INVALID_CODE, 'description' => 'The specified code is not valid. Contact your administrator for more information.', 'code' => 401, ], diff --git a/app/console b/app/console index 43891a526e..aea8c5f2bb 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit 43891a526e061454617cbb13def3c4901d99a7f1 +Subproject commit aea8c5f2bbe0836fc9fb4720b21d44fe01ac93d1 diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index e04b86e57e..634ba6e0f2 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -82,8 +82,12 @@ App::post('/v1/account/invite') $whitelistCodes = (!empty(App::getEnv('_APP_CONSOLE_WHITELIST_CODES', null))) ? \explode(',', App::getEnv('_APP_CONSOLE_WHITELIST_CODES', null)) : []; + if (empty($whitelistCodes)) { + throw new Exception(Exception::GENERAL_CODES_DISABLED); + } + if (!empty($whitelistCodes) && !\in_array($code, $whitelistCodes)) { - throw new Exception(Exception::USER_CODE_INVALID); + throw new Exception(Exception::USER_INVALID_CODE); } $limit = $project->getAttribute('auths', [])['limit'] ?? 0; diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 9f035863eb..8ee0dca0c6 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -50,6 +50,7 @@ class Exception extends \Exception public const GENERAL_CURSOR_NOT_FOUND = 'general_cursor_not_found'; public const GENERAL_SERVER_ERROR = 'general_server_error'; public const GENERAL_PROTOCOL_UNSUPPORTED = 'general_protocol_unsupported'; + public const GENERAL_CODES_DISABLED = 'general_codes_disabled'; /** Users */ public const USER_COUNT_EXCEEDED = 'user_count_exceeded'; @@ -60,7 +61,7 @@ class Exception extends \Exception public const USER_PASSWORD_RESET_REQUIRED = 'user_password_reset_required'; public const USER_EMAIL_NOT_WHITELISTED = 'user_email_not_whitelisted'; public const USER_IP_NOT_WHITELISTED = 'user_ip_not_whitelisted'; - public const USER_CODE_INVALID = 'user_code_invalid'; + public const USER_INVALID_CODE = 'user_invalid_code'; public const USER_INVALID_CREDENTIALS = 'user_invalid_credentials'; public const USER_ANONYMOUS_CONSOLE_PROHIBITED = 'user_anonymous_console_prohibited'; public const USER_SESSION_ALREADY_EXISTS = 'user_session_already_exists'; @@ -179,6 +180,7 @@ class Exception extends \Exception public const DOMAIN_VERIFICATION_FAILED = 'domain_verification_failed'; protected $type = ''; + protected $errors = []; public function __construct(string $type = Exception::GENERAL_UNKNOWN, string $message = null, int $code = null, \Throwable $previous = null) { diff --git a/tests/e2e/Services/Account/AccountConsoleClientTest.php b/tests/e2e/Services/Account/AccountConsoleClientTest.php index 4258004ecf..9cf0ba68b4 100644 --- a/tests/e2e/Services/Account/AccountConsoleClientTest.php +++ b/tests/e2e/Services/Account/AccountConsoleClientTest.php @@ -38,7 +38,7 @@ class AccountConsoleClientTest extends Scope ]); $this->assertEquals($response['headers']['status-code'], 401); - $this->assertEquals($response['body']['type'], Exception::USER_CODE_INVALID); + $this->assertEquals($response['body']['type'], Exception::USER_INVALID_CODE); $response = $this->client->call(Client::METHOD_POST, '/account/invite', array_merge([ 'origin' => 'http://localhost', @@ -52,7 +52,7 @@ class AccountConsoleClientTest extends Scope ]); $this->assertEquals($response['headers']['status-code'], 401); - $this->assertEquals($response['body']['type'], Exception::USER_CODE_INVALID); + $this->assertEquals($response['body']['type'], Exception::GENERAL_CODES_DISABLED); /** * Test for SUCCESS From 222421d8f3b65dbe82245bf2f7aa7f6286d678c5 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 19 Jan 2023 17:41:15 +0530 Subject: [PATCH 2/2] feat: update account codes --- tests/e2e/Services/Account/AccountConsoleClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Account/AccountConsoleClientTest.php b/tests/e2e/Services/Account/AccountConsoleClientTest.php index 9cf0ba68b4..69bb503428 100644 --- a/tests/e2e/Services/Account/AccountConsoleClientTest.php +++ b/tests/e2e/Services/Account/AccountConsoleClientTest.php @@ -52,7 +52,7 @@ class AccountConsoleClientTest extends Scope ]); $this->assertEquals($response['headers']['status-code'], 401); - $this->assertEquals($response['body']['type'], Exception::GENERAL_CODES_DISABLED); + $this->assertEquals($response['body']['type'], Exception::USER_INVALID_CODE); /** * Test for SUCCESS