mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 20:34:03 +00:00
wip
This commit is contained in:
parent
7d3298c6bb
commit
55482a0726
2 changed files with 54 additions and 0 deletions
|
|
@ -48,6 +48,23 @@ class UserImpersonation implements Feature
|
||||||
|
|
||||||
$token->delete();
|
$token->delete();
|
||||||
|
|
||||||
|
session()->put('tenancy_impersonation', true);
|
||||||
|
|
||||||
return redirect($token->redirect_url);
|
return redirect($token->redirect_url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function isImpersonating(): bool
|
||||||
|
{
|
||||||
|
return session()->has('tenancy_impersonation');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logout and forget session
|
||||||
|
*/
|
||||||
|
public static function stop(): void
|
||||||
|
{
|
||||||
|
auth()->logout();
|
||||||
|
|
||||||
|
session()->forget('tenancy_impersonation');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Carbon\CarbonInterval;
|
use Carbon\CarbonInterval;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Auth\TokenGuard;
|
use Illuminate\Auth\TokenGuard;
|
||||||
use Illuminate\Auth\SessionGuard;
|
use Illuminate\Auth\SessionGuard;
|
||||||
|
|
@ -83,6 +86,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_impersonation'))->toBeTrue();
|
||||||
|
|
||||||
|
// Leave impersonation
|
||||||
|
UserImpersonation::stop();
|
||||||
|
|
||||||
|
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
||||||
|
expect(session('tenancy_impersonation'))->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 +132,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_impersonation'))->toBeTrue();
|
||||||
|
|
||||||
|
// Leave impersonation
|
||||||
|
UserImpersonation::stop();
|
||||||
|
|
||||||
|
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
||||||
|
expect(session('tenancy_impersonation'))->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 () {
|
||||||
|
|
@ -307,3 +336,11 @@ class AnotherImpersonationUser extends Authenticable
|
||||||
|
|
||||||
protected $table = 'users';
|
protected $table = 'users';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Admin extends Authenticable
|
||||||
|
{
|
||||||
|
protected $guarded = [];
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $table = 'users';
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue