From 6ac81dea19a383be291800cb6412f1dca593d86b Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Sat, 26 Oct 2024 18:34:58 +0200 Subject: [PATCH] Check if subdomain exists before creating site --- composer.lock | 12 ++++----- .../Modules/Sites/Http/Sites/CreateSite.php | 25 +++++++++++++++---- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/composer.lock b/composer.lock index a7e95a95ca..91e9210fd8 100644 --- a/composer.lock +++ b/composer.lock @@ -157,16 +157,16 @@ }, { "name": "appwrite/php-runtimes", - "version": "0.16.2", + "version": "0.16.4", "source": { "type": "git", "url": "https://github.com/appwrite/runtimes.git", - "reference": "c33005e3eaaf2d427e9fd1077d5335e31f4d36f9" + "reference": "7e4741337b9373f77210396e68eca539018cabd1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/runtimes/zipball/c33005e3eaaf2d427e9fd1077d5335e31f4d36f9", - "reference": "c33005e3eaaf2d427e9fd1077d5335e31f4d36f9", + "url": "https://api.github.com/repos/appwrite/runtimes/zipball/7e4741337b9373f77210396e68eca539018cabd1", + "reference": "7e4741337b9373f77210396e68eca539018cabd1", "shasum": "" }, "require": { @@ -206,9 +206,9 @@ ], "support": { "issues": "https://github.com/appwrite/runtimes/issues", - "source": "https://github.com/appwrite/runtimes/tree/0.16.2" + "source": "https://github.com/appwrite/runtimes/tree/0.16.4" }, - "time": "2024-10-09T15:02:52+00:00" + "time": "2024-10-26T10:39:59+00:00" }, { "name": "beberlei/assert", diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/CreateSite.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/CreateSite.php index bf4ac62aeb..18daaeae53 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/CreateSite.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/CreateSite.php @@ -18,6 +18,7 @@ use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; +use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -95,6 +96,25 @@ class CreateSite extends Base public function action(string $siteId, string $name, string $framework, bool $enabled, string $installCommand, string $buildCommand, string $outputDirectory, string $fallbackRedirect, string $subDomain, array $scopes, string $installationId, string $providerRepositoryId, string $providerBranch, bool $providerSilentMode, string $providerRootDirectory, string $templateRepository, string $templateOwner, string $templateRootDirectory, string $templateVersion, string $specification, Request $request, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Build $queueForBuilds, Database $dbForConsole, GitHub $github) { + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + $ruleId = ''; + $routeSubdomain = ''; + $domain = ''; + + if (!empty($sitesDomain)) { + $ruleId = ID::unique(); + $routeSubdomain = $subDomain ?? ID::unique(); + $domain = "{$routeSubdomain}.{$sitesDomain}"; + + $subDomain = Authorization::skip(fn () => $dbForConsole->findOne('rules', [ + Query::equal('domain', [$domain]) + ])); + + if (!empty($subDomain)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Subdomain already exists. Please choose a different subdomain.'); + } + } + $siteId = ($siteId == 'unique()') ? ID::unique() : $siteId; $allowList = \array_filter(\explode(',', System::getEnv('_APP_SITES_FRAMEWORKS', ''))); @@ -213,12 +233,7 @@ class CreateSite extends Base ->setTemplate($template); } - $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); if (!empty($sitesDomain)) { - $ruleId = ID::unique(); - $routeSubdomain = $subDomain ?? ID::unique(); - $domain = "{$routeSubdomain}.{$sitesDomain}"; - $rule = Authorization::skip( fn () => $dbForConsole->createDocument('rules', new Document([ '$id' => $ruleId,