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
|
|
|
/**
|
2025-05-20 10:16:12 +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.
|
2025-05-26 11:33:47 +02:00
|
|
|
* @property string|null $tenant_id Associated tenant.
|
2025-05-20 10:16:12 +02:00
|
|
|
* @property boolean $status App user status.
|
2025-05-26 11:33:47 +02:00
|
|
|
* @property \App\Enums\AppUserRolesEnum $role App user role.
|
2025-05-20 10:16:12 +02:00
|
|
|
* @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
|
|
|
/**
|
2025-05-20 10:16:12 +02: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.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @var list<string>
|
2023-07-25 12:55:22 +02:00
|
|
|
*/
|
|
|
|
|
protected $hidden = [
|
|
|
|
|
'password',
|
|
|
|
|
];
|
|
|
|
|
|
2023-03-31 15:24:32 +02:00
|
|
|
/**
|
2025-05-20 10:16:12 +02:00
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @var array<string, string>
|
2023-03-31 15:24:32 +02:00
|
|
|
*/
|
2025-05-20 10:16:12 +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);
|
2025-05-20 10:16:12 +02:00
|
|
|
self::updatePassword($model);
|
2023-03-15 10:41:01 +01:00
|
|
|
});
|
|
|
|
|
static::updating(function (self $model) {
|
2025-05-20 10:16:12 +02:00
|
|
|
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.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @param self $model
|
2023-03-31 15:24:32 +02:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
private static function setOrder(self $model): void
|
2023-03-31 15:24:32 +02:00
|
|
|
{
|
2025-05-26 11:33:47 +02:00
|
|
|
$model->order = \intval(
|
|
|
|
|
self::query()->where('tenant_id', \tenant() ?? Tenant::first()->getKey())->max('order')
|
|
|
|
|
) + 1;
|
2023-03-31 15:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MorphToMany Directories
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
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
|
|
|
}
|