Skip to content

Commit 0123d83

Browse files
committed
Fixed not being able to remove all user roles
User roles would only be actioned if they existed in the form request, hence removal of all roles would have no data to action upon. This adds a placeholder 0-id role to ensure there is always role data to send, even when no roles are selected. This field value is latter filtered out. Added test to cover. Likely related to #3922.
1 parent 559e392 commit 0123d83

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

app/Auth/UserRepo.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ protected function isOnlyAdmin(User $user): bool
234234
*/
235235
protected function setUserRoles(User $user, array $roles)
236236
{
237+
$roles = array_filter(array_values($roles));
238+
237239
if ($this->demotingLastAdmin($user, $roles)) {
238240
throw new UserUpdateException(trans('errors.role_cannot_remove_only_admin'), $user->getEditUrl());
239241
}

resources/views/form/role-checkboxes.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
<div class="toggle-switch-list dual-column-content">
3+
<input type="hidden" name="{{ $name }}[0]" value="0">
34
@foreach($roles as $role)
45
<div>
56
@include('form.custom-checkbox', [

tests/User/UserManagementTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,34 @@ public function test_user_create_update_fails_if_locale_is_invalid()
274274
$resp->assertSessionHasErrors(['language' => 'The language may not be greater than 15 characters.']);
275275
$resp->assertSessionHasErrors(['language' => 'The language may only contain letters, numbers, dashes and underscores.']);
276276
}
277+
278+
public function test_role_removal_on_user_edit_removes_all_role_assignments()
279+
{
280+
$user = $this->getEditor();
281+
282+
$this->assertEquals(1, $user->roles()->count());
283+
284+
// A roles[0] hidden fields is used to indicate the existence of role selection in the submission
285+
// of the user edit form. We check that field is used and emulate its submission.
286+
$resp = $this->asAdmin()->get("/settings/users/{$user->id}");
287+
$this->withHtml($resp)->assertElementExists('input[type="hidden"][name="roles[0]"][value="0"]');
288+
289+
$resp = $this->asAdmin()->put("/settings/users/{$user->id}", [
290+
'name' => $user->name,
291+
'email' => $user->email,
292+
'roles' => ['0' => '0'],
293+
]);
294+
$resp->assertRedirect("/settings/users");
295+
296+
$this->assertEquals(0, $user->roles()->count());
297+
}
298+
299+
public function test_role_form_hidden_indicator_field_does_not_exist_where_roles_cannot_be_managed()
300+
{
301+
$user = $this->getEditor();
302+
$resp = $this->actingAs($user)->get("/settings/users/{$user->id}");
303+
$html = $this->withHtml($resp);
304+
$html->assertElementExists('input[name="email"]');
305+
$html->assertElementNotExists('input[type="hidden"][name="roles[0]"]');
306+
}
277307
}

0 commit comments

Comments
 (0)