ldc-qrcollector-back/app/Models/User.php

76 lines
1.6 KiB
PHP
Raw Normal View History

2023-03-28 14:37:49 +00:00
<?php
namespace App\Models;
2026-02-12 16:09:21 +01:00
use App\Enums\Users\RoleEnum;
use Illuminate\Auth\Access\AuthorizationException;
2023-03-28 14:37:49 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
/**
* The attributes that are mass assignable.
*
2026-02-12 16:09:21 +01:00
* @var list<string>
2023-03-28 14:37:49 +00:00
*/
protected $fillable = [
'firstname',
'lastname',
'email',
2026-02-12 16:09:21 +01:00
'role',
2023-03-28 14:37:49 +00:00
];
/**
* The attributes that should be hidden for serialization.
*
2026-02-12 16:09:21 +01:00
* @var list<string>
2023-03-28 14:37:49 +00:00
*/
protected $hidden = [
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
2026-02-12 16:09:21 +01:00
'role' => RoleEnum::class,
'last_connection' => 'date',
2023-03-28 14:37:49 +00:00
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
2026-02-12 16:09:21 +01:00
static::creating(function (self $model) {
self::checkElevationPrivileges($model);
2023-03-28 14:37:49 +00:00
});
2026-02-12 16:09:21 +01:00
static::updating(function (self $model) {
self::checkElevationPrivileges($model);
2023-03-28 14:37:49 +00:00
});
}
2026-02-12 16:09:21 +01:00
/**
* Prevent user elevation privileges.
*
* @param self $user
* @return void
*/
private static function checkElevationPrivileges(self $user): void
{
throw_if(
auth('backend')->user() and auth('backend')->user()->role->value() > $user->role->value(),
AuthorizationException::class
);
}
2023-03-28 14:37:49 +00:00
}