1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 17:24:03 +00:00

Single-domain tenants (#16)

* Add SingleDomainTenant

* Add logic for single domain tenants

* Test that the single domain approach works (wip)

* Fix code style (php-cs-fixer)

* Simplify SubdomainTest tests

* Add single domain tenant conditions to DomainTenantResolver

* Test single domain tenants in resolver test

* Fix test name typo

* Improve runUsingBothDomainApproaches()

* Delete extra tenancy()->end()

* Test early identification with both domain approaches

* Test that things work with both domain approaches in the rest of the tests

* Fix falsely passing test

* Fix PHPStan errors

* Change SingleDomainTenant to a contract, add SingleDomainTenant test model

* Fix TenantList domainsCLI()

* Improve setCurrentDomain() check

* Fix code style (php-cs-fixer)

* Add annotation

* Revert getCustomColumns() change

* Add comments

* Use the domain returned by the closure in runUsingBoth..()

* Delete `migrate` from test

* Improve test names

* Use variable instead of repeating the same string multiple times

* Update comment

* Add comment

* Clean up PreventAccess test

* Don't assign domain to a single-use variable

* Update comments

* Uncomment datasets

* Add todo

* Fix user impersonation test

* Don't specify tenant key when creating tenant in runUsingBoth..()

* Improve universal route test

* Improve `runUsingBothDomainApproaches()`

* Add tests specific to single domain tenants

* Get rid of the runUsingBothDomainApproaches method

* Add test file specific for the single domain tenant feature

* Rename test

* Make getCustomColumns() function static

* Positiopn datasets differently

* Fix early id test

* Add prevent MW to route MW in test

* Fix single domain tenant tests

* Delete SingleDomainTenantTest (CI testing)

* Add the test file back

* TUrn APP_DEBUG on temporarily

* Turn debug off

* Try creating tenant with non-unique domain (CI testing)

* dd duplicate tenant records

* Revert testing change

* Make SingleDomainTenant not extend base tenant (VirtualColumn issues)

* Fix early id test

* add todo

* Use dev-master stancl/virtualcolumn

* Make SingleDomainTenant extend the tenant base model

* remove todo

* Clean up EarlyIdentificationTest changes

* Finish test file cleanup

* Fix test

* improve test

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
lukinovec 2023-11-08 11:38:26 +01:00 committed by GitHub
parent c34952f328
commit e25e7b7961
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 243 additions and 14 deletions

View file

@ -66,7 +66,7 @@ abstract class CachedTenantResolver implements TenantResolver
*
* @return array[]
*/
abstract public function getArgsForTenant(Tenant $tenant): array;
abstract public function getArgsForTenant(Tenant $tenant): array; // todo@v4 make it clear that this is only used for cache *invalidation*
public static function shouldCache(): bool
{

View file

@ -7,30 +7,40 @@ namespace Stancl\Tenancy\Resolvers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\Domain;
use Stancl\Tenancy\Contracts\SingleDomainTenant;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
class DomainTenantResolver extends Contracts\CachedTenantResolver
{
/** The model representing the domain that the tenant was identified on. */
public static Domain $currentDomain; // todo |null?
public static Domain|null $currentDomain = null;
public function resolveWithoutCache(mixed ...$args): Tenant
{
$domain = $args[0];
$tenant = config('tenancy.models.tenant')::query()
->whereHas('domains', fn (Builder $query) => $query->where('domain', $domain))
->with('domains')
->first();
/** @var Tenant&Model $tenantModel */
$tenantModel = config('tenancy.models.tenant')::make();
if ($tenantModel instanceof SingleDomainTenant) {
$tenant = $tenantModel->newQuery()
->firstWhere('domain', $domain);
} else {
$tenant = $tenantModel->newQuery()
->whereHas('domains', fn (Builder $query) => $query->where('domain', $domain))
->with('domains')
->first();
}
/** @var (Tenant&Model)|null $tenant */
if ($tenant) {
$this->setCurrentDomain($tenant, $domain);
return $tenant;
}
throw new TenantCouldNotBeIdentifiedOnDomainException($args[0]);
throw new TenantCouldNotBeIdentifiedOnDomainException($domain);
}
public function resolved(Tenant $tenant, mixed ...$args): void
@ -41,11 +51,21 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver
protected function setCurrentDomain(Tenant $tenant, string $domain): void
{
/** @var Tenant&Model $tenant */
static::$currentDomain = $tenant->domains->where('domain', $domain)->first();
if (! $tenant instanceof SingleDomainTenant) {
static::$currentDomain = $tenant->domains->where('domain', $domain)->first();
}
}
public function getArgsForTenant(Tenant $tenant): array
{
if ($tenant instanceof SingleDomainTenant) {
/** @var SingleDomainTenant&Model $tenant */
return [
[$tenant->getOriginal('domain')], // Previous domain
[$tenant->domain], // Current domain
];
}
/** @var Tenant&Model $tenant */
$tenant->unsetRelation('domains');