mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:44:02 +00:00
Features are now *always* bootstrapped, even if Tenancy is not resolved from the container. Previous implementations include https://github.com/tenancy-for-laravel/v4/pull/19 https://github.com/archtechx/tenancy/pull/1021 Bug originally reported here https://github.com/archtechx/tenancy/issues/949 This implementation is much simpler, we do not distinguish between features that should be "always bootstrapped" and features that should only be bootstrapped after Tenancy is resolved. All features should work without issues if they're bootstrapped when TSP::boot() is called. We also add a Tenancy::bootstrapFeatures() method that can be used to bootstrap any features dynamically added at runtime that weren't bootstrapped in TSP::boot(). The function keeps track of which features were already bootstrapped so it doesn't bootstrap them again. The only potentialy risky thing in this implementation is that we're now resolving Tenancy in TSP::boot() (previously Tenancy was not being resolved) but that shouldn't be causing any issues.
83 lines
2.6 KiB
PHP
83 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Features;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Stancl\Tenancy\Contracts\Feature;
|
|
use Stancl\Tenancy\Contracts\Tenant;
|
|
use Stancl\Tenancy\Database\Models\ImpersonationToken;
|
|
use Stancl\Tenancy\Tenancy;
|
|
|
|
class UserImpersonation implements Feature
|
|
{
|
|
/** The lifespan of impersonation tokens (in seconds). */
|
|
public static int $ttl = 60;
|
|
|
|
public function bootstrap(): void
|
|
{
|
|
Tenancy::macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string|null $authGuard = null, bool $remember = false): Model {
|
|
return UserImpersonation::modelClass()::create([
|
|
Tenancy::tenantKeyColumn() => $tenant->getTenantKey(),
|
|
'user_id' => $userId,
|
|
'redirect_url' => $redirectUrl,
|
|
'auth_guard' => $authGuard,
|
|
'remember' => $remember,
|
|
]);
|
|
});
|
|
}
|
|
|
|
/** Impersonate a user and get an HTTP redirect response. */
|
|
public static function makeResponse(#[\SensitiveParameter] string|Model $token, ?int $ttl = null): RedirectResponse
|
|
{
|
|
/**
|
|
* The model does NOT have to extend ImpersonationToken, but usually it WILL be a child
|
|
* of ImpersonationToken and this makes it clear to phpstan that the model has a redirect_url property.
|
|
*
|
|
* @var ImpersonationToken $token
|
|
*/
|
|
$token = $token instanceof Model ? $token : static::modelClass()::findOrFail($token);
|
|
$ttl ??= static::$ttl;
|
|
|
|
$tokenExpired = $token->created_at->diffInSeconds(now()) > $ttl;
|
|
|
|
abort_if($tokenExpired, 403);
|
|
|
|
$tokenTenantId = (string) $token->getAttribute(Tenancy::tenantKeyColumn());
|
|
$currentTenantId = (string) tenant()->getTenantKey();
|
|
|
|
abort_unless($tokenTenantId === $currentTenantId, 403);
|
|
|
|
Auth::guard($token->auth_guard)->loginUsingId($token->user_id, $token->remember);
|
|
|
|
$token->delete();
|
|
|
|
session()->put('tenancy_impersonating', true);
|
|
|
|
return redirect($token->redirect_url);
|
|
}
|
|
|
|
/** @return class-string<Model> */
|
|
public static function modelClass(): string
|
|
{
|
|
return config('tenancy.models.impersonation_token');
|
|
}
|
|
|
|
public static function isImpersonating(): bool
|
|
{
|
|
return session()->has('tenancy_impersonating');
|
|
}
|
|
|
|
/**
|
|
* Logout from the current domain and forget impersonation session.
|
|
*/
|
|
public static function stopImpersonating(): void
|
|
{
|
|
auth()->logout();
|
|
|
|
session()->forget('tenancy_impersonating');
|
|
}
|
|
}
|