133 lines
3.5 KiB
PHP
133 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\AppUserRolesEnum;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|
|
|
/**
|
|
* @property integer $id App user id.
|
|
* @property string $lastname App user last name.
|
|
* @property string $firstname App user first name.
|
|
* @property string $email App user email.
|
|
* @property string $password App user password.
|
|
* @property integer $tenant_id Tenant key associated.
|
|
* @property \App\Enums\AppUserRolesEnum $role App user role.
|
|
* @property boolean $status App user status.
|
|
* @property integer $order App user order.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class AppUser extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'lastname',
|
|
'firstname',
|
|
'email',
|
|
'password',
|
|
'role',
|
|
'status',
|
|
'order',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'role' => AppUserRolesEnum::class
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::assertFieldIsUnique('email', $model->email);
|
|
self::setOrder($model);
|
|
self::updatePassword($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::updatePassword($model);
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Update password.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function updatePassword(self $model): void
|
|
{
|
|
if (Hash::needsRehash($model->password)) {
|
|
$model->password = Hash::make($model->password);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* MorphToMany Directories
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function directories(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphToMany(Directory::class, 'directorable');
|
|
}
|
|
}
|