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

129 lines
3.7 KiB
PHP
Raw Permalink 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;
use Illuminate\Support\Facades\Hash;
2020-10-21 15:26:21 +02:00
/**
2023-11-23 17:31:28 +01:00
* @property string $first_name Prénom.
* @property string $last_name Nom.
* @property string $email Email.
* @property string $password Mot de passe.
* @property \App\Enums\Users\RoleEnum $role Rôle.
* @property \Illuminate\Support\Carbon $published État de publication.
* @property \Illuminate\Support\Carbon $published_at Date de mise à jour de l'état de publication.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\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
{
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',
'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
*
2023-11-23 17:31:28 +01:00
* @var array<int, string>
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
*/
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.
*
* @return void
*/
2023-11-23 17:31:28 +01:00
protected static function booted(): void
{
static::creating(function (self $model) {
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(
auth('backend')->user() and auth('backend')->user()->role->value() > $model->role->value(),
AuthorizationException::class
);
});
static::updating(function (self $model) {
2024-01-31 09:03:18 +00:00
self::updatePassword($model);
2023-11-23 17:31:28 +01:00
if (optional(auth('backend')->user())->getKey() === $model->getKey()) {
\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(
auth('backend')->user() and auth('backend')->user()->role->value() > $model->role->value(),
AuthorizationException::class
);
});
}
// * METHODS
/**
* Update password.
*
* @param self $model
* @return void
*/
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.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function activityLogs(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ActivityLog::class);
}
2020-10-21 15:26:21 +02:00
}