Check if subdomain exists before creating site

This commit is contained in:
Khushboo Verma 2024-10-26 18:34:58 +02:00
parent e4dd9bc9f9
commit 6ac81dea19
2 changed files with 26 additions and 11 deletions

12
composer.lock generated
View file

@ -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",

View file

@ -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,