diff --git a/app/config/console.php b/app/config/console.php index 9a5df31866..bf1850182a 100644 --- a/app/config/console.php +++ b/app/config/console.php @@ -49,6 +49,7 @@ $console = [ 'disposableEmails' => false, 'canonicalEmails' => false, 'freeEmails' => false, + 'corporateEmails' => false, 'invalidateSessions' => true ], 'authWhitelistEmails' => (!empty(System::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null))) ? \explode(',', System::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null)) : [], diff --git a/app/config/errors.php b/app/config/errors.php index 0e5f6608b9..a51a83e529 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -246,6 +246,11 @@ return [ 'description' => 'This email address must already be in its canonical form. Please remove aliases, tags, or provider-specific variations and try again.', 'code' => 400, ], + Exception::USER_EMAIL_NOT_CORPORATE => [ + 'name' => Exception::USER_EMAIL_NOT_CORPORATE, + 'description' => 'Only corporate email addresses are allowed. Please use a work email address and try again.', + 'code' => 400, + ], Exception::USER_PASSWORD_MISMATCH => [ 'name' => Exception::USER_PASSWORD_MISMATCH, 'description' => 'Passwords do not match. Please check the password and confirm password.', diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 7ba94abf3b..a050e97b17 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -351,6 +351,10 @@ Http::post('/v1/account') throw new Exception(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !$emailMetadata['emailIsCorporate']) { + throw new Exception(Exception::USER_EMAIL_NOT_CORPORATE); + } + try { $userId = $userId == 'unique()' ? ID::unique() : $userId; $user->setAttributes([ @@ -1699,6 +1703,10 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect') $failureRedirect(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !$emailMetadata['emailIsCorporate']) { + $failureRedirect(Exception::USER_EMAIL_NOT_CORPORATE); + } + try { $userId = ID::unique(); $user->setAttributes([ @@ -1840,6 +1848,10 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect') $failureRedirect(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !$emailMetadata['emailIsCorporate']) { + $failureRedirect(Exception::USER_EMAIL_NOT_CORPORATE); + } + $user->setAttribute('email', $email); $user->setAttribute('emailCanonical', $emailMetadata['emailCanonical']); $user->setAttribute('emailIsCanonical', $emailMetadata['emailIsCanonical']); @@ -2198,6 +2210,10 @@ Http::post('/v1/account/tokens/magic-url') throw new Exception(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !$emailMetadata['emailIsCorporate']) { + throw new Exception(Exception::USER_EMAIL_NOT_CORPORATE); + } + $user->setAttributes([ '$id' => $userId, '$permissions' => [ @@ -2518,6 +2534,10 @@ Http::post('/v1/account/tokens/email') throw new Exception(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !$emailMetadata['emailIsCorporate']) { + throw new Exception(Exception::USER_EMAIL_NOT_CORPORATE); + } + $user->setAttributes([ '$id' => $userId, '$permissions' => [ @@ -3439,6 +3459,10 @@ Http::patch('/v1/account/email') throw new Exception(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !$emailMetadata['emailIsCorporate']) { + throw new Exception(Exception::USER_EMAIL_NOT_CORPORATE); + } + $user ->setAttribute('email', $email) ->setAttribute('emailVerification', false) // After this user needs to confirm mail again diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index c2e0735c09..9d95f45049 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -149,6 +149,10 @@ function createUser(Hash $hash, string $userId, ?string $email, ?string $passwor throw new Exception(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !($emailMetadata['emailIsCorporate'] ?? true)) { + throw new Exception(Exception::USER_EMAIL_NOT_CORPORATE); + } + $hashedPassword = null; $isHashed = !$hash instanceof Plaintext; @@ -1613,6 +1617,10 @@ Http::patch('/v1/users/:userId/email') throw new Exception(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !($emailMetadata['emailIsCorporate'] ?? true)) { + throw new Exception(Exception::USER_EMAIL_NOT_CORPORATE); + } + $user ->setAttribute('email', $email) ->setAttribute('emailVerification', false) diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index afe6f9fe21..349cff11de 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -85,6 +85,7 @@ class Exception extends \Exception public const string USER_EMAIL_ALREADY_EXISTS = 'user_email_already_exists'; public const string USER_EMAIL_DISPOSABLE = 'user_email_disposable'; public const string USER_EMAIL_FREE = 'user_email_free'; + public const string USER_EMAIL_NOT_CORPORATE = 'user_email_not_corporate'; public const string USER_EMAIL_NOT_CANONICAL = 'user_email_not_canonical'; public const string USER_PASSWORD_MISMATCH = 'user_password_mismatch'; public const string USER_SESSION_NOT_FOUND = 'user_session_not_found'; diff --git a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php index 785fddf233..90c3781701 100644 --- a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php @@ -94,6 +94,7 @@ class Create extends Action 'disposableEmails' => false, 'canonicalEmails' => false, 'freeEmails' => false, + 'corporateEmails' => false, 'mockNumbers' => [], 'sessionAlerts' => false, 'membershipsUserName' => false, diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php index 1560788d09..28405fe1da 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php @@ -97,6 +97,7 @@ class Create extends Action 'disposableEmails' => false, 'canonicalEmails' => false, 'freeEmails' => false, + 'corporateEmails' => false, 'mockNumbers' => [], 'sessionAlerts' => false, 'membershipsUserName' => false, diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php index 3bffb091ba..19d9237ff1 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php @@ -204,6 +204,10 @@ class Create extends Action throw new Exception(Exception::USER_EMAIL_FREE); } + if ((($project->getId() === 'console') || ($plan['supportsCorporateEmailValidation'] ?? false)) && ($project->getAttribute('auths', [])['corporateEmails'] ?? false) && !($emailMetadata['emailIsCorporate'] ?? true)) { + throw new Exception(Exception::USER_EMAIL_NOT_CORPORATE); + } + $hash = $proofForPassword->hash($proofForPassword->generate()); $userId = ID::unique(); diff --git a/src/Appwrite/Utopia/Response/Filters/V26.php b/src/Appwrite/Utopia/Response/Filters/V26.php index 30b4e7dacb..0f53290749 100644 --- a/src/Appwrite/Utopia/Response/Filters/V26.php +++ b/src/Appwrite/Utopia/Response/Filters/V26.php @@ -51,6 +51,7 @@ class V26 extends Filter $content['authDisposableEmails'] = $auths->getAttribute('disposableEmails', false); $content['authCanonicalEmails'] = $auths->getAttribute('canonicalEmails', false); $content['authFreeEmails'] = $auths->getAttribute('freeEmails', false); + $content['authCorporateEmails'] = $auths->getAttribute('corporateEmails', false); $content['authMockNumbers'] = $auths->getAttribute('mockNumbers', []); $content['authSessionAlerts'] = $auths->getAttribute('sessionAlerts', false); $content['authMembershipsUserName'] = $auths->getAttribute('membershipsUserName', false);