ldc-com-app-bo/app/Models/AppUser.php

120 lines
3 KiB
PHP
Raw Normal View History

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-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;
2025-05-20 16:20:23 +02:00
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
2023-03-14 16:17:23 +01:00
2023-03-31 15:24:32 +02:00
/**
* @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 string|null $tenant_id Associated tenant.
* @property boolean $status App user status.
* @property \App\Enums\AppUserRolesEnum $role App user role.
* @property integer $order App user order.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
2023-03-31 15:24:32 +02:00
*/
2023-03-14 16:17:23 +01:00
class AppUser extends Model
{
2025-05-20 16:20:23 +02:00
use BelongsToTenant;
2023-03-14 16:17:23 +01:00
use HasFactory;
2023-03-15 09:17:52 +01:00
2023-03-15 10:41:01 +01:00
/**
* The attributes that are mass assignable.
*
* @var list<string>
2023-03-15 10:41:01 +01:00
*/
protected $fillable = [
'lastname',
'firstname',
'email',
'password',
'role',
'status',
2023-03-31 15:24:32 +02:00
'order',
];
2023-07-25 12:55:22 +02:00
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
2023-07-25 12:55:22 +02:00
*/
protected $hidden = [
'password',
];
2023-03-31 15:24:32 +02:00
/**
* The attributes that should be cast.
*
* @var array<string, string>
2023-03-31 15:24:32 +02:00
*/
protected $casts = [
2023-03-31 15:24:32 +02:00
'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
*/
2025-05-20 16:20:23 +02:00
protected static function booted(): void
2023-03-15 10:41:01 +01:00
{
static::creating(function (self $model) {
2023-03-31 15:24:32 +02:00
self::setOrder($model);
self::updatePassword($model);
2023-03-15 10:41:01 +01:00
});
static::updating(function (self $model) {
self::updatePassword($model);
2023-03-15 10:41:01 +01:00
});
}
2023-03-31 15:24:32 +02:00
// * METHODS
2023-07-25 12:55:22 +02:00
/**
* 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);
}
}
2023-03-31 15:24:32 +02:00
/**
* Set order after the last element of the list.
*
* @param self $model
2023-03-31 15:24:32 +02:00
* @return void
*/
private static function setOrder(self $model): void
2023-03-31 15:24:32 +02:00
{
$model->order = \intval(
2025-06-17 12:29:15 +02:00
self::query()->where('tenant_id', \tenant()->getKey() ?? Tenant::first()->getKey())->max('order')
) + 1;
2023-03-31 15:24:32 +02:00
}
// * RELATIONSHIPS
/**
* MorphToMany Directories
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function directories(): \Illuminate\Database\Eloquent\Relations\MorphToMany
2023-03-31 15:24:32 +02:00
{
2023-07-13 08:34:54 +00:00
return $this->morphToMany(Directory::class, 'directorable');
2023-03-31 15:24:32 +02:00
}
2023-03-14 16:17:23 +01:00
}