Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix $user->roles($purpose) #6653

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/api/routes/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ function ($source, $filename) use ($id) {
'users/(:any)/roles',
],
'action' => function (string $id) {
return $this->user($id)->roles();
$kirby = $this->kirby();
$purpose = $kirby->request()->get('purpose');
return $this->user($id)->roles($purpose);
bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved
}
],
[
Expand Down
39 changes: 21 additions & 18 deletions src/Cms/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,33 +574,36 @@ public function role(): Role
}

/**
* Returns all available roles
* for this user, that can be selected
* by the authenticated user
* Returns all available roles for this user,
* that can be selected by the authenticated user
*
* @param string|null $purpose User action for which the roles are used (create, change)
*/
public function roles(): Roles
public function roles(string|null $purpose = null): Roles
{
$kirby = $this->kirby();
$roles = $kirby->roles();

// a collection with just the one role of the user
$myRole = $roles->filter('id', $this->role()->id());
// for the last admin, only their current role (admin) is available
if ($this->isLastAdmin() === true) {
// a collection with just the one role of the user
return $roles->filter('id', $this->role()->id());
}

// if there's an authenticated user …
// admin users can select pretty much any role
if ($kirby->user()?->isAdmin() === true) {
// except if the user is the last admin
if ($this->isLastAdmin() === true) {
// in which case they have to stay admin
return $myRole;
}
// filter roles based on the user action
// as user permissions and/or options can restrict these further
$roles = match ($purpose) {
'create' => $roles->canBeCreated(),
'change' => $roles->canBeChanged(),
default => $roles
};

// return all roles for mighty admins
return $roles;
// exclude the admin role, if the user isn't an admin themselves
if ($kirby->user()?->isAdmin() !== true) {
$roles = $roles->filter(fn ($role) => $role->name() !== 'admin');
}

// any other user can only keep their role
return $myRole;
return $roles;
}

/**
Expand Down
101 changes: 101 additions & 0 deletions tests/Cms/Users/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,107 @@ public static function passwordProvider(): array
];
}

/**
* @covers ::roles
*/
public function testRoles(): void
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'roles' => [
['name' => 'admin'],
['name' => 'editor'],
['name' => 'guest']
],
'users' => [
[
'email' => '[email protected]',
'role' => 'admin'
],
[
'email' => '[email protected]',
'role' => 'editor'
]
],
]);

// last admin has only admin role as option
$user = $app->user('[email protected]');
$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['admin'], $roles);

// normal user should not have admin as option
$user = $app->user('[email protected]');
$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['editor', 'guest'], $roles);

// only if current user is admin, normal user can also have admin option
$app->impersonate('[email protected]');
$user = $app->user('[email protected]');
$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['admin', 'editor', 'guest'], $roles);
}

/**
* @covers ::roles
*/
public function testRolesFilteredForPurpose(): void
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'blueprints' => [
'users/admin' => [
'name' => 'admin',
],
'users/editor' => [
'name' => 'editor',
],
'users/client' => [
'name' => 'client',
'options' => [
'create' => [
'editor' => false
]
]
],
'users/guest' => [
'name' => 'guest',
'options' => [
'changeRole' => [
'editor' => false
]
]
]
],
'users' => [
[
'email' => '[email protected]',
'role' => 'admin'
],
[
'email' => '[email protected]',
'role' => 'editor'
]
],
]);

$app->impersonate('[email protected]');
$user = $app->user('[email protected]');

$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['client', 'editor', 'guest'], $roles);

$roles = $user->roles('create')->values(fn ($role) => $role->id());
$this->assertSame(['editor', 'guest'], $roles);

$roles = $user->roles('change')->values(fn ($role) => $role->id());
$this->assertSame(['client', 'editor'], $roles);
}

public function testSecret()
{
$app = new App([
Expand Down
Loading