2023-03-14 16:17:23 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-03-15 09:17:52 +01:00
|
|
|
use App\Enums\AppUserRolesEnum;
|
2023-03-31 15:24:32 +02:00
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
2023-03-14 16:17:23 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-03-15 10:41:01 +01:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2023-03-15 09:17:52 +01:00
|
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
2023-03-14 16:17:23 +01:00
|
|
|
|
2023-03-31 15:24:32 +02:00
|
|
|
/**
|
|
|
|
|
* @property integer $id
|
|
|
|
|
* @property string $lastname
|
|
|
|
|
* @property string $firstname
|
|
|
|
|
* @property string $email
|
|
|
|
|
* @property string $password
|
|
|
|
|
* @property integer $role
|
|
|
|
|
* @property boolean $status
|
|
|
|
|
* @property integer $order
|
|
|
|
|
*/
|
2023-03-14 16:17:23 +01:00
|
|
|
class AppUser extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2023-03-15 09:17:52 +01:00
|
|
|
use CastsEnums;
|
|
|
|
|
|
2023-03-15 10:41:01 +01:00
|
|
|
/**
|
|
|
|
|
* fillable
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'lastname',
|
|
|
|
|
'firstname',
|
|
|
|
|
'email',
|
|
|
|
|
'password',
|
|
|
|
|
'role',
|
|
|
|
|
'status',
|
2023-03-31 15:24:32 +02:00
|
|
|
'order',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* cast enum
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
|
|
|
|
protected $enumCasts = [
|
|
|
|
|
'role' => AppUserRolesEnum::class
|
2023-03-15 10:41:01 +01:00
|
|
|
];
|
|
|
|
|
|
2023-03-31 15:24:32 +02:00
|
|
|
// * EVENTS
|
|
|
|
|
|
2023-03-15 10:41:01 +01:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (self $model) {
|
2023-03-31 15:24:32 +02:00
|
|
|
self::assertFieldIsUnique('email', $model->email);
|
|
|
|
|
self::setOrder($model);
|
|
|
|
|
$model->password = Hash::make($model->password);
|
2023-03-15 10:41:01 +01:00
|
|
|
});
|
|
|
|
|
static::updating(function (self $model) {
|
|
|
|
|
if (Hash::needsRehash($model->password)) {
|
|
|
|
|
$model->password = Hash::make($model->password);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-03-31 15:24:32 +02:00
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asserts using validation that the field is unique.
|
|
|
|
|
*
|
|
|
|
|
* @param string $nameField
|
|
|
|
|
* @param string $valueField
|
|
|
|
|
* @param integer|null $id
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
|
|
|
|
|
{
|
|
|
|
|
$table = (new self())->getTable();
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set order after the last element of the list.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private static function setOrder(Model $model): void
|
|
|
|
|
{
|
|
|
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MorphToMany Directories
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
|
|
|
*/
|
|
|
|
|
public function directories()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphToMany(Directory::class, 'taggable');
|
|
|
|
|
}
|
2023-03-14 16:17:23 +01:00
|
|
|
}
|