169 lines
3.8 KiB
PHP
169 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use App\Models\Dossier;
|
|
use App\Models\Historique;
|
|
use App\Models\Marque;
|
|
use App\Models\Media;
|
|
use App\Models\Panier;
|
|
use App\Models\Recherche;
|
|
use App\Models\Sujet;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use Notifiable;
|
|
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'password', 'remember_token',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*
|
|
* @var array
|
|
*/
|
|
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, 'marques_users', '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, 'dossiers_users', 'user_id', 'dossier_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsToMany sujet relation
|
|
* The sujet that belong to the user.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function sujets()
|
|
{
|
|
return $this->belongsToMany(Sujet::class, 'sujets_users', 'user_id', 'sujet_id');
|
|
}
|
|
|
|
/**
|
|
* HasMany medias relation
|
|
* Get the medias for the user.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function medias()
|
|
{
|
|
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('medias');
|
|
}
|
|
|
|
/**
|
|
* BelongsToMany paniers relation
|
|
* The paniers that belong to the user.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function paniersFavoris()
|
|
{
|
|
return $this->belongsToMany(Panier::class, 'paniers_users', 'user_id', 'panier_id');
|
|
}
|
|
}
|