197 lines
4.4 KiB
PHP
197 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Users\RoleEnum;
|
|
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;
|
|
|
|
/**
|
|
* @property string $name
|
|
* @property string $email
|
|
* @property string $password
|
|
* @property string $remember_token
|
|
* @property RoleEnum $role
|
|
* @property Collection $sites
|
|
*/
|
|
class User extends Authenticatable
|
|
{
|
|
use HasFactory;
|
|
use Notifiable;
|
|
use SoftDeletes;
|
|
|
|
/**
|
|
* stores unhashed pwd
|
|
*
|
|
* @var string|null
|
|
*/
|
|
public $tmpPass = null;
|
|
|
|
/**
|
|
* The attributes that are fillable.
|
|
*
|
|
* @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.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'email_verified_at' => 'datetime',
|
|
'role' => RoleEnum::class
|
|
];
|
|
|
|
/**
|
|
* 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::created(function (User $user) {
|
|
$user->sendMailConfirm();
|
|
});
|
|
static::updating(function (User $user) {
|
|
if (Hash::needsRehash($user->password)) {
|
|
$user->password = Hash::make($user->password);
|
|
}
|
|
Cache::flush('AdminUsers');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* get User brands
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Brand>
|
|
*/
|
|
public function brands()
|
|
{
|
|
return $this->sites->pluck('brand')->filter();
|
|
}
|
|
|
|
/**
|
|
* 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(mixed $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(RoleEnum::developper) or
|
|
$this->role->equals(RoleEnum::admin));
|
|
}
|
|
|
|
/**
|
|
* Get All Admin users
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public static function adminUsers(): Collection
|
|
{
|
|
return Cache::rememberForever('AdminUsers', function () {
|
|
return self::whereIn('role', [RoleEnum::admin, RoleEnum::developper])->get();
|
|
});
|
|
}
|
|
|
|
// * 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);
|
|
}
|
|
}
|