From 039b03a5b3dd7332c7389dbb37a042eca2c2ce87 Mon Sep 17 00:00:00 2001 From: Julien Nahum Date: Thu, 25 Jan 2024 05:51:18 +0100 Subject: [PATCH] Allow use of same domain in different workspaces --- .../Middleware/CustomDomainRestriction.php | 24 +++++-------------- client/middleware/custom-domain.global.js | 5 +--- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/app/Http/Middleware/CustomDomainRestriction.php b/app/Http/Middleware/CustomDomainRestriction.php index 7faff34..4aa2ca7 100644 --- a/app/Http/Middleware/CustomDomainRestriction.php +++ b/app/Http/Middleware/CustomDomainRestriction.php @@ -24,10 +24,6 @@ class CustomDomainRestriction $customDomain = $request->header(self::CUSTOM_DOMAIN_HEADER); if (!preg_match(CustomDomainRequest::CUSTOM_DOMAINS_REGEX, $customDomain)) { - \Log::info('[CUSTOM_DOMAIN]: Invalid domain', [ - 'domain' => $customDomain, - 'ip' => $request->ip(), - ]); return response()->json([ 'success' => false, 'message' => 'Invalid domain', @@ -42,11 +38,7 @@ class CustomDomainRestriction } // Check if domain is known - if (!$workspace = Workspace::whereJsonContains('custom_domains',$customDomain)->first()) { - \Log::info('[CUSTOM_DOMAIN]: Unknown domain', [ - 'domain' => $customDomain, - 'ip' => $request->ip(), - ]); + if (!$workspaces = Workspace::whereJsonContains('custom_domains',$customDomain)->get()) { return response()->json([ 'success' => false, 'message' => 'Unknown domain', @@ -54,16 +46,12 @@ class CustomDomainRestriction ], 420); } - \Log::info('[CUSTOM_DOMAIN]: Applying scopes', [ - 'domain' => $customDomain, - 'ip' => $request->ip(), - 'workspace' => $workspace->id, - ]); - Workspace::addGlobalScope('domain-restricted', function (Builder $builder) use ($workspace) { - $builder->where('workspaces.id', $workspace->id); + $workspacesIds = $workspaces->pluck('id')->toArray(); + Workspace::addGlobalScope('domain-restricted', function (Builder $builder) use ($workspacesIds) { + $builder->whereIn('id', $workspacesIds); }); - Form::addGlobalScope('domain-restricted', function (Builder $builder) use ($workspace) { - $builder->where('forms.workspace_id', $workspace->id); + Form::addGlobalScope('domain-restricted', function (Builder $builder) use ($workspacesIds) { + $builder->whereIn('workspace_id', $workspacesIds); }); return $next($request); diff --git a/client/middleware/custom-domain.global.js b/client/middleware/custom-domain.global.js index 376b29d..7f2b75f 100644 --- a/client/middleware/custom-domain.global.js +++ b/client/middleware/custom-domain.global.js @@ -41,10 +41,7 @@ export default defineNuxtRouteMiddleware((to, from) => { if (!customDomainAllowedRoutes.includes(to.name)) { // Custom domain only allowed for form url return redirectToMainDomain({ - reason: 'route_not_allowed', - route: to, - from: from, - customDomainAllowedRoutes: customDomainAllowedRoutes + reason: 'route_not_allowed' }) } })