ldc-dam/app/Models/User.php

206 lines
5.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
/**
* @property string $firstname Prénom.
* @property string $lastname Nom.
* @property string $email Email.
* @property string $objectguid
* @property string $matricule Unique LDAP.
* @property-read \Illuminate\Support\Carbon $email_verified_at Mail verified at date.
* @property string $password Mot de passe.
* @property string $remember_token
* @property integer $role Rôle in config/role; 1 => Admin.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Marque> $marques
* Marques BelongsToMany relationship.
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Dossier> $dossiers
* Dossiers BelongsToMany relationship.
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Sujet> $sujets
* Sujets BelongsToMany relationship.
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Media> $mediaList
* Medias HasMany relationship.
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Historique> $historiques
* Historiques HasMany relationship.
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Recherche> $recherches
* Recherches HasMany relationship.
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Panier> $paniersOwner
* Paniers HasMany relationship that belongs to the user.
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Panier> $paniersFavoris
* Paniers HasMany relationship.
*/
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
// * METHODS
/**
* Authorize role
*
* @param string|array $roles
* @return boolean
*/
public function authorizeRoles($roles): bool
{
if (is_array($roles) and in_array($this->role, $roles)) {
return true;
}
if (is_string($roles) and $this->role == $roles) {
return true;
}
abort(401, 'Cette action n\'est pas autorisée.');
}
/**
* Check one role
*
* @param array|string $roles
* @return boolean
*/
public function hasRole($roles): bool
{
if (is_array($roles)) {
return in_array($this->role, $roles);
}
return $this->role == $roles;
}
/**
* Get role label
*
* @return string|null
*/
public function getRoleLabel(): ?string
{
return config('roles.label.' . $this->role);
}
// * RELATIONSHIPS
/**
* BelongsToMany marque relation
* The marques that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function marques()
{
return $this->belongsToMany(Marque::class, 'marque_user', 'user_id', 'marque_id');
}
/**
* BelongsToMany dossier relation
* The dossiers that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function dossiers()
{
return $this->belongsToMany(Dossier::class);
}
/**
* BelongsToMany sujet relation
* The sujet that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function sujets()
{
return $this->belongsToMany(Sujet::class, 'sujet_user', 'user_id', 'sujet_id');
}
/**
* HasMany media relation
* Get the media for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function mediaList()
{
return $this->hasMany(Media::class);
}
/**
* HasMany historiques relation
* Get the historiques for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function historiques()
{
return $this->hasMany(Historique::class);
}
/**
* HasMany recherches relation
* Get the recherches for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function recherches()
{
return $this->hasMany(Recherche::class);
}
/**
* HasMany paniers relation
* Get the paniers for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function paniersOwner()
{
return $this->hasMany(Panier::class, 'user_id')->with('mediaList');
}
/**
* BelongsToMany paniers relation
* The paniers that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function paniersFavoris()
{
return $this->belongsToMany(Panier::class);
}
}