mirror of
https://github.com/archtechx/tenancy.git
synced 2026-06-20 22:54:05 +00:00
Merge branch 'master' into override-toroute
This commit is contained in:
commit
a5b311105f
22 changed files with 588 additions and 89 deletions
|
|
@ -53,8 +53,6 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
])->send(function (Events\TenantCreated $event) {
|
])->send(function (Events\TenantCreated $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->shouldBeQueued(false),
|
})->shouldBeQueued(false),
|
||||||
|
|
||||||
// Listeners\CreateTenantStorage::class,
|
|
||||||
],
|
],
|
||||||
Events\SavingTenant::class => [],
|
Events\SavingTenant::class => [],
|
||||||
Events\TenantSaved::class => [],
|
Events\TenantSaved::class => [],
|
||||||
|
|
@ -63,12 +61,11 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Events\DeletingTenant::class => [
|
Events\DeletingTenant::class => [
|
||||||
JobPipeline::make([
|
JobPipeline::make([
|
||||||
Jobs\DeleteDomains::class,
|
Jobs\DeleteDomains::class,
|
||||||
|
// Jobs\DeleteTenantStorage::class,
|
||||||
// Jobs\RemoveStorageSymlinks::class,
|
// Jobs\RemoveStorageSymlinks::class,
|
||||||
])->send(function (Events\DeletingTenant $event) {
|
])->send(function (Events\DeletingTenant $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->shouldBeQueued(false),
|
})->shouldBeQueued(false),
|
||||||
|
|
||||||
// Listeners\DeleteTenantStorage::class,
|
|
||||||
],
|
],
|
||||||
Events\TenantDeleted::class => [
|
Events\TenantDeleted::class => [
|
||||||
JobPipeline::make([
|
JobPipeline::make([
|
||||||
|
|
|
||||||
|
|
@ -80,8 +80,8 @@ services:
|
||||||
mssql:
|
mssql:
|
||||||
image: mcr.microsoft.com/mssql/server:2022-latest
|
image: mcr.microsoft.com/mssql/server:2022-latest
|
||||||
environment:
|
environment:
|
||||||
- ACCEPT_EULA=Y
|
ACCEPT_EULA: "Y"
|
||||||
- SA_PASSWORD=P@ssword # must be the same as TENANCY_TEST_SQLSRV_PASSWORD
|
SA_PASSWORD: "P@ssword" # must be the same as TENANCY_TEST_SQLSRV_PASSWORD
|
||||||
healthcheck: # https://github.com/Microsoft/mssql-docker/issues/133#issuecomment-1995615432
|
healthcheck: # https://github.com/Microsoft/mssql-docker/issues/133#issuecomment-1995615432
|
||||||
test: timeout 2 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/1433'
|
test: timeout 2 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/1433'
|
||||||
interval: 10s
|
interval: 10s
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ trait HasTenantOptions
|
||||||
{
|
{
|
||||||
return array_merge([
|
return array_merge([
|
||||||
new InputOption('tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'The tenants to run this command for. Leave empty for all tenants', null),
|
new InputOption('tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'The tenants to run this command for. Leave empty for all tenants', null),
|
||||||
new InputOption('with-pending', null, InputOption::VALUE_NONE, 'Include pending tenants in query'), // todo@pending should we also offer without-pending? if we add this, mention in docs
|
new InputOption('with-pending', null, InputOption::VALUE_OPTIONAL, 'Include pending tenants in query if true/1, exclude if false/0. Defaults to the tenancy.pending.include_in_queries config value.'),
|
||||||
], parent::getOptions());
|
], parent::getOptions());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +43,11 @@ trait HasTenantOptions
|
||||||
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
|
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
|
||||||
})
|
})
|
||||||
->when(tenancy()->model()::hasGlobalScope(PendingScope::class), function ($query) {
|
->when(tenancy()->model()::hasGlobalScope(PendingScope::class), function ($query) {
|
||||||
$query->withPending(config('tenancy.pending.include_in_queries') ?: $this->option('with-pending'));
|
$includePending = $this->input->hasParameterOption('--with-pending')
|
||||||
|
? filter_var($this->option('with-pending') ?? true, FILTER_VALIDATE_BOOLEAN)
|
||||||
|
: config('tenancy.pending.include_in_queries');
|
||||||
|
|
||||||
|
$query->withPending($includePending);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,18 @@ trait HasPending
|
||||||
public static function bootHasPending(): void
|
public static function bootHasPending(): void
|
||||||
{
|
{
|
||||||
static::addGlobalScope(new PendingScope());
|
static::addGlobalScope(new PendingScope());
|
||||||
|
|
||||||
|
static::creating(function (self $tenant): void {
|
||||||
|
if ($tenant->pending()) {
|
||||||
|
event(new CreatingPendingTenant($tenant));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
static::created(function (self $tenant): void {
|
||||||
|
if ($tenant->pending()) {
|
||||||
|
event(new PendingTenantCreated($tenant));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Initialize the trait. */
|
/** Initialize the trait. */
|
||||||
|
|
@ -49,22 +61,11 @@ trait HasPending
|
||||||
*/
|
*/
|
||||||
public static function createPending(array $attributes = []): Model&Tenant
|
public static function createPending(array $attributes = []): Model&Tenant
|
||||||
{
|
{
|
||||||
$tenant = null;
|
return static::create(array_merge(
|
||||||
|
static::getPendingAttributes($attributes),
|
||||||
try {
|
$attributes,
|
||||||
$tenant = static::create(array_merge(static::getPendingAttributes($attributes), $attributes));
|
['pending_since' => now()->timestamp],
|
||||||
event(new CreatingPendingTenant($tenant));
|
));
|
||||||
} finally {
|
|
||||||
// Update the pending_since value only after the tenant is created so it's
|
|
||||||
// not marked as pending until after migrations, seeders, etc are run.
|
|
||||||
$tenant?->update([
|
|
||||||
'pending_since' => now()->timestamp,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
event(new PendingTenantCreated($tenant));
|
|
||||||
|
|
||||||
return $tenant;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class PendingScope implements Scope
|
||||||
/**
|
/**
|
||||||
* Apply the scope to a given Eloquent query builder.
|
* Apply the scope to a given Eloquent query builder.
|
||||||
*
|
*
|
||||||
* @param Builder<Model> $builder
|
* @param Builder<covariant Model> $builder
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
@ -58,8 +58,10 @@ class PendingScope implements Scope
|
||||||
{
|
{
|
||||||
$builder->macro('withoutPending', function (Builder $builder) {
|
$builder->macro('withoutPending', function (Builder $builder) {
|
||||||
$builder->withoutGlobalScope(static::class)
|
$builder->withoutGlobalScope(static::class)
|
||||||
->whereNull($builder->getModel()->getColumnForQuery('pending_since'))
|
->where(function (Builder $query) {
|
||||||
->orWhereNull($builder->getModel()->getDataColumn());
|
$query->whereNull($query->getModel()->getColumnForQuery('pending_since'))
|
||||||
|
->orWhereNull($query->getModel()->getDataColumn());
|
||||||
|
});
|
||||||
|
|
||||||
return $builder;
|
return $builder;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ use Illuminate\Database\Eloquent\Scope;
|
||||||
class ParentModelScope implements Scope
|
class ParentModelScope implements Scope
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param Builder<Model> $builder
|
* @param Builder<covariant Model> $builder
|
||||||
*/
|
*/
|
||||||
public function apply(Builder $builder, Model $model): void
|
public function apply(Builder $builder, Model $model): void
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ use Stancl\Tenancy\Tenancy;
|
||||||
class TenantScope implements Scope
|
class TenantScope implements Scope
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param Builder<Model> $builder
|
* @param Builder<covariant Model> $builder
|
||||||
*/
|
*/
|
||||||
public function apply(Builder $builder, Model $model)
|
public function apply(Builder $builder, Model $model)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Features;
|
namespace Stancl\Tenancy\Features;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
@ -61,9 +62,9 @@ class UserImpersonation implements Feature
|
||||||
|
|
||||||
Auth::guard($token->auth_guard)->loginUsingId($token->user_id, $token->remember);
|
Auth::guard($token->auth_guard)->loginUsingId($token->user_id, $token->remember);
|
||||||
|
|
||||||
$token->delete();
|
session()->put('tenancy_impersonation_guard', $token->auth_guard);
|
||||||
|
|
||||||
session()->put('tenancy_impersonating', true);
|
$token->delete();
|
||||||
|
|
||||||
return redirect($token->redirect_url);
|
return redirect($token->redirect_url);
|
||||||
}
|
}
|
||||||
|
|
@ -76,16 +77,30 @@ class UserImpersonation implements Feature
|
||||||
|
|
||||||
public static function isImpersonating(): bool
|
public static function isImpersonating(): bool
|
||||||
{
|
{
|
||||||
return session()->has('tenancy_impersonating');
|
return session()->has('tenancy_impersonation_guard');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logout from the current domain and forget impersonation session.
|
* Stop user impersonation by forgetting the impersonation session.
|
||||||
|
*
|
||||||
|
* When $logout is true, the user will also be logged out
|
||||||
|
* from the impersonation guard stored in the session.
|
||||||
|
*
|
||||||
|
* Throws an exception if impersonation is not active
|
||||||
|
* (= the impersonation guard is not in the session).
|
||||||
*/
|
*/
|
||||||
public static function stopImpersonating(): void
|
public static function stopImpersonating(bool $logout = true): void
|
||||||
{
|
{
|
||||||
auth()->logout();
|
if (! static::isImpersonating()) {
|
||||||
|
throw new Exception('Not currently impersonating any user.');
|
||||||
|
}
|
||||||
|
|
||||||
session()->forget('tenancy_impersonating');
|
if ($logout) {
|
||||||
|
$guard = session()->get('tenancy_impersonation_guard');
|
||||||
|
|
||||||
|
auth($guard)->logout();
|
||||||
|
}
|
||||||
|
|
||||||
|
session()->forget('tenancy_impersonation_guard');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,12 +22,34 @@ class DeleteDatabase implements ShouldQueue
|
||||||
protected TenantWithDatabase&Model $tenant,
|
protected TenantWithDatabase&Model $tenant,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
/** Skip database deletion if the create_database internal attribute is false. */
|
||||||
|
public static bool $skipWhenCreateDatabaseIsFalse = true;
|
||||||
|
|
||||||
|
/** Ignore exceptions thrown during database deletion and continue execution. */
|
||||||
|
public static bool $ignoreFailures = false;
|
||||||
|
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
|
if (static::$skipWhenCreateDatabaseIsFalse && $this->tenant->getInternal('create_database') === false) {
|
||||||
|
// If database creation was skipped, we presume deletion should also be skipped.
|
||||||
|
// To avoid this skip, either unset the `create_database` attribute (or make it true), or
|
||||||
|
// set the $skipWhenCreateDatabaseIsFalse static property to false.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event(new DeletingDatabase($this->tenant));
|
event(new DeletingDatabase($this->tenant));
|
||||||
|
|
||||||
$this->tenant->database()->manager()->deleteDatabase($this->tenant);
|
$deleted = false;
|
||||||
|
|
||||||
event(new DatabaseDeleted($this->tenant));
|
try {
|
||||||
|
$this->tenant->database()->manager()->deleteDatabase($this->tenant);
|
||||||
|
$deleted = true;
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
if (! static::$ignoreFailures) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($deleted) event(new DatabaseDeleted($this->tenant));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
43
src/Jobs/DeleteTenantStorage.php
Normal file
43
src/Jobs/DeleteTenantStorage.php
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Jobs;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\File;
|
||||||
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
||||||
|
class DeleteTenantStorage implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public Tenant $tenant,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
if (config('tenancy.filesystem.suffix_storage_path') === false) {
|
||||||
|
// Skip storage deletion if path suffixing is disabled
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$centralStoragePath = tenancy()->central(fn () => storage_path());
|
||||||
|
$tenantStoragePath = tenancy()->run($this->tenant, fn () => storage_path());
|
||||||
|
|
||||||
|
if ($tenantStoragePath === $centralStoragePath) {
|
||||||
|
// Check again to ensure the tenant storage path is distinct from the central storage path
|
||||||
|
// to avoid any accidental central storage path deletion
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_dir($tenantStoragePath)) {
|
||||||
|
File::deleteDirectory($tenantStoragePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,6 +17,16 @@ class MigrateDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should pending tenants be included while migrating,
|
||||||
|
* regardless of the tenancy.pending.include_in_queries config value.
|
||||||
|
*
|
||||||
|
* If false, pending tenants will be specifically excluded.
|
||||||
|
*
|
||||||
|
* If null, default to tenancy.pending.include_in_queries config.
|
||||||
|
*/
|
||||||
|
public static ?bool $includePending = true;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected TenantWithDatabase&Model $tenant,
|
protected TenantWithDatabase&Model $tenant,
|
||||||
) {}
|
) {}
|
||||||
|
|
@ -25,6 +35,7 @@ class MigrateDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
Artisan::call('tenants:migrate', [
|
Artisan::call('tenants:migrate', [
|
||||||
'--tenants' => [$this->tenant->getTenantKey()],
|
'--tenants' => [$this->tenant->getTenantKey()],
|
||||||
|
'--with-pending' => static::$includePending ?? config('tenancy.pending.include_in_queries'),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,16 @@ class SeedDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should pending tenants be included while seeding,
|
||||||
|
* regardless of the tenancy.pending.include_in_queries config value.
|
||||||
|
*
|
||||||
|
* If false, pending tenants will be specifically excluded.
|
||||||
|
*
|
||||||
|
* If null, default to tenancy.pending.include_in_queries config.
|
||||||
|
*/
|
||||||
|
public static ?bool $includePending = true;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected TenantWithDatabase&Model $tenant,
|
protected TenantWithDatabase&Model $tenant,
|
||||||
) {}
|
) {}
|
||||||
|
|
@ -25,6 +35,7 @@ class SeedDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
Artisan::call('tenants:seed', [
|
Artisan::call('tenants:seed', [
|
||||||
'--tenants' => [$this->tenant->getTenantKey()],
|
'--tenants' => [$this->tenant->getTenantKey()],
|
||||||
|
'--with-pending' => static::$includePending ?? config('tenancy.pending.include_in_queries'),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,7 @@ namespace Stancl\Tenancy\Listeners;
|
||||||
use Stancl\Tenancy\Events\Contracts\TenantEvent;
|
use Stancl\Tenancy\Events\Contracts\TenantEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Can be used to manually create framework directories in the tenant storage when storage_path() is scoped.
|
* @deprecated FilesystemTenancyBootstrapper creates the path automatically when suffix_storage_path is enabled.
|
||||||
*
|
|
||||||
* Useful when using real-time facades which use the framework/cache directory.
|
|
||||||
*
|
|
||||||
* Generally not needed anymore as the directory is also created by the FilesystemTenancyBootstrapper.
|
|
||||||
*/
|
*/
|
||||||
class CreateTenantStorage
|
class CreateTenantStorage
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,29 @@ namespace Stancl\Tenancy\Listeners;
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
use Stancl\Tenancy\Events\Contracts\TenantEvent;
|
use Stancl\Tenancy\Events\Contracts\TenantEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use Stancl\Tenancy\Jobs\DeleteTenantStorage in a job pipeline instead.
|
||||||
|
*/
|
||||||
class DeleteTenantStorage
|
class DeleteTenantStorage
|
||||||
{
|
{
|
||||||
public function handle(TenantEvent $event): void
|
public function handle(TenantEvent $event): void
|
||||||
{
|
{
|
||||||
$path = tenancy()->run($event->tenant, fn () => storage_path());
|
if (config('tenancy.filesystem.suffix_storage_path') === false) {
|
||||||
|
// Skip storage deletion if path suffixing is disabled
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_dir($path)) {
|
$centralStoragePath = tenancy()->central(fn () => storage_path());
|
||||||
File::deleteDirectory($path);
|
$tenantStoragePath = tenancy()->run($event->tenant, fn () => storage_path());
|
||||||
|
|
||||||
|
if ($tenantStoragePath === $centralStoragePath) {
|
||||||
|
// Check again to ensure the tenant storage path is distinct from the central storage path
|
||||||
|
// to avoid any accidental central storage path deletion
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_dir($tenantStoragePath)) {
|
||||||
|
File::deleteDirectory($tenantStoragePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
|
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
|
||||||
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
|
use Stancl\Tenancy\Jobs\DeleteTenantStorage;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
|
|
@ -184,21 +184,63 @@ test('create and delete storage symlinks jobs work', function() {
|
||||||
$this->assertDirectoryDoesNotExist(public_path("public-$tenantKey"));
|
$this->assertDirectoryDoesNotExist(public_path("public-$tenantKey"));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('tenant storage can get deleted after the tenant when DeletingTenant listens to DeleteTenantStorage', function() {
|
test('tenant storage gets deleted during tenant deletion when the DeletingTenant pipeline contains DeleteTenantStorage', function() {
|
||||||
Event::listen(DeletingTenant::class, DeleteTenantStorage::class);
|
Event::listen(DeletingTenant::class,
|
||||||
|
JobPipeline::make([DeleteTenantStorage::class])->send(function (DeletingTenant $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->shouldBeQueued(false)->toListener()
|
||||||
|
);
|
||||||
|
|
||||||
|
$centralStoragePath = storage_path();
|
||||||
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
|
// FilesystemTenancyBootstrapper not enabled,
|
||||||
|
// tenant and central storage path is the same,
|
||||||
|
// the storage deletion will be skipped.
|
||||||
|
$tenantStoragePath = storage_path();
|
||||||
|
expect($tenantStoragePath)->toBe($centralStoragePath);
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
|
tenant()->delete();
|
||||||
|
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class],
|
||||||
|
'tenancy.filesystem.suffix_storage_path' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
|
$tenantStoragePath = storage_path();
|
||||||
|
|
||||||
|
// FilesystemTenancyBootstrapper enabled,
|
||||||
|
// but tenant and central storage path is still the same
|
||||||
|
// because suffix_storage_path is false.
|
||||||
|
// The storage deletion will be skipped.
|
||||||
|
expect($tenantStoragePath)->toBe($centralStoragePath);
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
|
tenant()->delete();
|
||||||
|
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class],
|
||||||
|
'tenancy.filesystem.suffix_storage_path' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
$tenantStoragePath = storage_path();
|
$tenantStoragePath = storage_path();
|
||||||
|
|
||||||
Storage::fake('test');
|
// FilesystemTenancyBootstrapper enabled,
|
||||||
|
// suffix_storage_path enabled, so the two paths are distinct.
|
||||||
|
// Tenant storage will be deleted.
|
||||||
|
expect($tenantStoragePath)->not()->toBe($centralStoragePath);
|
||||||
expect(File::isDirectory($tenantStoragePath))->toBeTrue();
|
expect(File::isDirectory($tenantStoragePath))->toBeTrue();
|
||||||
|
|
||||||
Storage::put('test.txt', 'testing file');
|
|
||||||
|
|
||||||
tenant()->delete();
|
tenant()->delete();
|
||||||
|
|
||||||
expect(File::isDirectory($tenantStoragePath))->toBeFalse();
|
expect(File::isDirectory($tenantStoragePath))->toBeFalse();
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('the framework/cache directory is created when storage_path is scoped', function (bool $suffixStoragePath) {
|
test('the framework/cache directory is created when storage_path is scoped', function (bool $suffixStoragePath) {
|
||||||
|
|
@ -256,4 +298,3 @@ test('scoped disks are scoped per tenant', function () {
|
||||||
expect(file_get_contents(storage_path() . "/app/public/scoped_disk_prefix/foo.txt"))->toBe('central2');
|
expect(file_get_contents(storage_path() . "/app/public/scoped_disk_prefix/foo.txt"))->toBe('central2');
|
||||||
expect(file_get_contents(storage_path() . "/tenant{$tenant->id}/app/public/scoped_disk_prefix/foo.txt"))->toBe('tenant');
|
expect(file_get_contents(storage_path() . "/tenant{$tenant->id}/app/public/scoped_disk_prefix/foo.txt"))->toBe('tenant');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,27 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Stancl\JobPipeline\JobPipeline;
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
use Stancl\Tenancy\Events\TenantCreated;
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
use Stancl\Tenancy\Events\TenantDeleted;
|
||||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
|
use Stancl\Tenancy\Jobs\DeleteDatabase;
|
||||||
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
||||||
use Stancl\Tenancy\Jobs\SeedDatabase;
|
use Stancl\Tenancy\Jobs\SeedDatabase;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticable;
|
use Illuminate\Foundation\Auth\User as Authenticable;
|
||||||
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
||||||
|
|
||||||
|
beforeEach($cleanup = function () {
|
||||||
|
DeleteDatabase::$ignoreFailures = false;
|
||||||
|
DeleteDatabase::$skipWhenCreateDatabaseIsFalse = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach($cleanup);
|
||||||
|
|
||||||
test('database can be created after tenant creation', function () {
|
test('database can be created after tenant creation', function () {
|
||||||
config(['tenancy.database.template_tenant_connection' => 'mysql']);
|
config(['tenancy.database.template_tenant_connection' => 'mysql']);
|
||||||
|
|
||||||
|
|
@ -82,6 +92,73 @@ test('custom job can be added to the pipeline', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('database can be deleted after tenant deletion', function () {
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
Event::listen(TenantDeleted::class, JobPipeline::make([DeleteDatabase::class])->send(function (TenantDeleted $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
$manager = $tenant->database()->manager();
|
||||||
|
|
||||||
|
expect($manager->databaseExists($tenant->database()->getName()))->toBeTrue();
|
||||||
|
|
||||||
|
$tenant->delete();
|
||||||
|
|
||||||
|
expect($manager->databaseExists($tenant->database()->getName()))->toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('database deletion is skipped when create_database is false', function (bool $skipWhenCreateDatabaseIsFalse) {
|
||||||
|
Event::listen(TenantDeleted::class, JobPipeline::make([DeleteDatabase::class])->send(function (TenantDeleted $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
// create_database=false means no DB is created (e.g. tenant uses a pre-existing DB)
|
||||||
|
// On deletion, DeleteDatabase should skip rather than attempting DROP DATABASE on a non-existent DB
|
||||||
|
$tenant = Tenant::create(['tenancy_create_database' => false, 'tenancy_db_name' => 'non_existing_db']);
|
||||||
|
|
||||||
|
$manager = $tenant->database()->manager();
|
||||||
|
expect($manager->databaseExists($tenant->database()->getName()))->toBeFalse();
|
||||||
|
|
||||||
|
DeleteDatabase::$skipWhenCreateDatabaseIsFalse = $skipWhenCreateDatabaseIsFalse;
|
||||||
|
|
||||||
|
if ($skipWhenCreateDatabaseIsFalse) {
|
||||||
|
$tenant->delete(); // no exception
|
||||||
|
} else {
|
||||||
|
expect(fn () => $tenant->delete())->toThrow(QueryException::class, "database doesn't exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
expect($manager->databaseExists($tenant->database()->getName()))->toBeFalse();
|
||||||
|
})->with([true, false]);
|
||||||
|
|
||||||
|
test('database deletion failure is ignored when ignoreFailures is true', function (bool $ignoreFailures) {
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
Event::listen(TenantDeleted::class, JobPipeline::make([DeleteDatabase::class])->send(function (TenantDeleted $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
DeleteDatabase::$ignoreFailures = $ignoreFailures;
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
$manager = $tenant->database()->manager();
|
||||||
|
expect($manager->databaseExists($tenant->database()->getName()))->toBeTrue();
|
||||||
|
|
||||||
|
$manager->deleteDatabase($tenant); // manually delete so the job fails
|
||||||
|
expect($manager->databaseExists($tenant->database()->getName()))->toBeFalse();
|
||||||
|
|
||||||
|
if ($ignoreFailures) {
|
||||||
|
$tenant->delete(); // no exception
|
||||||
|
} else {
|
||||||
|
expect(fn () => $tenant->delete())->toThrow(QueryException::class, "database doesn't exist");
|
||||||
|
}
|
||||||
|
})->with([true, false]);
|
||||||
|
|
||||||
class User extends Authenticable
|
class User extends Authenticable
|
||||||
{
|
{
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,25 @@ use Stancl\Tenancy\Events\PendingTenantPulled;
|
||||||
use Stancl\Tenancy\Events\PullingPendingTenant;
|
use Stancl\Tenancy\Events\PullingPendingTenant;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
|
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
||||||
|
use Stancl\Tenancy\Jobs\SeedDatabase;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\User;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
|
|
||||||
beforeEach($cleanup = function () {
|
beforeEach($cleanup = function () {
|
||||||
Tenant::$extraCustomColumns = [];
|
Tenant::$extraCustomColumns = [];
|
||||||
Tenant::$getPendingAttributesUsing = null;
|
Tenant::$getPendingAttributesUsing = null;
|
||||||
|
|
||||||
|
MigrateDatabase::$includePending = true;
|
||||||
|
SeedDatabase::$includePending = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach($cleanup);
|
afterEach($cleanup);
|
||||||
|
|
@ -111,6 +126,18 @@ test('a new tenant gets created while pulling a pending tenant if the pending po
|
||||||
expect(Tenant::withPending()->get()->count())->toBe(1); // All tenants
|
expect(Tenant::withPending()->get()->count())->toBe(1); // All tenants
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('withoutPending chained with where clauses returns correct results', function () {
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
$pendingTenant = Tenant::createPending();
|
||||||
|
|
||||||
|
// The query returned the correct tenant
|
||||||
|
expect(Tenant::withoutPending()->where('id', $tenant->id)->first()->id)->toBe($tenant->id);
|
||||||
|
// No tenant with this ID exists, the query returns null
|
||||||
|
expect(Tenant::withoutPending()->where('id', Str::random(8) . 'nonexistent-id')->first())->toBeNull();
|
||||||
|
// withoutPending() correctly excludes the pending tenant from the query
|
||||||
|
expect(Tenant::withoutPending()->where('id', $pendingTenant->id)->first())->toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
test('pending tenants are included in all queries based on the include_in_queries config', function () {
|
test('pending tenants are included in all queries based on the include_in_queries config', function () {
|
||||||
Tenant::createPending();
|
Tenant::createPending();
|
||||||
|
|
||||||
|
|
@ -142,8 +169,8 @@ test('pending events are dispatched', function () {
|
||||||
Event::assertDispatched(PendingTenantPulled::class);
|
Event::assertDispatched(PendingTenantPulled::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('commands do not run for pending tenants if tenancy.pending.include_in_queries is false and the with pending option does not get passed', function() {
|
test('commands include tenants based on the include_in_queries config when --with-pending is not passed', function (bool $includeInQueries) {
|
||||||
config(['tenancy.pending.include_in_queries' => false]);
|
config(['tenancy.pending.include_in_queries' => $includeInQueries]);
|
||||||
|
|
||||||
$tenants = collect([
|
$tenants = collect([
|
||||||
Tenant::create(),
|
Tenant::create(),
|
||||||
|
|
@ -152,21 +179,21 @@ test('commands do not run for pending tenants if tenancy.pending.include_in_quer
|
||||||
Tenant::createPending(),
|
Tenant::createPending(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
pest()->artisan('tenants:migrate --with-pending');
|
$command = pest()->artisan("tenants:run 'bar testing testing@test.test password foo'");
|
||||||
|
|
||||||
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz'");
|
$tenants->each(function ($tenant) use ($command, $includeInQueries) {
|
||||||
|
if ($tenant->pending() && ! $includeInQueries) {
|
||||||
|
$command->doesntExpectOutputToContain("Tenant: {$tenant->getTenantKey()}");
|
||||||
|
} else {
|
||||||
|
$command->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
$pendingTenants = $tenants->filter->pending();
|
$command->assertSuccessful();
|
||||||
$readyTenants = $tenants->reject->pending();
|
})->with([true, false]);
|
||||||
|
|
||||||
$pendingTenants->each(fn ($tenant) => $artisan->doesntExpectOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
test('commands include pending tenants when truthy --with-pending is passed', function (bool $includeInQueries) {
|
||||||
$readyTenants->each(fn ($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
config(['tenancy.pending.include_in_queries' => $includeInQueries]);
|
||||||
|
|
||||||
$artisan->assertExitCode(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('commands run for pending tenants too if tenancy.pending.include_in_queries is true', function() {
|
|
||||||
config(['tenancy.pending.include_in_queries' => true]);
|
|
||||||
|
|
||||||
$tenants = collect([
|
$tenants = collect([
|
||||||
Tenant::create(),
|
Tenant::create(),
|
||||||
|
|
@ -175,17 +202,22 @@ test('commands run for pending tenants too if tenancy.pending.include_in_queries
|
||||||
Tenant::createPending(),
|
Tenant::createPending(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
pest()->artisan('tenants:migrate --with-pending');
|
foreach ([
|
||||||
|
'--with-pending',
|
||||||
|
'--with-pending=true',
|
||||||
|
'--with-pending=1'
|
||||||
|
] as $option) {
|
||||||
|
$command = pest()->artisan("tenants:run 'bar testing testing@test.test password foo' {$option}");
|
||||||
|
|
||||||
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz'");
|
// Pending tenants are included regardless of tenancy.pending.include_in_queries
|
||||||
|
$tenants->each(fn ($tenant) => $command->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
||||||
|
|
||||||
$tenants->each(fn ($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
$command->assertSuccessful();
|
||||||
|
}
|
||||||
|
})->with([true, false]);
|
||||||
|
|
||||||
$artisan->assertExitCode(0);
|
test('commands exclude pending tenants when falsy --with-pending is passed', function (bool $includeInQueries) {
|
||||||
});
|
config(['tenancy.pending.include_in_queries' => $includeInQueries]);
|
||||||
|
|
||||||
test('commands run for pending tenants too if the with pending option is passed', function() {
|
|
||||||
config(['tenancy.pending.include_in_queries' => false]);
|
|
||||||
|
|
||||||
$tenants = collect([
|
$tenants = collect([
|
||||||
Tenant::create(),
|
Tenant::create(),
|
||||||
|
|
@ -194,14 +226,25 @@ test('commands run for pending tenants too if the with pending option is passed'
|
||||||
Tenant::createPending(),
|
Tenant::createPending(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
pest()->artisan('tenants:migrate --with-pending');
|
foreach ([
|
||||||
|
'--with-pending=false',
|
||||||
|
'--with-pending=0',
|
||||||
|
'--with-pending=foo' // Invalid values are treated as false
|
||||||
|
] as $option) {
|
||||||
|
$command = pest()->artisan("tenants:run 'bar testing testing@test.test password foo' {$option}");
|
||||||
|
|
||||||
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz' --with-pending");
|
$tenants->each(function ($tenant) use ($command) {
|
||||||
|
if ($tenant->pending()) {
|
||||||
|
// Pending tenants are excluded regardless of tenancy.pending.include_in_queries
|
||||||
|
$command->doesntExpectOutputToContain("Tenant: {$tenant->getTenantKey()}");
|
||||||
|
} else {
|
||||||
|
$command->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
$tenants->each(fn ($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
$command->assertSuccessful();
|
||||||
|
}
|
||||||
$artisan->assertExitCode(0);
|
})->with([true, false]);
|
||||||
});
|
|
||||||
|
|
||||||
test('pending tenants can have default attributes for non-nullable columns', function (bool $withPendingAttributes) {
|
test('pending tenants can have default attributes for non-nullable columns', function (bool $withPendingAttributes) {
|
||||||
Schema::table('tenants', function (Blueprint $table) {
|
Schema::table('tenants', function (Blueprint $table) {
|
||||||
|
|
@ -224,3 +267,105 @@ test('pending tenants can have default attributes for non-nullable columns', fun
|
||||||
else
|
else
|
||||||
expect($fn)->toThrow(QueryException::class);
|
expect($fn)->toThrow(QueryException::class);
|
||||||
})->with([true, false]);
|
})->with([true, false]);
|
||||||
|
|
||||||
|
test('pending tenant databases can be migrated using a job unless configured otherwise', function (bool $includeInQueries, ?bool $migrateWithPending) {
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class],
|
||||||
|
'tenancy.pending.include_in_queries' => $includeInQueries,
|
||||||
|
]);
|
||||||
|
|
||||||
|
MigrateDatabase::$includePending = $migrateWithPending;
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
MigrateDatabase::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$pendingTenant = Tenant::createPending();
|
||||||
|
|
||||||
|
expect(Schema::hasTable('users'))->toBeFalse();
|
||||||
|
|
||||||
|
tenancy()->initialize($pendingTenant);
|
||||||
|
|
||||||
|
// MigrateDatabase includes/excludes pending tenants based on its $includePending property,
|
||||||
|
// regardless of the tenancy.pending.include_in_queries config.
|
||||||
|
expect(Schema::hasTable('users'))->toBe($migrateWithPending ?? $includeInQueries);
|
||||||
|
})->with([
|
||||||
|
'include pending in queries' => [true],
|
||||||
|
'exclude pending from queries' => [false],
|
||||||
|
])->with([
|
||||||
|
'migrate with pending' => [true],
|
||||||
|
'migrate without pending' => [false],
|
||||||
|
'default to config' => [null],
|
||||||
|
]);
|
||||||
|
|
||||||
|
test('pending tenant databases can be seeded using a job unless configured otherwise', function (bool $includeInQueries, ?bool $seedWithPending) {
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class],
|
||||||
|
'tenancy.pending.include_in_queries' => $includeInQueries,
|
||||||
|
'tenancy.seeder_parameters.--class' => TestSeeder::class,
|
||||||
|
]);
|
||||||
|
|
||||||
|
MigrateDatabase::$includePending = true;
|
||||||
|
SeedDatabase::$includePending = $seedWithPending;
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
MigrateDatabase::class,
|
||||||
|
SeedDatabase::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$pendingTenant = Tenant::createPending();
|
||||||
|
|
||||||
|
tenancy()->initialize($pendingTenant);
|
||||||
|
|
||||||
|
// SeedDatabase includes/excludes pending tenants based on its $includePending property,
|
||||||
|
// regardless of the tenancy.pending.include_in_queries config.
|
||||||
|
expect(User::where('email', 'seeded@user')->exists())->toBe($seedWithPending ?? $includeInQueries);
|
||||||
|
})->with([
|
||||||
|
'include pending in queries' => [true],
|
||||||
|
'exclude pending from queries' => [false],
|
||||||
|
])->with([
|
||||||
|
'seed with pending' => [true],
|
||||||
|
'seed without pending' => [false],
|
||||||
|
'default to config' => [null],
|
||||||
|
]);
|
||||||
|
|
||||||
|
test('jobs that run before tenants get fully created recognize pending tenants', function () {
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class],
|
||||||
|
]);
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
PendingTenantJob::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
Tenant::createPending();
|
||||||
|
|
||||||
|
expect(app('tenant_is_pending'))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
class PendingTenantJob
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public Tenant $tenant,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
app()->instance('tenant_is_pending', $this->tenant->pending());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -89,13 +89,14 @@ test('tenant user can be impersonated on a tenant domain', function () {
|
||||||
->assertSee('You are logged in as Joe');
|
->assertSee('You are logged in as Joe');
|
||||||
|
|
||||||
expect(UserImpersonation::isImpersonating())->toBeTrue();
|
expect(UserImpersonation::isImpersonating())->toBeTrue();
|
||||||
expect(session('tenancy_impersonating'))->toBeTrue();
|
expect(session('tenancy_impersonation_guard'))->toBe('web');
|
||||||
|
expect($token->auth_guard)->toBe('web');
|
||||||
|
|
||||||
// Leave impersonation
|
// Leave impersonation
|
||||||
UserImpersonation::stopImpersonating();
|
UserImpersonation::stopImpersonating();
|
||||||
|
|
||||||
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
||||||
expect(session('tenancy_impersonating'))->toBeNull();
|
expect(session('tenancy_impersonation_guard'))->toBeNull();
|
||||||
|
|
||||||
// Assert can't access the tenant dashboard
|
// Assert can't access the tenant dashboard
|
||||||
pest()->get('http://foo.localhost/dashboard')
|
pest()->get('http://foo.localhost/dashboard')
|
||||||
|
|
@ -135,19 +136,113 @@ test('tenant user can be impersonated on a tenant path', function () {
|
||||||
->assertSee('You are logged in as Joe');
|
->assertSee('You are logged in as Joe');
|
||||||
|
|
||||||
expect(UserImpersonation::isImpersonating())->toBeTrue();
|
expect(UserImpersonation::isImpersonating())->toBeTrue();
|
||||||
expect(session('tenancy_impersonating'))->toBeTrue();
|
expect(session('tenancy_impersonation_guard'))->toBe('web');
|
||||||
|
expect($token->auth_guard)->toBe('web');
|
||||||
|
|
||||||
// Leave impersonation
|
// Leave impersonation
|
||||||
UserImpersonation::stopImpersonating();
|
UserImpersonation::stopImpersonating();
|
||||||
|
|
||||||
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
||||||
expect(session('tenancy_impersonating'))->toBeNull();
|
expect(session('tenancy_impersonation_guard'))->toBeNull();
|
||||||
|
|
||||||
// Assert can't access the tenant dashboard
|
// Assert can't access the tenant dashboard
|
||||||
pest()->get('/acme/dashboard')
|
pest()->get('/acme/dashboard')
|
||||||
->assertRedirect('/login');
|
->assertRedirect('/login');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('stopImpersonating can keep the user authenticated', function () {
|
||||||
|
makeLoginRoute();
|
||||||
|
|
||||||
|
Route::middleware(InitializeTenancyByPath::class)->prefix('/{tenant}')->group(getRoutes(false));
|
||||||
|
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
'tenancy_db_name' => 'db' . Str::random(16),
|
||||||
|
]);
|
||||||
|
|
||||||
|
migrateTenants();
|
||||||
|
|
||||||
|
$user = $tenant->run(function () {
|
||||||
|
return ImpersonationUser::create([
|
||||||
|
'name' => 'Joe',
|
||||||
|
'email' => 'joe@local',
|
||||||
|
'password' => bcrypt('secret'),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Impersonate the user
|
||||||
|
$token = tenancy()->impersonate($tenant, $user->id, '/acme/dashboard');
|
||||||
|
|
||||||
|
pest()->get('/acme/impersonate/' . $token->token)
|
||||||
|
->assertRedirect('/acme/dashboard');
|
||||||
|
|
||||||
|
expect(UserImpersonation::isImpersonating())->toBeTrue();
|
||||||
|
|
||||||
|
// Stop impersonating without logging out
|
||||||
|
UserImpersonation::stopImpersonating(false);
|
||||||
|
|
||||||
|
// The impersonation session key should be cleared
|
||||||
|
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
||||||
|
expect(session('tenancy_impersonation_guard'))->toBeNull();
|
||||||
|
|
||||||
|
// The user should still be authenticated
|
||||||
|
pest()->get('/acme/dashboard')
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertSee('You are logged in as Joe');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stopImpersonating logs out the user from the impersonation guard stored in session', function () {
|
||||||
|
Route::middleware(InitializeTenancyByPath::class)->prefix('/{tenant}')->group(getRoutes(false));
|
||||||
|
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
'tenancy_db_name' => 'db' . Str::random(16),
|
||||||
|
]);
|
||||||
|
|
||||||
|
migrateTenants();
|
||||||
|
|
||||||
|
$user = $tenant->run(function () {
|
||||||
|
return ImpersonationUser::create([
|
||||||
|
'name' => 'Joe',
|
||||||
|
'email' => 'joe@local',
|
||||||
|
'password' => bcrypt('secret'),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Impersonate the user
|
||||||
|
$token = tenancy()->impersonate($tenant, $user->id, '/acme/dashboard');
|
||||||
|
|
||||||
|
pest()->get('/acme/impersonate/' . $token->token)
|
||||||
|
->assertRedirect('/acme/dashboard');
|
||||||
|
|
||||||
|
expect(session('tenancy_impersonation_guard'))->toBe('web');
|
||||||
|
|
||||||
|
// Impersonation logged in the user using the current guard ('web')
|
||||||
|
expect(auth('web')->check())->toBeTrue();
|
||||||
|
|
||||||
|
config(['auth.guards.test' => [
|
||||||
|
'driver' => 'session',
|
||||||
|
'provider' => 'users',
|
||||||
|
]]);
|
||||||
|
|
||||||
|
// Manually log the user in through the 'test' guard
|
||||||
|
auth('test')->loginUsingId($user->id);
|
||||||
|
|
||||||
|
// Should log the user out from the guard used for impersonation ('web')
|
||||||
|
UserImpersonation::stopImpersonating();
|
||||||
|
|
||||||
|
expect(auth('web')->check())->toBeFalse();
|
||||||
|
expect(auth('test')->check())->toBeTrue();
|
||||||
|
|
||||||
|
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
||||||
|
|
||||||
|
// tenancy_impersonation_guard isn't in the session anymore,
|
||||||
|
// stopImpersonating should throw an exception instead of logging out
|
||||||
|
expect(fn() => UserImpersonation::stopImpersonating())->toThrow(Exception::class);
|
||||||
|
|
||||||
|
expect(auth('test')->check())->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
test('tokens have a limited ttl', function () {
|
test('tokens have a limited ttl', function () {
|
||||||
Route::middleware(InitializeTenancyByDomain::class)->group(getRoutes());
|
Route::middleware(InitializeTenancyByDomain::class)->group(getRoutes());
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue