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

Make the exception only about requiring a stateful guard

This commit is contained in:
lukinovec 2022-09-02 09:56:43 +02:00
parent 7802c280ed
commit f1e93a0ca0
4 changed files with 19 additions and 19 deletions

View file

@ -10,7 +10,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Stancl\Tenancy\Database\Concerns\CentralConnection;
use Stancl\Tenancy\Exceptions\ImpersonationTokenCouldNotBeCreatedWithNonStatefulGuard;
use Stancl\Tenancy\Exceptions\StatefulGuardRequiredException;
/**
* @param string $token
@ -44,7 +44,7 @@ class ImpersonationToken extends Model
$authGuard = $model->auth_guard ?? config('auth.defaults.guard');
if (! Auth::guard($authGuard) instanceof StatefulGuard) {
throw new ImpersonationTokenCouldNotBeCreatedWithNonStatefulGuard($authGuard);
throw new StatefulGuardRequiredException($authGuard);
}
$model->created_at = $model->created_at ?? $model->freshTimestamp();

View file

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Exceptions;
use Exception;
class ImpersonationTokenCouldNotBeCreatedWithNonStatefulGuard extends Exception
{
public function __construct(string $guardName)
{
parent::__construct("Cannot use a non-stateful auth guard ('$guardName') with user impersonation. Use a guard implementing the Illuminate\\Contracts\\Auth\\StatefulGuard interface.");
}
}

View file

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Exceptions;
use Exception;
class StatefulGuardRequiredException extends Exception
{
public function __construct(string $guardName)
{
parent::__construct("Cannot use a non-stateful guard ('$guardName'). A guard implementing the Illuminate\\Contracts\\Auth\\StatefulGuard interface is required.");
}
}

View file

@ -24,7 +24,7 @@ use Stancl\Tenancy\Database\Models\ImpersonationToken;
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Exceptions\ImpersonationTokenCouldNotBeCreatedWithNonStatefulGuard;
use Stancl\Tenancy\Exceptions\StatefulGuardRequiredException;
beforeEach(function () {
pest()->artisan('migrate', [
@ -257,7 +257,7 @@ test('impersonation tokens can be created only with stateful guards', function (
Auth::extend('nonstateful', fn($app, $name, array $config) => new TokenGuard(Auth::createUserProvider($config['provider']), request()));
expect(fn() => tenancy()->impersonate($tenant, $user->id, '/dashboard', 'nonstateful'))
->toThrow(ImpersonationTokenCouldNotBeCreatedWithNonStatefulGuard::class);
->toThrow(StatefulGuardRequiredException::class);
Auth::extend('stateful', fn ($app, $name, array $config) => new SessionGuard($name, Auth::createUserProvider($config['provider']), session()));