private Map<Long, ComponentDto> searchProjects(DbSession dbSession, List<PropertyDto> properties) { Set<Long> componentIds = properties.stream() .map(PropertyDto::getResourceId) .filter(Objects::nonNull) .collect(MoreCollectors.toSet(properties.size())); Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, componentIds, userSession.getUserId(), UserRole.USER); return dbClient.componentDao().selectByIds(dbSession, componentIds) .stream() .filter(c -> authorizedProjectIds.contains(c.getId())) .collect(MoreCollectors.uniqueIndex(ComponentDto::getId)); }
private static void verifyFileMoveRowDto(RecordingResultHandler resultHander, ComponentAndSource componentAndSource) { FileMoveRowDto dto = resultHander.getById(componentAndSource.component.getId()).get(); assertThat(dto.getKey()).isEqualTo(componentAndSource.component.getDbKey()); assertThat(dto.getUuid()).isEqualTo(componentAndSource.component.uuid()); assertThat(dto.getPath()).isEqualTo(componentAndSource.component.path()); assertThat(dto.getLineCount()).isEqualTo(componentAndSource.source.getLineCount()); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_empty_for_any_users_and_any_permission_on_private_project_without_any_permission_in_DB() { ComponentDto project = db.components().insertPrivateProject(organization); PermissionsTestHelper.ALL_PERMISSIONS .forEach(perm -> { assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, perm, project.getId())) .isEmpty(); }); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, randomPermission, project.getId())) .isEmpty(); }
public void assertExists(String channel, String dispatcher, int userId, @Nullable ComponentDto component) { List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel)) .setComponentId(component == null ? null : component.getId()) .setUserId(userId) .build(), dbSession).stream() .filter(prop -> component == null ? prop.getResourceId() == null : prop.getResourceId() != null) .collect(MoreCollectors.toList()); assertThat(result).hasSize(1); assertThat(result.get(0).getValue()).isEqualTo("true"); }
@Test @UseDataProvider("allValuesForSelect") public void selectProjectProperties_supports_all_values(String dbValue, String expected) { ComponentDto projectDto = insertPrivateProject("A"); insertProperty("project.one", dbValue, projectDto.getId(), null); List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.getDbKey()); assertThat(dtos).hasSize(1); assertThatDto(dtos.iterator().next()) .hasKey("project.one") .hasResourceId(projectDto.getId()) .hasValue(expected); }
@Test public void deleteByRootComponentIdAndGroupId_has_no_effect_if_group_does_not_exist() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = randomPublicOrPrivateProject(organization); assertThat(underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), 5678)).isEqualTo(0); }
private void insertProjectPermissionOnGroup(DbSession dbSession, ComponentDto component, String permission, Integer groupId) { dbClient.groupPermissionDao().insert(dbSession, new GroupPermissionDto() .setOrganizationUuid(component.getOrganizationUuid()) .setResourceId(component.getId()) .setGroupId(groupId) .setRole(permission)); }
@Test public void get_by_id_on_disabled_component() { ComponentDto enabledProject = db.components().insertPrivateProject(); ComponentDto disabledProject = db.components().insertPrivateProject(p -> p.setEnabled(false)); Optional<ComponentDto> result = underTest.selectById(dbSession, disabledProject.getId()); assertThat(result).isPresent(); assertThat(result.get().isEnabled()).isFalse(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_any_users_for_public_project_without_any_permission_in_DB_and_permission_USER() { ComponentDto project = db.components().insertPublicProject(organization); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, UserRole.USER, project.getId())) .containsAll(randomExistingUserIds); }
public boolean hasNoFavorite(ComponentDto componentDto) { List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setComponentId(componentDto.getId()) .build(), dbSession); return result.isEmpty(); } }
@Test public void deletePermissions_deletes_permissions_of_public_project() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPublicProject(organization); addPermissions(organization, project); PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler); purgeCommands.deletePermissions(project.getId()); assertThat(dbTester.countRowsOfTable("group_roles")).isEqualTo(2); assertThat(dbTester.countRowsOfTable("user_roles")).isEqualTo(1); }
@Test public void load_component_id_from_db() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organizationDto); ComponentDto file = db.components().insertComponent(newFileDto(project)); long componentId = underTest.component(db.getSession(), new DefaultIssue().setComponentUuid(file.uuid())).getId(); assertThat(componentId).isEqualTo(file.getId()); }
public boolean hasFavorite(ComponentDto componentDto, int userId) { List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setComponentId(componentDto.getId()) .setUserId(userId) .build(), dbSession); return !result.isEmpty(); }
@Test public void deleteProjectPermissionOfAnyUser_has_no_effect_if_specified_component_has_no_permission_at_all() { OrganizationDto organization = db.organizations().insert(); UserDto user = insertUser(organization); db.users().insertPermissionOnUser(organization, user, SCAN); ComponentDto project = randomPublicOrPrivateProject(organization); int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.getId(), SCAN.getKey()); assertThat(deletedCount).isEqualTo(0); assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); }
private void unsafeInsertProjectPermissionOnUser(ComponentDto component, UserDto user, String permission) { UserPermissionDto dto = new UserPermissionDto(component.getOrganizationUuid(), permission, user.getId(), component.getId()); dbTester.getDbClient().userPermissionDao().insert(dbTester.getSession(), dto); dbTester.commit(); }
@Test public void deactivate_user_deletes_his_properties() { logInAsSystemAdministrator(); UserDto user = db.users().insertUser(); ComponentDto project = db.components().insertPrivateProject(); db.properties().insertProperty(newUserPropertyDto(user)); db.properties().insertProperty(newUserPropertyDto(user)); db.properties().insertProperty(newUserPropertyDto(user).setResourceId(project.getId())); deactivate(user.getLogin()); assertThat(db.getDbClient().propertiesDao().selectByQuery(PropertyQuery.builder().setUserId(user.getId()).build(), dbSession)).isEmpty(); assertThat(db.getDbClient().propertiesDao().selectByQuery(PropertyQuery.builder().setUserId(user.getId()).setComponentId(project.getId()).build(), dbSession)).isEmpty(); }
public static ComponentDto newProjectCopy(String uuid, ComponentDto project, ComponentDto view) { checkNotNull(project.getId(), "The project need to be persisted before creating this technical project."); return newChildComponent(uuid, view, view) .setDbKey(view.getDbKey() + project.getDbKey()) .setName(project.name()) .setLongName(project.longName()) .setCopyComponentUuid(project.uuid()) .setScope(Scopes.FILE) .setQualifier(Qualifiers.PROJECT) .setPath(null) .setLanguage(null); }
private void unsafeInsertProjectPermissionOnAnyone(String perm, ComponentDto project) { GroupPermissionDto dto = new GroupPermissionDto() .setOrganizationUuid(project.getOrganizationUuid()) .setGroupId(null) .setRole(perm) .setResourceId(project.getId()); db.getDbClient().groupPermissionDao().insert(db.getSession(), dto); db.commit(); } }
private void grantPermissionToAnyone(ComponentDto project, String permission) { dbClient.groupPermissionDao().insert(session, new GroupPermissionDto() .setOrganizationUuid(project.getOrganizationUuid()) .setGroupId(null) .setResourceId(project.getId()) .setRole(permission)); session.commit(); userSession.logIn().addProjectPermission(permission, project); }