ldc-supportweb/app/Models/User.php
2022-05-25 09:07:47 +02:00

293 lines
7.5 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Role;
use App\Jobs\SendUserCreatedEmail;
use App\Notifications\ResetPassword;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
use Kwaadpepper\Enum\Traits\CastsEnums;
class User extends Authenticatable
{
use CastsEnums;
use HasFactory;
use IsCrudModel;
use Notifiable;
use SoftDeletes;
/**
* The attributes that should be cast to Enums.
*
* @var array
*/
protected $enumCasts = [
'role' => Role::class
];
/**
* Crud Label
*
* @var string
*/
protected $crudLabelColumn = 'name';
/**
* Crud Value
*
* @var string
*/
protected $crudValueColumn = 'id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
'role',
'sites'
];
/**
* 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',
];
/**
* User
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
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' => [
'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
]));
}
}
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'role' => [
'label' => trans('Role'),
'type' => CrudType::enum(),
'enum' => Role::class,
'required' => true,
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'sites' => [
'label' => trans('Sites'),
'type' => CrudType::belongsToMany(),
'belongsToMany' => Site::class,
'belongsToManyLabel' => 'name',
'nullable' => true,
'readonly' => function () {
/** @var \App\Models\User */
$user = auth()->user();
return !$user->isAdmin();
},
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
]
];
parent::__construct($attributes);
}
/**
* 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::updating(function (User $user) {
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($user->password);
}
Cache::flush('AdminUsers');
});
}
// * 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);
}
/**
* HasMany Schedule relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function schedules()
{
return $this->hasMany(Schedule::class);
}
// * METHODS
/**
* 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($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(Role::developper()) or
$this->role->equals(Role::admin()));
}
/**
* 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();
});
}
}