2020-11-18 10:23:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2021-06-18 15:39:19 +02:00
|
|
|
use App\Enums\Role;
|
2021-06-29 09:12:35 +02:00
|
|
|
use App\Jobs\SendUserCreatedEmail;
|
2021-08-01 18:19:55 +02:00
|
|
|
use App\Notifications\ResetPassword;
|
2022-05-24 17:23:36 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-11-18 10:23:51 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-06-28 17:04:15 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2020-11-18 10:23:51 +01:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
2022-05-24 17:23:36 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2021-06-28 17:04:15 +02:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
use Illuminate\Support\Str;
|
2021-07-24 14:20:56 +02:00
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudType;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
|
2021-06-18 15:39:19 +02:00
|
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
2020-11-18 10:23:51 +01:00
|
|
|
|
|
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
2021-06-18 15:39:19 +02:00
|
|
|
use CastsEnums;
|
2021-07-25 19:11:33 +02:00
|
|
|
use HasFactory;
|
2021-06-18 15:39:19 +02:00
|
|
|
use IsCrudModel;
|
2021-07-25 19:11:33 +02:00
|
|
|
use Notifiable;
|
2021-06-28 17:04:15 +02:00
|
|
|
use SoftDeletes;
|
2021-06-18 15:39:19 +02:00
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast to Enums.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-06-18 15:39:19 +02:00
|
|
|
protected $enumCasts = [
|
|
|
|
|
'role' => Role::class
|
|
|
|
|
];
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* Crud Label
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-06-18 15:39:19 +02:00
|
|
|
protected $crudLabelColumn = 'name';
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* Crud Value
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-06-18 15:39:19 +02:00
|
|
|
protected $crudValueColumn = 'id';
|
2020-11-18 10:23:51 +01:00
|
|
|
|
2022-06-30 17:51:15 +02:00
|
|
|
/**
|
|
|
|
|
* stores unhashed pwd
|
|
|
|
|
*
|
|
|
|
|
* @var string|null
|
|
|
|
|
*/
|
2022-06-09 16:57:24 +02:00
|
|
|
public $tmpPass = null;
|
|
|
|
|
|
2020-11-18 10:23:51 +01:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
|
|
|
|
'email',
|
|
|
|
|
'password',
|
2021-07-27 12:27:27 +02:00
|
|
|
'role',
|
|
|
|
|
'sites'
|
2020-11-18 10:23:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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',
|
|
|
|
|
];
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* User
|
|
|
|
|
*
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(array $attributes = [])
|
2021-06-18 15:39:19 +02:00
|
|
|
{
|
|
|
|
|
self::$editableProperties = [
|
|
|
|
|
'name' => [
|
|
|
|
|
'label' => trans('Nom'),
|
|
|
|
|
'type' => CrudType::string(),
|
|
|
|
|
'validate' => [
|
|
|
|
|
'string',
|
|
|
|
|
'max:255'
|
|
|
|
|
],
|
|
|
|
|
'required' => true,
|
|
|
|
|
'nullable' => false,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'email' => [
|
|
|
|
|
'label' => trans('Email'),
|
|
|
|
|
'type' => CrudType::email(),
|
|
|
|
|
'required' => true,
|
|
|
|
|
'nullable' => false,
|
|
|
|
|
'validate' => [
|
2021-06-28 17:04:15 +02:00
|
|
|
'max:255',
|
|
|
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
|
|
|
function ($attribute, $value, $fail) {
|
|
|
|
|
$q = static::query();
|
|
|
|
|
if (request()->user) {
|
|
|
|
|
$q = $q->where('id', '!=', $this->id);
|
|
|
|
|
}
|
|
|
|
|
if ($q->where('email', $value)->exists()) {
|
|
|
|
|
$fail(__(':attribute est déjà utilisé', [
|
|
|
|
|
'attribute' => $attribute
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-18 15:39:19 +02:00
|
|
|
],
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
2021-06-18 17:06:16 +02:00
|
|
|
'role' => [
|
|
|
|
|
'label' => trans('Role'),
|
|
|
|
|
'type' => CrudType::enum(),
|
|
|
|
|
'enum' => Role::class,
|
|
|
|
|
'required' => true,
|
|
|
|
|
'nullable' => false,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
],
|
2021-07-23 11:34:16 +02:00
|
|
|
'sites' => [
|
2021-09-28 10:52:53 +02:00
|
|
|
'label' => trans('Sites'),
|
2021-06-18 15:39:19 +02:00
|
|
|
'type' => CrudType::belongsToMany(),
|
2021-07-23 11:34:16 +02:00
|
|
|
'belongsToMany' => Site::class,
|
2021-06-18 15:39:19 +02:00
|
|
|
'belongsToManyLabel' => 'name',
|
2021-06-28 16:04:52 +02:00
|
|
|
'nullable' => true,
|
2021-06-18 15:39:19 +02:00
|
|
|
'readonly' => function () {
|
2021-07-23 11:04:09 +02:00
|
|
|
/** @var \App\Models\User */
|
|
|
|
|
$user = auth()->user();
|
2021-07-24 14:20:56 +02:00
|
|
|
return !$user->isAdmin();
|
2021-06-18 15:39:19 +02:00
|
|
|
},
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update(),
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
parent::__construct($attributes);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-06-28 17:04:15 +02:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (User $user) {
|
|
|
|
|
if (\is_null($user->password)) {
|
|
|
|
|
$user->password = Str::random(10);
|
2022-03-29 16:57:54 +02:00
|
|
|
$user->tmpPass = $user->password;
|
2021-06-28 17:04:15 +02:00
|
|
|
}
|
|
|
|
|
if (Hash::needsRehash($user->password)) {
|
|
|
|
|
$user->password = Hash::make($user->password);
|
|
|
|
|
}
|
2022-05-24 17:23:36 +02:00
|
|
|
Cache::flush('AdminUsers');
|
2021-06-28 17:04:15 +02:00
|
|
|
});
|
|
|
|
|
static::updating(function (User $user) {
|
|
|
|
|
if (Hash::needsRehash($user->password)) {
|
|
|
|
|
$user->password = Hash::make($user->password);
|
|
|
|
|
}
|
2022-05-24 17:23:36 +02:00
|
|
|
Cache::flush('AdminUsers');
|
2021-06-28 17:04:15 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HasMany Ticket relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2021-04-18 20:31:07 +02:00
|
|
|
public function tickets()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Ticket::class, 'created_by');
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|
2020-11-25 16:05:21 +01:00
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* HasMany Message relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2021-04-18 20:31:07 +02:00
|
|
|
public function messages()
|
|
|
|
|
{
|
2022-05-24 17:23:36 +02:00
|
|
|
return $this->hasMany(Message::class, 'user_id');
|
2020-11-25 16:05:21 +01:00
|
|
|
}
|
2020-11-26 15:48:35 +01:00
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* BelongsToMany Ticket relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
*/
|
2021-04-18 20:31:07 +02:00
|
|
|
public function subscriptions()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Ticket::class, 'subscriptions');
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* BelongsToMany Site relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
*/
|
2021-07-23 11:34:16 +02:00
|
|
|
public function sites()
|
2021-04-18 20:31:07 +02:00
|
|
|
{
|
2021-07-23 11:34:16 +02:00
|
|
|
return $this->belongsToMany(Site::class);
|
2020-11-26 15:48:35 +01:00
|
|
|
}
|
2021-06-29 09:12:35 +02:00
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* HasMany Schedule relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2022-02-09 13:11:29 +00:00
|
|
|
public function schedules()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Schedule::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
// * METHODS
|
|
|
|
|
|
2022-06-30 17:51:15 +02:00
|
|
|
/**
|
|
|
|
|
* get User brands
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Brand>
|
|
|
|
|
*/
|
|
|
|
|
public function brands()
|
|
|
|
|
{
|
|
|
|
|
return $this->sites->pluck('brand')->filter();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* Send User creation confirm mail
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-06-29 09:12:35 +02:00
|
|
|
public function sendMailConfirm()
|
|
|
|
|
{
|
|
|
|
|
dispatch(new SendUserCreatedEmail($this));
|
|
|
|
|
}
|
2021-07-23 11:04:09 +02:00
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
|
|
|
|
* Send User reset password mail
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $token
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-08-01 18:19:55 +02:00
|
|
|
public function sendPasswordResetNotification($token)
|
|
|
|
|
{
|
2022-05-24 17:23:36 +02:00
|
|
|
/** @var string $token */
|
2021-08-01 18:19:55 +02:00
|
|
|
$this->notify(new ResetPassword($token));
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 11:04:09 +02:00
|
|
|
/**
|
|
|
|
|
* Is the user an admin or a developper ?
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function isAdmin(): bool
|
|
|
|
|
{
|
|
|
|
|
return ($this->role->equals(Role::developper()) or
|
|
|
|
|
$this->role->equals(Role::admin()));
|
|
|
|
|
}
|
2022-05-24 17:23:36 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get All Admin users
|
|
|
|
|
*
|
|
|
|
|
* @return Collection
|
|
|
|
|
*/
|
|
|
|
|
public static function adminUsers(): Collection
|
|
|
|
|
{
|
|
|
|
|
return Cache::rememberForever('AdminUsers', function () {
|
|
|
|
|
return self::whereIn('role', [Role::admin(), Role::developper()])->get();
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|