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

Add session state when impersonating tenant (#1029)

* wip

* Fix code style (php-cs-fixer)

* Update TenantUserImpersonationTest.php

* renamed method

* update method name in test

* rename session key

* fix test

* Update src/Features/UserImpersonation.php

Co-authored-by: Samuel Štancl <samuel@archte.ch>

* Update UserImpersonation.php

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
Abrar Ahmad 2022-12-17 06:08:03 +05:00 committed by GitHub
parent 21dc69e358
commit f42f08cb87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View file

@ -48,6 +48,23 @@ class UserImpersonation implements Feature
$token->delete(); $token->delete();
session()->put('tenancy_impersonating', true);
return redirect($token->redirect_url); 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 leave(): void // todo possibly rename
{
auth()->logout();
session()->forget('tenancy_impersonating');
}
} }

View file

@ -83,6 +83,19 @@ test('tenant user can be impersonated on a tenant domain', function () {
pest()->get('http://foo.localhost/dashboard') pest()->get('http://foo.localhost/dashboard')
->assertSuccessful() ->assertSuccessful()
->assertSee('You are logged in as Joe'); ->assertSee('You are logged in as Joe');
expect(UserImpersonation::isImpersonating())->toBeTrue();
expect(session('tenancy_impersonating'))->toBeTrue();
// Leave impersonation
UserImpersonation::leave();
expect(UserImpersonation::isImpersonating())->toBeFalse();
expect(session('tenancy_impersonating'))->toBeNull();
// Assert can't access the tenant dashboard
pest()->get('http://foo.localhost/dashboard')
->assertRedirect('http://foo.localhost/login');
}); });
test('tenant user can be impersonated on a tenant path', function () { test('tenant user can be impersonated on a tenant path', function () {
@ -116,6 +129,19 @@ test('tenant user can be impersonated on a tenant path', function () {
pest()->get('/acme/dashboard') pest()->get('/acme/dashboard')
->assertSuccessful() ->assertSuccessful()
->assertSee('You are logged in as Joe'); ->assertSee('You are logged in as Joe');
expect(UserImpersonation::isImpersonating())->toBeTrue();
expect(session('tenancy_impersonating'))->toBeTrue();
// Leave impersonation
UserImpersonation::leave();
expect(UserImpersonation::isImpersonating())->toBeFalse();
expect(session('tenancy_impersonating'))->toBeNull();
// Assert can't access the tenant dashboard
pest()->get('/acme/dashboard')
->assertRedirect('/login');
}); });
test('tokens have a limited ttl', function () { test('tokens have a limited ttl', function () {