Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| User | |
0.00% |
0 / 23 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| booted | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
20 | |||
| updatePassword | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| activityLogs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use App\Enums\Users\RoleEnum; |
| 6 | use App\Traits\Models\ActivityLog; |
| 7 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 8 | use Illuminate\Foundation\Auth\User as Authenticatable; |
| 9 | use Illuminate\Notifications\Notifiable; |
| 10 | use Illuminate\Support\Facades\Hash; |
| 11 | |
| 12 | /** |
| 13 | * @property string $first_name Prénom. |
| 14 | * @property string $last_name Nom. |
| 15 | * @property string $email Email. |
| 16 | * @property string $password Mot de passe. |
| 17 | * @property \App\Enums\Users\RoleEnum $role Rôle. |
| 18 | * @property \Illuminate\Support\Carbon $published État de publication. |
| 19 | * @property \Illuminate\Support\Carbon $published_at Date de mise à jour de l'état de publication. |
| 20 | * @property-read \Illuminate\Support\Carbon $created_at Created date. |
| 21 | * @property-read \Illuminate\Support\Carbon $updated_at Updated date. |
| 22 | * |
| 23 | * @method protected static function booted() Perform any actions required after the model boots. |
| 24 | * @method private static function setOrder($model) Set order after the last element of the list. |
| 25 | * @method private static function updatePassword($model) Update password. |
| 26 | * |
| 27 | * @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship. |
| 28 | */ |
| 29 | class User extends Authenticatable |
| 30 | { |
| 31 | use ActivityLog; |
| 32 | use HasFactory; |
| 33 | use Notifiable; |
| 34 | |
| 35 | /** |
| 36 | * The attributes that are mass assignable. |
| 37 | * |
| 38 | * @var array<int, string> |
| 39 | */ |
| 40 | protected $fillable = [ |
| 41 | 'first_name', |
| 42 | 'last_name', |
| 43 | 'email', |
| 44 | 'password', |
| 45 | 'role', |
| 46 | 'published', |
| 47 | 'published_at', |
| 48 | ]; |
| 49 | |
| 50 | /** |
| 51 | * The attributes that should be hidden for serialization. |
| 52 | * |
| 53 | * @var array<int, string> |
| 54 | */ |
| 55 | protected $hidden = [ |
| 56 | 'password', |
| 57 | 'remember_token', |
| 58 | ]; |
| 59 | |
| 60 | /** |
| 61 | * The attributes that should be cast. |
| 62 | * |
| 63 | * @var array<string, string> |
| 64 | */ |
| 65 | protected $casts = [ |
| 66 | 'role' => RoleEnum::class, |
| 67 | 'email_verified_at' => 'datetime', |
| 68 | 'published' => 'boolean', |
| 69 | ]; |
| 70 | |
| 71 | /** |
| 72 | * Perform any actions required after the model boots. |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | protected static function booted(): void |
| 77 | { |
| 78 | static::creating(function (self $model) { |
| 79 | static::updatePassword($model); |
| 80 | // * Prevent user elevation privileges |
| 81 | throw_if( |
| 82 | auth('backend')->user() and auth('backend')->user()->role->value() > $model->role->value(), |
| 83 | AuthorizationException::class |
| 84 | ); |
| 85 | }); |
| 86 | static::updating(function (self $model) { |
| 87 | static::updatePassword($model); |
| 88 | if (optional(auth('backend')->user())->getKey() === $model->getKey()) { |
| 89 | \validator( |
| 90 | ['published' => $model->published], |
| 91 | ['published' => 'required|boolean|accepted'], |
| 92 | ['published.accepted' => trans('Vous ne pouvez pas déactiver votre propre compte')], |
| 93 | )->validate(); |
| 94 | } |
| 95 | // * Prevent user elevation privileges |
| 96 | throw_if( |
| 97 | auth('backend')->user() and auth('backend')->user()->role->value() > $model->role->value(), |
| 98 | AuthorizationException::class |
| 99 | ); |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | // * METHODS |
| 104 | |
| 105 | /** |
| 106 | * Update password. |
| 107 | * |
| 108 | * @param self $model |
| 109 | * @return void |
| 110 | */ |
| 111 | private static function updatePassword(self $model): void |
| 112 | { |
| 113 | if (Hash::needsRehash($model->password)) { |
| 114 | $model->password = Hash::make($model->password); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // * RELATIONS |
| 119 | |
| 120 | /** |
| 121 | * Activities logs One-to-many relationship. |
| 122 | * |
| 123 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 124 | */ |
| 125 | public function activityLogs(): \Illuminate\Database\Eloquent\Relations\HasMany |
| 126 | { |
| 127 | return $this->hasMany(ActivityLog::class); |
| 128 | } |
| 129 | } |