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

129 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2020-10-21 15:26:21 +02:00
<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
namespace App\Models;
2020-10-21 15:26:21 +02:00
use App\Enums\Users\RoleEnum;
2026-10-02 12:09:46 +02:00
use App\Traits\Models\HasActivityLog;
2023-12-12 12:25:45 +01:00
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2026-10-02 12:09:46 +02:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2020-10-21 15:26:21 +02:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
2026-10-02 12:09:46 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
2020-10-21 15:26:21 +02:00
/**
2026-10-02 12:09:46 +02:00
* @property string $first_name Prénom.
* @property string $last_name Nom.
* @property string $email Email.
* @property string $password Mot de passe.
* @property RoleEnum $role Rôle.
* @property Carbon $published État de publication.
* @property Carbon $published_at Date de mise à jour de l'état de publication.
* @property-read Carbon $created_at Created date.
* @property-read Carbon $updated_at Updated date.
2023-11-23 17:31:28 +01:00
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
2020-10-21 15:26:21 +02:00
class User extends Authenticatable
{
2026-10-02 12:09:46 +02:00
use HasActivityLog;
use HasFactory;
2020-10-21 15:26:21 +02:00
use Notifiable;
/**
* The attributes that are mass assignable.
*
2026-10-02 12:09:46 +02:00
* @var list<string>
2020-10-21 15:26:21 +02:00
*/
2026-10-02 12:09:46 +02:00
#[\Override]
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',
'password',
'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
*
2026-10-02 12:09:46 +02:00
* @var list<string>
2020-10-21 15:26:21 +02:00
*/
2026-10-02 12:09:46 +02:00
#[\Override]
2020-10-21 15:26:21 +02:00
protected $hidden = [
'password',
'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
*/
2026-10-02 12:09:46 +02:00
#[\Override]
2023-11-23 17:31:28 +01:00
protected $casts = [
'role' => RoleEnum::class,
2020-10-21 15:26:21 +02:00
'email_verified_at' => 'datetime',
2023-11-23 17:31:28 +01:00
'published' => 'boolean',
2020-10-21 15:26:21 +02:00
];
2023-12-12 12:25:45 +01:00
// * EVENTS
/**
* Perform any actions required after the model boots.
*/
2023-11-23 17:31:28 +01:00
protected static function booted(): void
{
2026-10-02 12:09:46 +02:00
static::creating(function (self $model): void {
2024-01-31 09:03:18 +00:00
self::updatePassword($model);
2023-11-23 17:31:28 +01:00
// * Prevent user elevation privileges
throw_if(
2026-10-02 12:09:46 +02:00
auth('backend')->user() && auth('backend')->user()->role->value() > $model->role->value(),
2023-11-23 17:31:28 +01:00
AuthorizationException::class
);
});
2026-10-02 12:09:46 +02:00
static::updating(function (self $model): void {
2024-01-31 09:03:18 +00:00
self::updatePassword($model);
2026-10-02 12:09:46 +02:00
if (auth('backend')->user()?->getKey() === $model->getKey()) {
2023-11-23 17:31:28 +01:00
\validator(
['published' => $model->published],
['published' => 'required|boolean|accepted'],
['published.accepted' => trans('Vous ne pouvez pas déactiver votre propre compte')],
)->validate();
}
// * Prevent user elevation privileges
throw_if(
2026-10-02 12:09:46 +02:00
auth('backend')->user() && auth('backend')->user()->role->value() > $model->role->value(),
2023-11-23 17:31:28 +01:00
AuthorizationException::class
);
});
}
// * METHODS
/**
* Update password.
*/
private static function updatePassword(self $model): void
2021-08-18 17:08:02 +02:00
{
if (Hash::needsRehash($model->password)) {
$model->password = Hash::make($model->password);
}
}
2023-11-23 17:31:28 +01:00
// * RELATIONS
/**
* Activities logs One-to-many relationship.
*
2026-10-02 12:09:46 +02:00
* @return HasMany<ActivityLog, $this>
2023-11-23 17:31:28 +01:00
*/
2026-10-02 12:09:46 +02:00
public function activityLogs(): HasMany
2023-11-23 17:31:28 +01:00
{
return $this->hasMany(ActivityLog::class);
}
2020-10-21 15:26:21 +02:00
}