mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 17:14:03 +00:00
Throw an exception on attempt to create impersonation token with a non-stateful guard
This commit is contained in:
parent
eade69c3f4
commit
8c73e5759f
2 changed files with 26 additions and 2 deletions
|
|
@ -5,9 +5,12 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Contracts\Auth\StatefulGuard;
|
||||
use Stancl\Tenancy\Database\Concerns\CentralConnection;
|
||||
use Stancl\Tenancy\Exceptions\ImpersonationTokenCouldNotBeCreatedWithNonStatefulGuard;
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
|
|
@ -38,9 +41,15 @@ class ImpersonationToken extends Model
|
|||
public static function booted()
|
||||
{
|
||||
static::creating(function ($model) {
|
||||
$authGuard = $model->auth_guard ?? config('auth.defaults.guard');
|
||||
|
||||
if (! Auth::guard($authGuard) instanceof StatefulGuard) {
|
||||
throw new ImpersonationTokenCouldNotBeCreatedWithNonStatefulGuard($authGuard);
|
||||
}
|
||||
|
||||
$model->created_at = $model->created_at ?? $model->freshTimestamp();
|
||||
$model->token = $model->token ?? Str::random(128);
|
||||
$model->auth_guard = $model->auth_guard ?? config('auth.defaults.guard');
|
||||
$model->auth_guard = $authGuard;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
<?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.");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue