mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 10:34:04 +00:00
* Throw an exception on attempt to create impersonation token with a non-stateful guard * Test that impersonation tokens can only be created with a stateful guard * Fix code style (php-cs-fixer) * Escape backslashes in the exception's message Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Make the exception only about requiring a stateful guard Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Database\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Contracts\Auth\StatefulGuard;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Str;
|
|
use Stancl\Tenancy\Database\Concerns\CentralConnection;
|
|
use Stancl\Tenancy\Exceptions\StatefulGuardRequiredException;
|
|
|
|
/**
|
|
* @property string $token
|
|
* @property string $tenant_id
|
|
* @property string $user_id
|
|
* @property string $auth_guard
|
|
* @property string $redirect_url
|
|
* @property Carbon $created_at
|
|
*/
|
|
class ImpersonationToken extends Model
|
|
{
|
|
use CentralConnection;
|
|
|
|
protected $guarded = [];
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $primaryKey = 'token';
|
|
|
|
public $incrementing = false;
|
|
|
|
protected $table = 'tenant_user_impersonation_tokens';
|
|
|
|
protected $dates = [
|
|
'created_at',
|
|
];
|
|
|
|
public static function booted(): void
|
|
{
|
|
static::creating(function ($model) {
|
|
$authGuard = $model->auth_guard ?? config('auth.defaults.guard');
|
|
|
|
if (! Auth::guard($authGuard) instanceof StatefulGuard) {
|
|
throw new StatefulGuardRequiredException($authGuard);
|
|
}
|
|
|
|
$model->created_at = $model->created_at ?? $model->freshTimestamp();
|
|
$model->token = $model->token ?? Str::random(128);
|
|
$model->auth_guard = $authGuard;
|
|
});
|
|
}
|
|
}
|