mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 20:54:02 +00:00
Make stopImpersonating throw an exception if no user is impersonated currently
This commit is contained in:
parent
1289550533
commit
d17c7b3e86
2 changed files with 13 additions and 0 deletions
|
|
@ -11,6 +11,7 @@ use Stancl\Tenancy\Contracts\Feature;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
use Stancl\Tenancy\Database\Models\ImpersonationToken;
|
use Stancl\Tenancy\Database\Models\ImpersonationToken;
|
||||||
use Stancl\Tenancy\Tenancy;
|
use Stancl\Tenancy\Tenancy;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class UserImpersonation implements Feature
|
class UserImpersonation implements Feature
|
||||||
{
|
{
|
||||||
|
|
@ -84,6 +85,10 @@ class UserImpersonation implements Feature
|
||||||
*/
|
*/
|
||||||
public static function stopImpersonating(bool $logout = true): void
|
public static function stopImpersonating(bool $logout = true): void
|
||||||
{
|
{
|
||||||
|
if (! static::isImpersonating()) {
|
||||||
|
throw new Exception('Not currently impersonating any user.');
|
||||||
|
}
|
||||||
|
|
||||||
if ($logout) {
|
if ($logout) {
|
||||||
$guard = session()->get('tenancy_impersonation_guard');
|
$guard = session()->get('tenancy_impersonation_guard');
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -234,6 +234,14 @@ test('stopImpersonating logs out the user from tenancy_impersonation_guard store
|
||||||
|
|
||||||
expect(auth('web')->check())->toBeFalse();
|
expect(auth('web')->check())->toBeFalse();
|
||||||
expect(auth('test')->check())->toBeTrue();
|
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 () {
|
test('tokens have a limited ttl', function () {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue