78 lines
2.6 KiB
PHP
78 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\HasUuid;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Fortify\TwoFactorAuthenticatable;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
/**
|
|
* An operator: the owner of CluPilot, or a member of staff.
|
|
*
|
|
* Extends Authenticatable rather than App\Models\User — sharing the class would
|
|
* put both groups back in one table, which is the thing this exists to end.
|
|
*
|
|
* `$guard_name` is what makes Spatie resolve roles against the `operator` guard.
|
|
* Without it every role assignment would be checked against `web` and fail,
|
|
* which is the failure mode to expect if roles suddenly stop matching.
|
|
*/
|
|
class Operator extends Authenticatable
|
|
{
|
|
use HasFactory, HasRoles, HasUuid, Notifiable, TwoFactorAuthenticatable;
|
|
|
|
/** Tells spatie/laravel-permission which guard this model's roles live under. */
|
|
protected string $guard_name = 'operator';
|
|
|
|
/** The six roles that grant access to the console. */
|
|
/**
|
|
* Die Rollen dieser Installation — aus der Datenbank, nicht aus einer
|
|
* Liste im Code.
|
|
*
|
|
* Vorher stand sie hier als Konstante UND zweimal als
|
|
* `in:Owner,Admin,…`-Regel in Admin\Settings. `Developer` war in der
|
|
* Konstanten und in keiner der beiden Regeln: die Rolle existierte, und
|
|
* das Einladen-Formular bot sie nie an. Seit Admin\Roles Rollen anlegen
|
|
* kann, wäre jede feste Liste ohnehin schon beim ersten Anlegen falsch.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public static function operatorRoles(): array
|
|
{
|
|
return Role::query()
|
|
->where('guard_name', 'operator')
|
|
->orderBy('name')
|
|
->pluck('name')
|
|
->all();
|
|
}
|
|
|
|
protected $fillable = ['name', 'email', 'password', 'last_login_at', 'disabled_at'];
|
|
|
|
protected $hidden = ['password', 'remember_token', 'two_factor_secret', 'two_factor_recovery_codes'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'password' => 'hashed',
|
|
'last_login_at' => 'datetime',
|
|
'disabled_at' => 'datetime',
|
|
'two_factor_confirmed_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/** True when this operator holds a role that reaches the console. */
|
|
public function isOperator(): bool
|
|
{
|
|
return $this->hasAnyRole(self::operatorRoles());
|
|
}
|
|
|
|
/** A disabled operator keeps their record and loses their access. */
|
|
public function isActive(): bool
|
|
{
|
|
return $this->disabled_at === null;
|
|
}
|
|
}
|