Skip to content

Commit

Permalink
Add Enum support for Role and WithoutRole scopes
Browse files Browse the repository at this point in the history
Ref #2391
  • Loading branch information
drbyte committed Jul 16, 2023
1 parent bf4b198 commit 299fe4b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function roles(): BelongsToMany
/**
* Scope the model query to certain roles only.
*
* @param string|int|array|Role|Collection $roles
* @param string|int|array|Role|Collection|\BackedEnum $roles
* @param string $guard
*/
public function scopeRole(Builder $query, $roles, $guard = null): Builder
Expand All @@ -82,6 +82,10 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
return $role;
}

if ($role instanceof \BackedEnum) {
$role = $role->value;
}

$method = is_int($role) || PermissionRegistrar::isUid($role) ? 'findById' : 'findByName';

return $this->getRoleClass()::{$method}($role, $guard ?: $this->getDefaultGuardName());
Expand All @@ -98,7 +102,7 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
/**
* Scope the model query to only those without certain roles.
*
* @param string|int|array|Role|Collection $roles
* @param string|int|array|Role|Collection|\BackedEnum $roles
* @param string $guard
*/
public function scopeWithoutRole(Builder $query, $roles, $guard = null): Builder
Expand All @@ -112,6 +116,10 @@ public function scopeWithoutRole(Builder $query, $roles, $guard = null): Builder
return $role;
}

if ($role instanceof \BackedEnum) {
$role = $role->value;
}

$method = is_int($role) || PermissionRegistrar::isUid($role) ? 'findById' : 'findByName';

return $this->getRoleClass()::{$method}($role, $guard ?: $this->getDefaultGuardName());
Expand All @@ -128,7 +136,7 @@ public function scopeWithoutRole(Builder $query, $roles, $guard = null): Builder
/**
* Returns roles ids as array keys
*
* @param array|string|int|Role|Collection $roles
* @param array|string|int|Role|Collection|\BackedEnum $roles
*/
private function collectRoles(...$roles): array
{
Expand Down
31 changes: 31 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ public function it_can_assign_and_remove_a_role_using_enums()
$this->assertFalse($this->testUser->hasRole($enum1));
}

/**
* @test
*
* @requires PHP >= 8.1
*/
public function it_can_scope_a_role_using_enums()
{
$enum1 = TestModels\TestRolePermissionsEnum::USERMANAGER;
$enum2 = TestModels\TestRolePermissionsEnum::WRITER;
$role1 = app(Role::class)->findOrCreate($enum1->value, 'web');
$role2 = app(Role::class)->findOrCreate($enum2->value, 'web');

User::all()->each(fn ($item) => $item->delete());
$user1 = User::create(['email' => '[email protected]']);
$user2 = User::create(['email' => '[email protected]']);
$user3 = User::create(['email' => '[email protected]']);

// assign only one user to a role
$user2->assignRole($enum1);
$this->assertTrue($user2->hasRole($enum1));
$this->assertFalse($user2->hasRole($enum2));

$scopedUsers1 = User::role($enum1)->get();
$scopedUsers2 = User::role($enum2)->get();
$scopedUsers3 = User::withoutRole($enum2)->get();

$this->assertEquals(1, $scopedUsers1->count());
$this->assertEquals(0, $scopedUsers2->count());
$this->assertEquals(3, $scopedUsers3->count());
}

/** @test */
public function it_can_assign_and_remove_a_role()
{
Expand Down

0 comments on commit 299fe4b

Please sign in to comment.