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() for last admin #6693

Merged
merged 1 commit into from
Sep 24, 2024
Merged
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
5 changes: 3 additions & 2 deletions src/Cms/User.php
Original file line number Diff line number Diff line change
@@ -584,8 +584,9 @@ public function roles(string|null $purpose = null): Roles
$kirby = $this->kirby();
$roles = $kirby->roles();

// for the last admin, only their current role (admin) is available
if ($this->isLastAdmin() === true) {
// for the last admin,
// only their current role (admin) is available for changing
if ($purpose === 'change' && $this->isLastAdmin() === true) {
// a collection with just the one role of the user
return $roles->filter('id', $this->role()->id());
}
10 changes: 5 additions & 5 deletions tests/Cms/Users/UserTest.php
Original file line number Diff line number Diff line change
@@ -367,11 +367,6 @@ public function testRoles(): void
],
]);

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

// normal user should not have admin as option
$user = $app->user('editor@getkirby.com');
$roles = $user->roles()->values(fn ($role) => $role->id());
@@ -382,6 +377,11 @@ public function testRoles(): void
$user = $app->user('editor@getkirby.com');
$roles = $user->roles()->values(fn ($role) => $role->id());
$this->assertSame(['admin', 'editor', 'guest'], $roles);

// last admin has only admin role as option
$user = $app->user('admin@getkirby.com');
$roles = $user->roles('change')->values(fn ($role) => $role->id());
$this->assertSame(['admin'], $roles);
}

/**