ldc-gen-game-web/app/Models/User.php

128 lines
3.7 KiB
PHP
Raw Normal View History

2020-10-21 15:26:21 +02:00
<?php
namespace App\Models;
2020-10-21 15:26:21 +02:00
use App\Enums\Users\RoleEnum;
2023-11-23 17:31:28 +01:00
use App\Traits\Models\ActivityLog;
2023-12-12 12:25:45 +01:00
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2020-10-21 15:26:21 +02:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
/**
2025-07-18 14:34:40 +02:00
* @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.
*
2025-07-18 14:34:40 +02:00
* @property-read \App\Models\ActivityLog $activityLogs Activities logs HasMany relationship.
*/
2020-10-21 15:26:21 +02:00
class User extends Authenticatable
{
2023-11-23 17:31:28 +01:00
use ActivityLog;
use HasFactory;
2020-10-21 15:26:21 +02:00
use Notifiable;
/**
* The attributes that are mass assignable.
*
2023-11-23 17:31:28 +01:00
* @var array<int, string>
2020-10-21 15:26:21 +02:00
*/
protected $fillable = [
2023-11-23 17:31:28 +01:00
'first_name',
'last_name',
2020-10-29 15:40:23 +01:00
'email',
'role',
2023-11-23 17:31:28 +01:00
'published',
'published_at',
2020-10-21 15:26:21 +02:00
];
/**
* The attributes that should be hidden for serialization.
2020-10-21 15:26:21 +02:00
*
2023-11-23 17:31:28 +01:00
* @var array<int, string>
2020-10-21 15:26:21 +02:00
*/
protected $hidden = [
'remember_token',
2020-10-21 15:26:21 +02:00
];
/**
2023-11-23 17:31:28 +01:00
* The attributes that should be cast.
2020-10-21 15:26:21 +02:00
*
2023-11-23 17:31:28 +01:00
* @var array<string, string>
2020-10-21 15:26:21 +02:00
*/
2023-11-23 17:31:28 +01:00
protected $casts = [
'role' => RoleEnum::class,
2025-07-18 14:34:40 +02:00
'last_connection' => 'date',
2023-11-23 17:31:28 +01:00
'published' => 'boolean',
2025-07-18 14:34:40 +02:00
'published_at' => 'datetime',
2020-10-21 15:26:21 +02:00
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
2023-11-23 17:31:28 +01:00
protected static function booted(): void
{
static::creating(function (self $model) {
2025-07-18 14:34:40 +02:00
self::checkElevationPrivileges($model);
});
static::updating(function (self $model) {
2025-07-18 14:34:40 +02:00
self::updatePublishedStatus($model);
self::checkElevationPrivileges($model);
});
}
// * METHODS
/**
2025-07-18 14:34:40 +02:00
* Check if the authenticable user can update the published status.
*
2025-07-18 14:34:40 +02:00
* @param self $user
* @return void
*/
2025-07-18 14:34:40 +02:00
private static function updatePublishedStatus(self $user): void
2021-08-18 17:08:02 +02:00
{
2025-07-18 14:34:40 +02:00
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();
}
}
2023-11-23 17:31:28 +01:00
/**
2025-07-18 14:34:40 +02:00
* Prevent user elevation privileges.
2023-11-23 17:31:28 +01:00
*
2025-07-18 14:34:40 +02:00
* @param self $user
* @return void
2023-11-23 17:31:28 +01:00
*/
2025-07-18 14:34:40 +02:00
private static function checkElevationPrivileges(self $user): void
2023-11-23 17:31:28 +01:00
{
2025-07-18 14:34:40 +02:00
throw_if(
auth('backend')->user() and auth('backend')->user()->role->value() > $user->role->value(),
AuthorizationException::class
);
2023-11-23 17:31:28 +01:00
}
2025-07-18 14:34:40 +02:00
// * 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);
}
2020-10-21 15:26:21 +02:00
}