mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 09:34:04 +00:00
* cleanup, resolve todos, add immediate todos * Improve path_identification_middleware docblock * rename leave() method in tests * wip fix hardcoded values making assumptions about the parameters used in routing * defaultParameterNames * fix CreatesDatabaseUsers return values * $tenant -> tenant() * resolve more todos * make comment block a complete block * Correct useTenantRoutesInFortify(), delete unused import * test fixes * remove todos * remove JobPipeline todo * simplify comment example * remove todo * fix VERSION_PREFIX in queue.yml --------- Co-authored-by: lukinovec <lukinovec@gmail.com>
71 lines
2.2 KiB
PHP
71 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Features;
|
|
|
|
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(Tenancy $tenancy): void
|
|
{
|
|
$tenancy->macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string|null $authGuard = null, bool $remember = false): ImpersonationToken {
|
|
return ImpersonationToken::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|ImpersonationToken $token, ?int $ttl = null): RedirectResponse
|
|
{
|
|
/** @var ImpersonationToken $token */
|
|
$token = $token instanceof ImpersonationToken ? $token : ImpersonationToken::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);
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|