1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-06 17:04:04 +00:00

Make stopImpersonating throw an exception if no user is impersonated currently

This commit is contained in:
lukinovec 2026-04-13 15:19:17 +02:00
parent 1289550533
commit d17c7b3e86
2 changed files with 13 additions and 0 deletions

View file

@ -11,6 +11,7 @@ use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Database\Models\ImpersonationToken;
use Stancl\Tenancy\Tenancy;
use Exception;
class UserImpersonation implements Feature
{
@ -84,6 +85,10 @@ class UserImpersonation implements Feature
*/
public static function stopImpersonating(bool $logout = true): void
{
if (! static::isImpersonating()) {
throw new Exception('Not currently impersonating any user.');
}
if ($logout) {
$guard = session()->get('tenancy_impersonation_guard');

View file

@ -234,6 +234,14 @@ test('stopImpersonating logs out the user from tenancy_impersonation_guard store
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()->check())->toBeTrue();
});
test('tokens have a limited ttl', function () {