Allow use of same domain in different workspaces

This commit is contained in:
Julien Nahum 2024-01-25 05:51:18 +01:00
parent 2dbbc38ba4
commit 039b03a5b3
2 changed files with 7 additions and 22 deletions

View File

@ -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);

View File

@ -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'
})
}
})