ldc-gen-game-web/app/Models/User.php
2025-08-29 15:01:37 +02:00

127 lines
3.7 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Users\RoleEnum;
use App\Traits\Models\ActivityLog;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
/**
* @property string $first_name User firstname.
* @property string $last_name User lastname.
* @property string $email User email.
* @property string $remember_token Remember_token.
* @property \App\Enums\Users\RoleEnum $role User role.
* @property \Illuminate\Support\Carbon $last_connection User last_connection.
* @property boolean $published User published status.
* @property \Illuminate\Support\Carbon $published_at User publication status update date.
* @property-read \Illuminate\Support\Carbon $created_at User created date.
* @property-read \Illuminate\Support\Carbon $updated_at User updated date.
*
* @property-read \App\Models\ActivityLog $activityLogs Activities logs HasMany relationship.
*/
class User extends Authenticatable
{
use ActivityLog;
use HasFactory;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'role',
'published',
'published_at',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'role' => RoleEnum::class,
'last_connection' => 'date',
'published' => 'boolean',
'published_at' => 'datetime',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::checkElevationPrivileges($model);
});
static::updating(function (self $model) {
self::updatePublishedStatus($model);
self::checkElevationPrivileges($model);
});
}
// * METHODS
/**
* Check if the authenticable user can update the published status.
*
* @param self $user
* @return void
*/
private static function updatePublishedStatus(self $user): void
{
if (optional(auth('backend')->user())->getKey() === $user->getKey()) {
\validator(
['published' => $user->published],
['published' => 'required|boolean|accepted'],
['published.accepted' => trans('Vous ne pouvez pas désactiver votre propre compte')],
)->validate();
}
}
/**
* 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
);
}
// * RELATIONS
/**
* The tenants that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tenants(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Tenant::class);
}
}