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

[4.x] Make the ImpersonationToken model configurable (#1335)

* Make the ImpersonationToken model configurable, resolve #1315

* Add type definition

* Make phpstan happy
This commit is contained in:
Samuel Štancl 2025-03-18 18:42:08 +01:00 committed by GitHub
parent 37a0f1a713
commit 95dd906de2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 6 deletions

View file

@ -15,6 +15,7 @@ return [
'models' => [ 'models' => [
'tenant' => Stancl\Tenancy\Database\Models\Tenant::class, 'tenant' => Stancl\Tenancy\Database\Models\Tenant::class,
'domain' => Stancl\Tenancy\Database\Models\Domain::class, 'domain' => Stancl\Tenancy\Database\Models\Domain::class,
'impersonation_token' => Stancl\Tenancy\Database\Models\ImpersonationToken::class,
/** /**
* Name of the column used to relate models to tenants. * Name of the column used to relate models to tenants.

View file

@ -25,6 +25,9 @@ class ImpersonationToken extends Model
{ {
use CentralConnection; use CentralConnection;
/** You can set this property to customize the table name */
public static string $tableName = 'tenant_user_impersonation_tokens';
protected $guarded = []; protected $guarded = [];
public $timestamps = false; public $timestamps = false;
@ -33,11 +36,15 @@ class ImpersonationToken extends Model
public $incrementing = false; public $incrementing = false;
protected $table = 'tenant_user_impersonation_tokens';
protected $casts = [ protected $casts = [
'created_at' => 'datetime', 'created_at' => 'datetime',
]; ];
public function getTable()
{
return static::$tableName;
}
public static function booted(): void public static function booted(): void
{ {
static::creating(function ($model) { static::creating(function ($model) {

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Features; namespace Stancl\Tenancy\Features;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Stancl\Tenancy\Contracts\Feature; use Stancl\Tenancy\Contracts\Feature;
@ -18,8 +19,8 @@ class UserImpersonation implements Feature
public function bootstrap(Tenancy $tenancy): void public function bootstrap(Tenancy $tenancy): void
{ {
$tenancy->macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string|null $authGuard = null, bool $remember = false): ImpersonationToken { $tenancy->macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string|null $authGuard = null, bool $remember = false): Model {
return ImpersonationToken::create([ return UserImpersonation::modelClass()::create([
Tenancy::tenantKeyColumn() => $tenant->getTenantKey(), Tenancy::tenantKeyColumn() => $tenant->getTenantKey(),
'user_id' => $userId, 'user_id' => $userId,
'redirect_url' => $redirectUrl, 'redirect_url' => $redirectUrl,
@ -30,10 +31,15 @@ class UserImpersonation implements Feature
} }
/** Impersonate a user and get an HTTP redirect response. */ /** Impersonate a user and get an HTTP redirect response. */
public static function makeResponse(#[\SensitiveParameter] string|ImpersonationToken $token, ?int $ttl = null): RedirectResponse public static function makeResponse(#[\SensitiveParameter] string|Model $token, ?int $ttl = null): RedirectResponse
{ {
/** @var ImpersonationToken $token */ /**
$token = $token instanceof ImpersonationToken ? $token : ImpersonationToken::findOrFail($token); * 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; $ttl ??= static::$ttl;
$tokenExpired = $token->created_at->diffInSeconds(now()) > $ttl; $tokenExpired = $token->created_at->diffInSeconds(now()) > $ttl;
@ -54,6 +60,12 @@ class UserImpersonation implements Feature
return redirect($token->redirect_url); 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 public static function isImpersonating(): bool
{ {
return session()->has('tenancy_impersonating'); return session()->has('tenancy_impersonating');