diff --git a/src/Traits/HasRole.php b/src/Traits/HasRole.php index b5fe3c3..08d8dc1 100644 --- a/src/Traits/HasRole.php +++ b/src/Traits/HasRole.php @@ -2,22 +2,48 @@ namespace Kiwilan\Steward\Traits; +use BackedEnum; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Validation\Rules\Enum; use Kiwilan\Steward\Enums\UserRoleEnum; - +use UnitEnum; + +/** + * Trait HasRole + * + * - Default role column is `role`, can be override by setting `$role_column` property + * - Default enum with is `\Kiwilan\Steward\Enums\UserRoleEnum`, can be override by setting `$role_enum` property + * + * ```php + * class Post extends Model + * { + * use HasRole; + * + * protected $role_column = 'role_column'; // default is `role` + * protected $role_enum = 'role_enum'; // default is `\Kiwilan\Steward\Enums\UserRoleEnum` + * } + * ``` + */ trait HasRole { protected $default_role_column = 'role'; + protected $default_role_enum = UserRoleEnum::class; + public function initializeHasRole() { $this->fillable[] = $this->getRoleColumn(); $this->fillable[] = 'is_blocked'; - $this->casts[$this->getRoleColumn()] = UserRoleEnum::class; + $this->casts[$this->getRoleColumn()] = $this->getEnumClass(); $this->casts['is_blocked'] = 'boolean'; } + private function getEnumClass(): mixed + { + return $this->role_enum ?? $this->default_role_enum; + } + public function getRoleColumn(): string { return $this->role_column ?? $this->default_role_column; @@ -25,7 +51,7 @@ public function getRoleColumn(): string public function scopeHaveDashboardAccess(Builder $builder): Builder { - return $builder->where($this->getRoleColumn(), '!=', UserRoleEnum::user->value) + return $builder->where($this->getRoleColumn(), '!=', $this->getEnumClass()::user->value) ->where('is_blocked', '!=', true) ; } @@ -37,22 +63,22 @@ public function isDashboardAllowed(): bool public function isSuperAdmin(): bool { - return $this->role->value === UserRoleEnum::super_admin->value; + return $this->role->value === $this->getEnumClass()::super_admin->value; } public function isAdmin(): bool { - return $this->role->value === UserRoleEnum::admin->value; + return $this->role->value === $this->getEnumClass()::admin->value; } public function isEditor(): bool { - return $this->role->value === UserRoleEnum::editor->value; + return $this->role->value === $this->getEnumClass()::editor->value; } public function isUser(): bool { - return $this->role->value === UserRoleEnum::user->value; + return $this->role->value === $this->getEnumClass()::user->value; } public function isBlocked() @@ -82,11 +108,11 @@ protected function getIsUserAttribute(): bool public function scopeWhereIsUser(Builder $query) { - return $query->where('role', UserRoleEnum::user); + return $query->where('role', $this->getEnumClass()::user); } public function scopeWhereIsNotSuperAdmin(Builder $query) { - return $query->where('role', '!=', UserRoleEnum::super_admin); + return $query->where('role', '!=', $this->getEnumClass()::super_admin); } }