$messages * @property-read \Illuminate\Notifications\DatabaseNotificationCollection $notifications * @property-read Collection $sites * @property-read Collection $subscriptions * @property-read Collection $tickets * @method static \Database\Factories\UserFactory factory($count = null, $state = []) * @method static \Illuminate\Database\Eloquent\Builder|User onlyTrashed() * @method static \Illuminate\Database\Eloquent\Builder|User withTrashed(bool $withTrashed = true) * @method static \Illuminate\Database\Eloquent\Builder|User withoutTrashed() * @mixin \Eloquent */ class User extends Authenticatable { use HasFactory; use Notifiable; use SoftDeletes; /** * stores unhashed pwd * * @var string|null */ public $tmpPass = null; /** * The attributes that are fillable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'role', 'sites', 'gitlab_id', 'favourite_site_id' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', 'role' => RoleEnum::class ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (User $user) { if (\is_null($user->password)) { $user->password = Str::random(10); $user->tmpPass = $user->password; } if (Hash::needsRehash($user->password)) { $user->password = Hash::make($user->password); } Cache::flush('AdminUsers'); }); static::created(function (User $user) { $user->sendMailConfirm(); }); static::updating(function (User $user) { if (Hash::needsRehash($user->password)) { $user->password = Hash::make($user->password); } Cache::flush('AdminUsers'); }); } // * METHODS /** * get User brands * * @return \Illuminate\Database\Eloquent\Collection<\App\Models\Brand> */ public function brands() { return $this->sites->pluck('brand')->filter(); } /** * Send User creation confirm mail * * @return void */ public function sendMailConfirm() { dispatch(new SendUserCreatedEmail($this)); } /** * Send User reset password mail * * @param mixed $token * @return void */ public function sendPasswordResetNotification(mixed $token) { /** @var string $token */ $this->notify(new ResetPassword($token)); } /** * Is the user an admin or a developper ? * * @return boolean */ public function isAdmin(): bool { return ($this->role->equals(RoleEnum::developper) or $this->role->equals(RoleEnum::admin)); } /** * Get All Admin users * * @return Collection */ public static function adminUsers(): Collection { return Cache::remember('AdminUsers', 86400, function () { return self::whereIn('role', [RoleEnum::admin, RoleEnum::developper])->orderBy('name')->get(); }); } // * RELATIONSHIPS /** * HasMany Ticket relation * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function tickets() { return $this->hasMany(Ticket::class, 'created_by'); } /** * HasMany Message relation * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function messages() { return $this->hasMany(Message::class, 'user_id'); } /** * BelongsToMany Ticket relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function subscriptions() { return $this->belongsToMany(Ticket::class, 'subscriptions'); } /** * BelongsToMany Site relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function sites() { return $this->belongsToMany(Site::class); } }