2020-11-18 10:23:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-08-14 09:23:39 +00:00
|
|
|
use App\Enums\Users\RoleEnum;
|
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;
|
2020-11-18 10:23:51 +01:00
|
|
|
|
2023-08-14 09:23:39 +00:00
|
|
|
/**
|
2026-06-24 16:25:58 +02:00
|
|
|
* @property int $id
|
2023-08-14 09:23:39 +00:00
|
|
|
* @property string $name
|
|
|
|
|
* @property string $email
|
|
|
|
|
* @property string $password
|
|
|
|
|
* @property RoleEnum $role
|
2026-06-24 16:25:58 +02:00
|
|
|
* @property string|null $remember_token
|
|
|
|
|
* @property int|null $gitlab_id ID du user Gitlab
|
|
|
|
|
* @property int|null $favourite_site_id
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $deleted_at
|
|
|
|
|
* @property-read Collection<int, \App\Models\Message> $messages
|
|
|
|
|
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
|
|
|
|
|
* @property-read Collection<int, \App\Models\Site> $sites
|
|
|
|
|
* @property-read Collection<int, \App\Models\Ticket> $subscriptions
|
|
|
|
|
* @property-read Collection<int, \App\Models\Ticket> $tickets
|
|
|
|
|
* @method static \Database\Factories\UserFactory factory($count = null, $state = [])
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|User onlyTrashed()
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|User withTrashed(bool $withTrashed = true)
|
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|User withoutTrashed()
|
|
|
|
|
* @mixin \Eloquent
|
2023-08-14 09:23:39 +00:00
|
|
|
*/
|
2020-11-18 10:23:51 +01:00
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
2021-07-25 19:11:33 +02:00
|
|
|
use HasFactory;
|
|
|
|
|
use Notifiable;
|
2021-06-28 17:04:15 +02:00
|
|
|
use SoftDeletes;
|
2021-06-18 15:39:19 +02: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
|
|
|
/**
|
2023-08-14 09:23:39 +00: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',
|
2025-04-17 17:05:50 +02:00
|
|
|
'sites',
|
2025-07-10 12:12:21 +02:00
|
|
|
'gitlab_id',
|
|
|
|
|
'favourite_site_id'
|
2020-11-18 10:23:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $hidden = [
|
|
|
|
|
'password',
|
|
|
|
|
'remember_token',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* The attributes that should be cast.
|
2020-11-18 10:23:51 +01:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'email_verified_at' => 'datetime',
|
2023-08-14 09:23:39 +00:00
|
|
|
'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
|
|
|
|
|
*/
|
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
|
|
|
});
|
2023-09-25 17:35:14 +02:00
|
|
|
static::created(function (User $user) {
|
|
|
|
|
$user->sendMailConfirm();
|
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 09:23:39 +00:00
|
|
|
// * METHODS
|
2022-05-24 17:23:36 +02:00
|
|
|
|
|
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* get User brands
|
2022-05-24 17:23:36 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Brand>
|
2022-05-24 17:23:36 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
public function brands()
|
2021-04-18 20:31:07 +02:00
|
|
|
{
|
2023-08-14 09:23:39 +00:00
|
|
|
return $this->sites->pluck('brand')->filter();
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|
2020-11-25 16:05:21 +01:00
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* Send User creation confirm mail
|
2022-05-24 17:23:36 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @return void
|
2022-05-24 17:23:36 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
public function sendMailConfirm()
|
2021-04-18 20:31:07 +02:00
|
|
|
{
|
2023-08-14 09:23:39 +00:00
|
|
|
dispatch(new SendUserCreatedEmail($this));
|
2020-11-25 16:05:21 +01:00
|
|
|
}
|
2020-11-26 15:48:35 +01:00
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* Send User reset password mail
|
2022-05-24 17:23:36 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @param mixed $token
|
|
|
|
|
* @return void
|
2022-05-24 17:23:36 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
public function sendPasswordResetNotification(mixed $token)
|
2021-04-18 20:31:07 +02:00
|
|
|
{
|
2023-08-14 09:23:39 +00: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
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* Is the user an admin or a developper ?
|
2022-05-24 17:23:36 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @return boolean
|
2022-05-24 17:23:36 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
public function isAdmin(): bool
|
2021-04-18 20:31:07 +02:00
|
|
|
{
|
2023-08-14 09:23:39 +00:00
|
|
|
return ($this->role->equals(RoleEnum::developper) or
|
|
|
|
|
$this->role->equals(RoleEnum::admin));
|
2020-11-26 15:48:35 +01:00
|
|
|
}
|
2021-06-29 09:12:35 +02:00
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* Get All Admin users
|
2022-05-24 17:23:36 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @return Collection
|
2022-05-24 17:23:36 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00: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();
|
2023-08-14 09:23:39 +00:00
|
|
|
});
|
2022-02-09 13:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-14 09:23:39 +00:00
|
|
|
// * RELATIONSHIPS
|
2022-05-24 17:23:36 +02:00
|
|
|
|
2022-06-30 17:51:15 +02:00
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* HasMany Ticket relation
|
2022-06-30 17:51:15 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
2022-06-30 17:51:15 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
public function tickets()
|
2022-06-30 17:51:15 +02:00
|
|
|
{
|
2023-08-14 09:23:39 +00:00
|
|
|
return $this->hasMany(Ticket::class, 'created_by');
|
2022-06-30 17:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 17:23:36 +02:00
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* HasMany Message relation
|
2022-05-24 17:23:36 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
2022-05-24 17:23:36 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
public function messages()
|
2021-06-29 09:12:35 +02:00
|
|
|
{
|
2023-08-14 09:23:39 +00: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
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* BelongsToMany Ticket relation
|
2022-05-24 17:23:36 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
2022-05-24 17:23:36 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
public function subscriptions()
|
2021-08-01 18:19:55 +02:00
|
|
|
{
|
2023-08-14 09:23:39 +00:00
|
|
|
return $this->belongsToMany(Ticket::class, 'subscriptions');
|
2021-08-01 18:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 11:04:09 +02:00
|
|
|
/**
|
2023-08-14 09:23:39 +00:00
|
|
|
* BelongsToMany Site relation
|
2021-07-23 11:04:09 +02:00
|
|
|
*
|
2023-08-14 09:23:39 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
2021-07-23 11:04:09 +02:00
|
|
|
*/
|
2023-08-14 09:23:39 +00:00
|
|
|
public function sites()
|
2021-07-23 11:04:09 +02:00
|
|
|
{
|
2023-08-14 09:23:39 +00:00
|
|
|
return $this->belongsToMany(Site::class);
|
2021-07-23 11:04:09 +02:00
|
|
|
}
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|