1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-08-06 06:54:03 +00:00
tenancy/src/Resolvers/DomainTenantResolver.php
lukinovec ec06dcc52e
Correct DomainTenantResolver::isSubdomain() check (#1425)
Added a failing test for determining if a host is a subdomain, then
fixed `DomainTenantResolver::isSubdomain()` (similar fix as in #1423)
and a related assertion.

Previously, while having `tenancy.identification.central_domains` set to
e.g. `['site.com']`, the `isSubdomain()` check consider `tenantsite.com`
a subdomain because it ends with `site.com`. Now, instead of the
`endsWith()` check, the method checks if the passed domain is in the
configured central domains. If it is, it returns `false`. Otherwise,
loop through all the central domains and check if the passed domain
matches any of the central domains prefixed with a dot (e.g.
`tenant.site.com` would be considered a subdomain, `tenant.site.com`
wouldn't).

Because in InitializeTenancyByDomainOrSubdomain, if tenancy fails to
initialize using a subdomain (before this PR's changes, e.g.
`tenantsite.com` would be considered a subdomain, and `tenantsite` would
be used for initializing tenancy), it'll catch the exception and use the
whole domain for identification instead, this error will likely never be
noticed in real-world usage. So this PR corrects the subdomain detection
logic, but the real-world impact of that is negligible.

> Note: The subdomain error catching logic in domainOrSubdomain ID MW
was added in v4. If we applied this change in v3, it'd fix a real issue
where domainOrSubdomain ID MW would just fail at the subdomain
initialization, without attempting domain initialization after the
failure.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Samuel Štancl <samuel@archte.ch>
2026-05-11 14:26:06 +02:00

109 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Resolvers;
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;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
use Stancl\Tenancy\Tenancy;
class DomainTenantResolver extends Contracts\CachedTenantResolver
{
/** The model representing the domain that the tenant was identified on. */
public static Domain|null $currentDomain = null;
public function resolveWithoutCache(mixed ...$args): Tenant
{
$domain = $args[0];
$tenant = static::findTenantByDomain($domain);
/** @var (Tenant&Model)|null $tenant */
if ($tenant) {
static::setCurrentDomain($tenant, $domain);
return $tenant;
}
throw new TenantCouldNotBeIdentifiedOnDomainException($domain);
}
public static function findTenantByDomain(string $domain): (Tenant&Model)|null
{
/** @var Tenant&Model $tenantModel */
$tenantModel = tenancy()->model();
if ($tenantModel instanceof SingleDomainTenant) {
$tenant = $tenantModel->newQuery()
->with(Tenancy::$findWith)
->firstWhere('domain', $domain);
} else {
$tenant = $tenantModel->newQuery()
->whereHas('domains', fn (Builder $query) => $query->where('domain', $domain))
->with(array_unique(array_merge(Tenancy::$findWith, ['domains'])))
->first();
}
/** @var (Tenant&Model)|null $tenant */
return $tenant;
}
public static function isSubdomain(string $domain): bool
{
$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
{
$this->setCurrentDomain($tenant, $args[0]);
}
protected static function setCurrentDomain(Tenant $tenant, string $domain): void
{
/** @var Tenant&Model $tenant */
if (! $tenant instanceof SingleDomainTenant) {
static::$currentDomain = $tenant->domains->where('domain', $domain)->first();
}
}
public function getPossibleCacheKeys(Tenant&Model $tenant): array
{
$domains = [];
if ($tenant instanceof SingleDomainTenant) {
$domains = array_filter([
$tenant->getOriginal('domain'), // Previous domain
$tenant->domain, // Current domain
]);
} elseif (method_exists($tenant, 'domains') && $tenant->domains() instanceof Relation) {
$tenant->unsetRelation('domains');
$domains = $tenant->domains->map(function (Domain&Model $domain) {
return $domain->domain;
})->toArray();
}
return array_map(fn (string $domain) => $this->formatCacheKey($domain), $domains);
}
}