ldc-jeusselin/app/Models/User.php

110 lines
2.8 KiB
PHP
Raw Normal View History

2021-02-11 11:24:44 +01:00
<?php
namespace App\Models;
use App\Enums\UserRoleEnum;
use Illuminate\Auth\Passwords\CanResetPassword;
2021-02-11 11:24:44 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
2021-02-11 11:24:44 +01:00
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Kwaadpepper\Enum\Traits\CastsEnums;
2021-02-11 11:24:44 +01:00
/**
* @property string $hashcode
* @property string $firstname
* @property string $lastname
* @property string $email
* @property string $password
* @property string $password_token
* @property \App\Enums\UserRoleEnum $role
* @property string $remember_token
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*
* @package App\Models
*/
2021-02-11 11:24:44 +01:00
class User extends Authenticatable
{
use CanResetPassword;
use CastsEnums;
2023-08-16 15:27:22 +02:00
use HasFactory;
use Notifiable;
2021-04-02 13:30:11 +02:00
2021-02-11 11:24:44 +01:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname',
'lastname',
2021-02-11 11:24:44 +01:00
'email',
'password',
2023-08-16 15:27:22 +02:00
'role'
2021-02-11 11:24:44 +01:00
];
/**
* The attributes that should be hidden for arrays.
*
* @var array<string>
2021-02-11 11:24:44 +01:00
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Map attribute names to enum classes.
2021-02-11 11:24:44 +01:00
*
* @var array<string,string>
2021-02-11 11:24:44 +01:00
*/
protected $enumCasts = [
'role' => UserRoleEnum::class
2021-02-11 11:24:44 +01:00
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (User $user) {
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);
}
});
}
/**
* Send a password reset notification to the user.
*
* @param string $token
* @return void
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
*/
public function sendPasswordResetNotification($token)
{
// phpcs:enable
$notification = new ResetPasswordNotification($token);
// * Changed route here only
$notification->createUrlUsing(function ($notifiable, $token) {
return url(route('bo.password.reset', [
'token' => $token,
'email' => $notifiable->getEmailForPasswordReset(),
], false));
});
$this->notify($notification);
}
2021-02-11 11:24:44 +01:00
}