ldc-supportweb/app/Models/User.php

198 lines
4.4 KiB
PHP
Raw Normal View History

2020-11-18 10:23:51 +01:00
<?php
namespace App\Models;
use App\Enums\Users\RoleEnum;
2021-06-29 09:12:35 +02:00
use App\Jobs\SendUserCreatedEmail;
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;
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;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
2020-11-18 10:23:51 +01:00
/**
* @property string $name
* @property string $email
* @property string $password
* @property string $remember_token
* @property RoleEnum $role
* @property Collection $sites
*/
2020-11-18 10:23:51 +01:00
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
use SoftDeletes;
/**
* 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 fillable.
2020-11-18 10:23:51 +01:00
*
* @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.
2020-11-18 10:23:51 +01:00
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
'role' => RoleEnum::class
2020-11-18 10:23:51 +01:00
];
2022-05-24 17:23:36 +02:00
/**
* 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);
2022-03-29 16:57:54 +02:00
$user->tmpPass = $user->password;
}
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($user->password);
}
2022-05-24 17:23:36 +02:00
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);
}
2022-05-24 17:23:36 +02:00
Cache::flush('AdminUsers');
});
}
// * METHODS
2022-05-24 17:23:36 +02:00
/**
* get User brands
2022-05-24 17:23:36 +02:00
*
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Brand>
2022-05-24 17:23:36 +02:00
*/
public function brands()
2021-04-18 20:31:07 +02:00
{
return $this->sites->pluck('brand')->filter();
2020-11-18 10:23:51 +01:00
}
2022-05-24 17:23:36 +02:00
/**
* Send User creation confirm mail
2022-05-24 17:23:36 +02:00
*
* @return void
2022-05-24 17:23:36 +02:00
*/
public function sendMailConfirm()
2021-04-18 20:31:07 +02:00
{
dispatch(new SendUserCreatedEmail($this));
}
2022-05-24 17:23:36 +02:00
/**
* Send User reset password mail
2022-05-24 17:23:36 +02:00
*
* @param mixed $token
* @return void
2022-05-24 17:23:36 +02:00
*/
public function sendPasswordResetNotification(mixed $token)
2021-04-18 20:31:07 +02:00
{
/** @var string $token */
$this->notify(new ResetPassword($token));
2021-04-18 20:31:07 +02:00
}
2022-05-24 17:23:36 +02:00
/**
* Is the user an admin or a developper ?
2022-05-24 17:23:36 +02:00
*
* @return boolean
2022-05-24 17:23:36 +02:00
*/
public function isAdmin(): bool
2021-04-18 20:31:07 +02:00
{
return ($this->role->equals(RoleEnum::developper) or
$this->role->equals(RoleEnum::admin));
}
2021-06-29 09:12:35 +02:00
2022-05-24 17:23:36 +02:00
/**
* Get All Admin users
2022-05-24 17:23:36 +02:00
*
* @return Collection
2022-05-24 17:23:36 +02:00
*/
public static function adminUsers(): Collection
2022-02-09 13:11:29 +00:00
{
2025-04-11 18:30:04 +02:00
return Cache::remember('AdminUsers', 86400, function () {
return self::whereIn('role', [RoleEnum::admin, RoleEnum::developper])->orderBy('name')->get();
});
2022-02-09 13:11:29 +00:00
}
// * RELATIONSHIPS
2022-05-24 17:23:36 +02:00
/**
* HasMany Ticket relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tickets()
{
return $this->hasMany(Ticket::class, 'created_by');
}
2022-05-24 17:23:36 +02:00
/**
* HasMany Message relation
2022-05-24 17:23:36 +02:00
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2022-05-24 17:23:36 +02:00
*/
public function messages()
2021-06-29 09:12:35 +02:00
{
return $this->hasMany(Message::class, 'user_id');
2021-06-29 09:12:35 +02:00
}
2021-07-23 11:04:09 +02:00
2022-05-24 17:23:36 +02:00
/**
* BelongsToMany Ticket relation
2022-05-24 17:23:36 +02:00
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
2022-05-24 17:23:36 +02:00
*/
public function subscriptions()
{
return $this->belongsToMany(Ticket::class, 'subscriptions');
}
2021-07-23 11:04:09 +02:00
/**
* BelongsToMany Site relation
2021-07-23 11:04:09 +02:00
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
2021-07-23 11:04:09 +02:00
*/
public function sites()
2021-07-23 11:04:09 +02:00
{
return $this->belongsToMany(Site::class);
2021-07-23 11:04:09 +02:00
}
2020-11-18 10:23:51 +01:00
}