1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-06-20 22:54:05 +00:00

Merge branch 'master' into fix-cache-invalidation

This commit is contained in:
lukinovec 2026-06-01 14:42:45 +02:00 committed by GitHub
commit fd1aa2439a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 3 deletions

View file

@ -31,10 +31,12 @@ class PreventAccessFromUnwantedDomains
{ {
$route = tenancy()->getRoute($request); $route = tenancy()->getRoute($request);
if ($this->shouldBeSkipped($route) || tenancy()->routeIsUniversal($route)) { if ($this->shouldBeSkipped($route)) {
return $next($request); return $next($request);
} }
// If the route is universal, neither of these checks will pass and the logic will
// fall through to the $next($request) call at the end.
if ($this->accessingTenantRouteFromCentralDomain($request, $route) || $this->accessingCentralRouteFromTenantDomain($request, $route)) { if ($this->accessingTenantRouteFromCentralDomain($request, $route) || $this->accessingCentralRouteFromTenantDomain($request, $route)) {
$abortRequest = static::$abortRequest ?? function () { $abortRequest = static::$abortRequest ?? function () {
abort(404); abort(404);

View file

@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Resolvers;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Stancl\Tenancy\Contracts\Domain; use Stancl\Tenancy\Contracts\Domain;
use Stancl\Tenancy\Contracts\SingleDomainTenant; use Stancl\Tenancy\Contracts\SingleDomainTenant;
@ -58,7 +59,19 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver
public static function isSubdomain(string $domain): bool public static function isSubdomain(string $domain): bool
{ {
return Str::endsWith($domain, config('tenancy.identification.central_domains')); $centralDomains = Arr::wrap(config('tenancy.identification.central_domains'));
if (in_array($domain, $centralDomains, true)) {
return false;
}
foreach ($centralDomains as $centralDomain) {
if (Str::endsWith($domain, '.' . $centralDomain)) {
return true;
}
}
return false;
} }
public function resolved(Tenant $tenant, mixed ...$args): void public function resolved(Tenant $tenant, mixed ...$args): void

View file

@ -300,7 +300,7 @@ test('using different default route modes works with global domain identificatio
$exception = match ($middleware) { $exception = match ($middleware) {
InitializeTenancyByDomain::class => TenantCouldNotBeIdentifiedOnDomainException::class, InitializeTenancyByDomain::class => TenantCouldNotBeIdentifiedOnDomainException::class,
InitializeTenancyBySubdomain::class => NotASubdomainException::class, InitializeTenancyBySubdomain::class => NotASubdomainException::class,
InitializeTenancyByDomainOrSubdomain::class => NotASubdomainException::class, InitializeTenancyByDomainOrSubdomain::class => TenantCouldNotBeIdentifiedOnDomainException::class,
}; };
expect(fn () => $this->withoutExceptionHandling()->get('http://localhost/central-route'))->toThrow($exception); expect(fn () => $this->withoutExceptionHandling()->get('http://localhost/central-route'))->toThrow($exception);

View file

@ -7,6 +7,7 @@ use Stancl\Tenancy\Database\Concerns\HasDomains;
use Stancl\Tenancy\Exceptions\NotASubdomainException; use Stancl\Tenancy\Exceptions\NotASubdomainException;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain; use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Database\Models; use Stancl\Tenancy\Database\Models;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
use function Stancl\Tenancy\Tests\pest; use function Stancl\Tenancy\Tests\pest;
beforeEach(function () { beforeEach(function () {
@ -108,6 +109,14 @@ test('we cant use a subdomain that doesnt belong to our central domains', functi
->get('http://foo.localhost/foo/abc/xyz'); ->get('http://foo.localhost/foo/abc/xyz');
}); });
test('domain resolver correctly determines if string is a subdomain', function() {
config(['tenancy.identification.central_domains' => ['site.com', 'blog.site.com']]);
expect(DomainTenantResolver::isSubdomain('blog.site.com'))->toBeFalse();
expect(DomainTenantResolver::isSubdomain('tenant.site.com'))->toBeTrue();
expect(DomainTenantResolver::isSubdomain('tenantsite.com'))->toBeFalse();
});
class SubdomainTenant extends Models\Tenant class SubdomainTenant extends Models\Tenant
{ {
use HasDomains; use HasDomains;