@Test public void count_users_with_one_permission_when_the_last_one_is_in_a_group() { DbClient dbClient = db.getDbClient(); UserDto user = dbClient.userDao().insert(db.getSession(), new UserDto().setActive(true)); GroupDto group = dbClient.groupDao().insert(db.getSession(), new GroupDto()); dbClient .userGroupDao() .insert( db.getSession(), new UserGroupDto().setGroupId(group.getId()).setUserId(user.getId())); dbClient .roleDao() .insertGroupRole( db.getSession(), new GroupRoleDto().setGroupId(group.getId()).setRole(GlobalPermissions.SYSTEM_ADMIN)); int resultWithoutExcludingGroup = underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null); int resultWithGroupExclusion = underTest.countUserPermissions( db.getSession(), GlobalPermissions.SYSTEM_ADMIN, group.getId()); assertThat(resultWithoutExcludingGroup).isEqualTo(1); assertThat(resultWithGroupExclusion).isEqualTo(0); }
@Test public void count_user_twice_when_user_and_group_permission() { DbClient dbClient = db.getDbClient(); UserDto user = dbClient.userDao().insert(db.getSession(), new UserDto().setActive(true)); GroupDto group = dbClient.groupDao().insert(db.getSession(), new GroupDto()); dbClient .userGroupDao() .insert( db.getSession(), new UserGroupDto().setGroupId(group.getId()).setUserId(user.getId())); dbClient .roleDao() .insertGroupRole( db.getSession(), new GroupRoleDto().setGroupId(group.getId()).setRole(GlobalPermissions.SYSTEM_ADMIN)); dbClient .roleDao() .insertUserRole( db.getSession(), new UserRoleDto().setUserId(user.getId()).setRole(GlobalPermissions.SYSTEM_ADMIN)); int result = underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null); assertThat(result).isEqualTo(2); }
@Test public void count_users_with_one_specific_permission() { DbClient dbClient = db.getDbClient(); UserDto user = dbClient.userDao().insert(db.getSession(), new UserDto().setActive(true)); dbClient .roleDao() .insertUserRole( db.getSession(), new UserRoleDto() .setUserId(user.getId()) .setResourceId(123L) .setRole(GlobalPermissions.SYSTEM_ADMIN)); dbClient .roleDao() .insertUserRole( db.getSession(), new UserRoleDto().setUserId(user.getId()).setRole(GlobalPermissions.SYSTEM_ADMIN)); dbClient .roleDao() .insertUserRole( db.getSession(), new UserRoleDto().setUserId(user.getId()).setRole(GlobalPermissions.SCAN_EXECUTION)); int result = underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null); assertThat(result).isEqualTo(1); }