Skip to content

Commit

Permalink
Refactor HasRole trait to support custom role column and enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Dec 25, 2023
1 parent d4d0845 commit 1888b88
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/Traits/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,56 @@

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;
}

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)
;
}
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1888b88

Please sign in to comment.