Version 0.7.0 #1
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Models\Enums\SystemRoleEnum;
|
use App\Enums\SystemRole as SystemRoleEnum;
|
||||||
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
@ -20,7 +21,7 @@ final class Role extends Model
|
|||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'name',
|
'name',
|
||||||
'slug'
|
'code'
|
||||||
];
|
];
|
||||||
|
|
||||||
public function scopeLatest(Builder $query): Builder
|
public function scopeLatest(Builder $query): Builder
|
||||||
@ -38,22 +39,17 @@ final class Role extends Model
|
|||||||
return $this->hasMany(RolePermission::class, 'role_id', 'id');
|
return $this->hasMany(RolePermission::class, 'role_id', 'id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function isRemove(): Attribute
|
||||||
/**
|
|
||||||
* Проверяем можем мы удалять эту группу или нет.
|
|
||||||
* Есть системные группы, которые нельзя удалять.
|
|
||||||
*/
|
|
||||||
public function isRemove(): bool
|
|
||||||
{
|
{
|
||||||
$dontRemove = SystemRoleEnum::toArray();
|
return Attribute::make(
|
||||||
return (array_search($this->slug, $dontRemove) === false);
|
get: fn ($dontRemove) => ( SystemRoleEnum::tryFrom($this->code) === null ),
|
||||||
|
)->shouldCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function isAdmin(): Attribute
|
||||||
* Проверка эта группа самая главная или нет.
|
|
||||||
*/
|
|
||||||
public function isAdmin(): bool
|
|
||||||
{
|
{
|
||||||
return ($this->slug === SystemRoleEnum::Admin->value);
|
return Attribute::make(
|
||||||
|
get: fn () => ( $this->code === SystemRoleEnum::Admin->value ),
|
||||||
|
)->shouldCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user