ldc-supportweb/app/Models/User.php

202 lines
5.5 KiB
PHP
Raw Normal View History

2020-11-18 10:23:51 +01:00
<?php
namespace App\Models;
use App\Enums\CrudAction;
use App\Enums\CrudType;
use App\Enums\Role;
2021-06-29 09:12:35 +02:00
use App\Jobs\SendUserCreatedEmail;
use App\Traits\IsCrudModel;
2020-11-18 10:23:51 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
2020-11-18 10:23:51 +01:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Kwaadpepper\Enum\Traits\CastsEnums;
2020-11-18 10:23:51 +01:00
class User extends Authenticatable
{
2021-04-18 20:31:07 +02:00
use HasFactory;
use Notifiable;
use CastsEnums;
use IsCrudModel;
use SoftDeletes;
protected $enumCasts = [
'role' => Role::class
];
protected $crudLabelColumn = 'name';
protected $crudValueColumn = 'id';
2020-11-18 10:23:51 +01:00
2021-06-29 09:12:35 +02:00
// stores unhashed pwd
public $tmpPass = null;
2020-11-18 10:23:51 +01:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* 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',
];
public function __construct($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(),
]
],
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' => [
'label' => trans('Projets'),
'type' => CrudType::belongsToMany(),
2021-07-23 11:34:16 +02:00
'belongsToMany' => Site::class,
'belongsToManyLabel' => 'name',
2021-06-28 16:04:52 +02:00
'nullable' => true,
'readonly' => function () {
2021-07-23 11:04:09 +02:00
/** @var \App\Models\User */
$user = auth()->user();
return $user->isAdmin();
},
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
]
];
parent::__construct($attributes);
}
protected static function booted()
{
static::creating(function (User $user) {
if (\is_null($user->password)) {
$user->password = Str::random(10);
2021-06-29 09:12:35 +02:00
$user->tmpPass = $user->password;
}
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($user->password);
}
});
static::updating(function (User $user) {
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($user->password);
}
});
}
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
}
2021-04-18 20:31:07 +02:00
public function messages()
{
return $this->hasMany(Messages::class, 'user_id');
}
2021-04-18 20:31:07 +02:00
public function subscriptions()
{
return $this->belongsToMany(Ticket::class, 'subscriptions');
}
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);
}
2021-06-29 09:12:35 +02:00
public function sendMailConfirm()
{
dispatch(new SendUserCreatedEmail($this));
}
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()));
}
2020-11-18 10:23:51 +01:00
}