create(['name' => $name]); foreach ($global as $ability) { $role->grants()->create(['ability' => $ability]); } foreach ($objects as [$ability, $type, $id]) { $role->grants()->create(['ability' => $ability, 'resource_type' => $type, 'resource_id' => $id]); } return $role; } public function test_administration_role_can_do_everything(): void { $admin = User::factory()->admin()->create(); $this->assertTrue($admin->isAdministrator()); $this->assertTrue($admin->can('users.manage')); $this->assertTrue($admin->can('news.post.create', ['news_category', 999])); $this->assertTrue($admin->canAccessAdmin()); } public function test_roles_are_additive(): void { $user = User::factory()->withRoles([ $this->role('Redaktion', ['appearance.manage']), $this->role('Personal', ['users.manage']), ])->create(); $this->assertTrue($user->can('appearance.manage')); $this->assertTrue($user->can('users.manage')); $this->assertFalse($user->can('plugins.manage')); } public function test_users_without_roles_have_no_special_rights(): void { $user = User::factory()->create(); $this->assertFalse($user->canAccessAdmin()); $this->assertFalse($user->can('users.manage')); $this->get('/admin')->assertRedirect('/login'); $this->actingAs($user)->get('/admin')->assertForbidden(); } public function test_deactivated_users_lose_all_rights(): void { $user = User::factory()->admin()->inactive()->create(); $this->assertFalse($user->can('users.manage')); $this->assertFalse($user->canAccessAdmin()); } public function test_object_grants_only_apply_to_their_objects(): void { $user = User::factory()->withRoles([$this->role('MAV', objects: [['news.post.create', 'news_category', 7]])])->create(); $this->assertTrue($user->can('news.post.create', ['news_category', 7])); $this->assertFalse($user->can('news.post.create', ['news_category', 8])); $this->assertSame([7], $user->permissions()->ids('news.post.create', 'news_category')); $this->assertTrue($user->canAccessAdmin()); } public function test_grant_for_all_objects_of_a_type(): void { $user = User::factory()->withRoles([$this->role('Chefredaktion', objects: [['news.post.publish', 'news_category', null]])])->create(); $this->assertTrue($user->can('news.post.publish', ['news_category', 123])); $this->assertTrue($user->permissions()->ids('news.post.publish', 'news_category')); } public function test_permissions_are_cached_and_invalidated_on_changes(): void { $role = $this->role('Redaktion', ['appearance.manage']); $user = User::factory()->withRoles([$role])->create(); app(Permissions::class)->for($user); $this->app->forgetInstance(Permissions::class); DB::enableQueryLog(); $this->assertTrue(app(Permissions::class)->for($user)->can('appearance.manage')); $this->assertCount(0, DB::getQueryLog(), 'Zweiter Aufruf muss aus dem Cache kommen.'); $role->grants()->create(['ability' => 'users.manage']); $this->app->forgetInstance(Permissions::class); $this->assertTrue(app(Permissions::class)->for($user->fresh())->can('users.manage')); } public function test_manual_role_sync_keeps_identity_provider_roles(): void { $manual = $this->role('Manuell'); $fromGroup = $this->role('Aus Entra-Gruppe'); $user = User::factory()->create(); $user->roles()->attach($fromGroup->id, ['source' => 'idp', 'source_ref' => 'entra:gruppe-1', 'created_at' => now()]); $user->syncManualRoles([$manual->id]); $user->syncManualRoles([]); $this->assertTrue($user->hasRole($fromGroup->slug)); $this->assertFalse($user->hasRole($manual->slug)); } public function test_system_role_cannot_be_deleted_and_admins_cannot_remove_themselves(): void { $admin = User::factory()->admin()->create(); $this->actingAs($admin); $this->assertFalse($admin->can('delete', Role::administration())); Livewire::test(EditRole::class, ['record' => Role::administration()->getRouteKey()]) ->fillForm(['members' => []]) ->call('save') ->assertHasFormErrors(['members']); $this->assertTrue($admin->fresh()->isAdministrator()); } public function test_roles_can_be_created_with_global_and_object_rights_and_members(): void { $this->actingAs(User::factory()->admin()->create()); $category = NewsCategory::factory()->create(['name' => 'MAV-Info']); $member = User::factory()->create(); Livewire::test(CreateRole::class) ->fillForm([ 'name' => 'MAV', 'description' => 'Mitarbeitervertretung', 'global_abilities' => ['feedback.send'], 'object_grants' => [ ['ability' => 'news.post.create', 'resource_ids' => [(string) $category->id]], ['ability' => 'news.post.publish', 'resource_ids' => ['*']], ], 'members' => [$member->id], ]) ->call('create') ->assertHasNoFormErrors(); $role = Role::query()->where('slug', 'mav')->sole(); $this->assertSame(3, $role->grants()->count()); $this->assertTrue($member->fresh()->can('news.post.create', $category)); $this->assertTrue($member->fresh()->can('news.post.publish', ['news_category', 42])); $this->assertTrue($member->fresh()->can('feedback.send')); $this->assertNull(Grant::query()->where('ability', 'news.post.publish')->value('resource_id')); } public function test_core_admin_pages_follow_their_rights(): void { $user = User::factory()->withRoles([$this->role('Gestaltung', ['appearance.manage'])])->create(); $this->actingAs($user)->get('/admin/erscheinungsbild')->assertOk(); $this->actingAs($user)->get('/admin/schnellzugriffe')->assertOk(); foreach (['/admin/benutzer', '/admin/rollen', '/admin/plugins', '/admin/aktualisierung', '/admin/systemzustand', '/admin/rueckmeldung', '/admin/rechte-pruefen', '/admin/neuigkeiten', '/admin/termine'] as $url) { $this->actingAs($user)->get($url)->assertForbidden(); } $this->actingAs($user)->postJson('/admin/update/check')->assertForbidden(); } public function test_permission_checker_explains_where_rights_come_from(): void { $mav = $this->role('MAV', objects: [['news.post.create', 'news_category', NewsCategory::factory()->create(['name' => 'MAV-Info'])->id]]); $person = User::factory()->withRoles([$mav])->create(['first_name' => 'Grete', 'last_name' => 'Müller']); $this->actingAs(User::factory()->admin()->create()) ->get('/admin/rechte-pruefen?person='.$person->id) ->assertOk() ->assertSee('Grete Müller') ->assertSee('MAV · manuell') ->assertSee('Beiträge schreiben') ->assertSee('Kategorie: MAV-Info'); } }