permissions refactoring

This commit is contained in:
2026-04-12 19:21:04 +02:00
parent 51014395a2
commit 9dcdfb981a
64 changed files with 1842 additions and 656 deletions
@@ -27,10 +27,10 @@ class UserManagementControllerTest extends TestCase
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('admin/users/Index')
->where('users.0.email', $admin->email)
->where('users.1.email', $member->email)
->where('users.0.is_admin', true)
->where('users.1.is_admin', false),
->has('users', 2)
->where("directPermissions.{$admin->id}.0", 'global:admin')
->where('permissionOptions.0.value', 'global:admin')
->where('permissionOptions.1.value', 'project:create'),
);
}
@@ -47,9 +47,9 @@ class UserManagementControllerTest extends TestCase
}
/**
* Ensure admins can update another user's admin access.
* Ensure admins can update another user's global permissions.
*/
public function test_admin_can_update_a_users_admin_access(): void
public function test_admin_can_update_a_users_global_permissions(): void
{
$admin = User::factory()->admin()->create();
$member = User::factory()->create([
@@ -58,30 +58,72 @@ class UserManagementControllerTest extends TestCase
$this->actingAs($admin)
->patch(route('admin.users.update', $member), [
'is_admin' => true,
'permissions' => ['global:admin', 'project:create'],
])
->assertRedirect(route('admin.users.index'))
->assertSessionHas('status', 'Regular User now has admin access.');
->assertSessionHas('status', 'Regular User global permissions were updated.');
$this->assertTrue($member->refresh()->is_admin);
$this->assertDatabaseHas('permission_assignments', [
'assignee_id' => $member->id,
'assignee_type' => $member->getMorphClass(),
'permission' => 'global:admin',
'scope_id' => null,
'scope_type' => null,
]);
$this->assertDatabaseHas('permission_assignments', [
'assignee_id' => $member->id,
'assignee_type' => $member->getMorphClass(),
'permission' => 'project:create',
'scope_id' => null,
'scope_type' => null,
]);
}
/**
* Ensure non-admin users cannot update admin access.
* Ensure admins can also clear a user's global permissions.
*/
public function test_non_admin_cannot_update_a_users_admin_access(): void
public function test_admin_can_clear_a_users_global_permissions(): void
{
$admin = User::factory()->admin()->create();
$member = User::factory()->create();
$this->syncGlobalPermissions($member, ['global:admin', 'project:create']);
$this->actingAs($admin)
->patch(route('admin.users.update', $member), [
'permissions' => [],
])
->assertRedirect(route('admin.users.index'))
->assertSessionHas('status', "{$member->name} global permissions were updated.");
$this->assertDatabaseMissing('permission_assignments', [
'assignee_id' => $member->id,
'assignee_type' => $member->getMorphClass(),
'scope_id' => null,
'scope_type' => null,
]);
}
/**
* Ensure non-admin users cannot update global permissions.
*/
public function test_non_admin_cannot_update_a_users_global_permissions(): void
{
$member = User::factory()->create();
$otherUser = User::factory()->create([
'is_admin' => false,
]);
$otherUser = User::factory()->create();
$this->actingAs($member)
->patch(route('admin.users.update', $otherUser), [
'is_admin' => true,
'permissions' => ['global:admin'],
])
->assertForbidden();
$this->assertFalse($otherUser->refresh()->is_admin);
$this->assertDatabaseMissing('permission_assignments', [
'assignee_id' => $otherUser->id,
'assignee_type' => $otherUser->getMorphClass(),
'permission' => 'global:admin',
'scope_id' => null,
'scope_type' => null,
]);
}
}