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:
parent
21dc69e358
commit
f42f08cb87
2 changed files with 43 additions and 0 deletions
|
|
@ -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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 () {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue