Skip to content

Commit

Permalink
(feat): remove caps from cloned role
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonvanWijhe committed Nov 7, 2024
1 parent 3eda111 commit 11b66c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/UserRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,18 @@ private function addCustomRoles(): void
}

$capabilities = [];
$removeCapabilities = [];

if ($this->cloneValid($properties)) {
$this->createRole($role, $properties, ['clone' => $properties['clone']['from']]);

if (isset($properties['clone']['add']) && is_array($properties['clone']['add'])) {
$capabilities = $this->capabilitiesFromRoleProperties($properties['clone']['add']);
}

if (isset($properties['clone']['remove']) && is_array($properties['clone']['remove'])) {
$removeCapabilities = $this->capabilitiesFromRoleProperties($properties['clone']['remove']);
}
} else {
$this->createRole($role, $properties);
$capabilities = $this->capabilitiesFromRoleProperties($properties);
Expand All @@ -117,6 +122,10 @@ private function addCustomRoles(): void
foreach ($capabilities as $cap => $grant) {
$role->add_cap((string)$cap, $grant);
}

foreach ($removeCapabilities as $cap => $grant) {
$role->remove_cap((string)$cap);
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/UserRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,37 @@ function mockEmptyCurrentRoles(): void
(new UserRoles($config, $this->roleCommand, new WP_CLI))
->createRoles();
});

it('can remove caps from a cloned role', function () {
// ARRANGE //
$config = [
'prefix' => 'yard',
'roles' => [
'visitor' => [
'display_name' => 'Bezoeker',
'clone' => [
'from' => 'subscriber',
'remove' => [
'caps' => [
'my_custom_cap',
],
],
],
],
],
];

// EXPECT //
$this->roleCommand->shouldReceive('create')
->once()
->with(['yard_visitor', 'Bezoeker'], ['clone' => 'subscriber']);

$this->wpRole->shouldReceive('remove_cap')
->once()
->with('my_custom_cap');

// ACT //
(new UserRoles($config, $this->roleCommand, new WP_CLI))
->createRoles();
});
});

0 comments on commit 11b66c3

Please sign in to comment.