Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Resolvers/DomainTenantResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Stancl\Tenancy\Contracts\Domain;
use Stancl\Tenancy\Contracts\SingleDomainTenant;
Expand Down Expand Up @@ -58,7 +59,19 @@ public static function findTenantByDomain(string $domain): (Tenant&Model)|null

public static function isSubdomain(string $domain): bool
{
return Str::endsWith($domain, config('tenancy.identification.central_domains'));
$centralDomains = Arr::wrap(config('tenancy.identification.central_domains'));

foreach ($centralDomains as $centralDomain) {
if ($domain === $centralDomain) {
return false;
}

if (Str::endsWith($domain, '.' . $centralDomain)) {
return true;
}
}

return false;
}

public function resolved(Tenant $tenant, mixed ...$args): void
Expand Down
2 changes: 1 addition & 1 deletion tests/EarlyIdentificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@
$exception = match ($middleware) {
InitializeTenancyByDomain::class => TenantCouldNotBeIdentifiedOnDomainException::class,
InitializeTenancyBySubdomain::class => NotASubdomainException::class,
InitializeTenancyByDomainOrSubdomain::class => NotASubdomainException::class,
InitializeTenancyByDomainOrSubdomain::class => TenantCouldNotBeIdentifiedOnDomainException::class,
};

expect(fn () => $this->withoutExceptionHandling()->get('http://localhost/central-route'))->toThrow($exception);
Expand Down
8 changes: 8 additions & 0 deletions tests/SubdomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Stancl\Tenancy\Exceptions\NotASubdomainException;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Database\Models;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
use function Stancl\Tenancy\Tests\pest;

beforeEach(function () {
Expand Down Expand Up @@ -108,6 +109,13 @@
->get('http://foo.localhost/foo/abc/xyz');
});

test('domain resolver correctly determines if string is a subdomain', function() {
config(['tenancy.identification.central_domains' => ['app.test']]);

expect(DomainTenantResolver::isSubdomain('foo.app.test'))->toBeTrue();
expect(DomainTenantResolver::isSubdomain('fooapp.test'))->toBeFalse();
});

class SubdomainTenant extends Models\Tenant
{
use HasDomains;
Expand Down
Loading