private static boolean groupNameValidForProject(ComponentDto project, String groupName) { return !project.isPrivate() || !isAnyone(groupName); }
/** * Naive implementation, to be overridden if needed */ protected List<ComponentDto> doKeepAuthorizedComponents(String permission, Collection<ComponentDto> components) { boolean allowPublicComponent = PUBLIC_PERMISSIONS.contains(permission); return components.stream() .filter(c -> (allowPublicComponent && !c.isPrivate()) || hasComponentPermission(permission, c)) .collect(MoreCollectors.toList()); }
public ProjectId(ComponentDto project) { this.id = requireNonNull(project.getId()); this.uuid = requireNonNull(project.uuid()); this.isPrivate = project.isPrivate(); }
private static boolean isRootPrivate(Component root, Map<String, ComponentDto> existingDtosByUuids) { ComponentDto rootDto = existingDtosByUuids.get(root.getUuid()); if (rootDto == null) { if (Component.Type.VIEW == root.getType()) { return false; } throw new IllegalStateException(String.format("The project '%s' is not stored in the database, during a project analysis.", root.getDbKey())); } return rootDto.isPrivate(); }
private void checkComponentType(DbSession dbSession, ComponentDto project) { Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbSession, project.uuid()); if (project.isPrivate()) { throw generateInvalidProjectExcpetion(); } if (branch.isPresent() && !branch.get().getBranchType().equals(LONG)) { throw generateInvalidProjectExcpetion(); } if (!project.qualifier().equals(PROJECT) && !project.qualifier().equals(APP)) { throw generateInvalidProjectExcpetion(); } }
@SafeVarargs private final ComponentDto insertComponentImpl(ComponentDto component, @Nullable Boolean isPrivate, Consumer<ComponentDto>... dtoPopulators) { Arrays.stream(dtoPopulators) .forEach(dtoPopulator -> dtoPopulator.accept(component)); checkState(isPrivate == null || component.isPrivate() == isPrivate, "Illegal modification of private flag"); dbClient.componentDao().insert(dbSession, component); db.commit(); return component; }
public T addProjectPermission(String permission, ComponentDto... components) { Arrays.stream(components).forEach(component -> { checkArgument( component.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission), "public component %s can't be granted public permission %s", component.uuid(), permission); }); registerComponents(components); this.projectPermissions.add(permission); Arrays.stream(components) .forEach(component -> this.projectUuidByPermission.put(permission, component.projectUuid())); return clazz.cast(this); }
@SafeVarargs public final ComponentDto insertSubView(ComponentDto view, Consumer<ComponentDto>... dtoPopulators) { return insertComponentImpl(newSubView(view), view.isPrivate(), dtoPopulators); }
private static List<Component> writeProjects(List<ComponentDto> projects, Set<String> desiredFields) { return projects.stream().map(project -> { Component.Builder compBuilder = Component.newBuilder().setUuid(project.uuid()); writeIfNeeded("key", project.getDbKey(), compBuilder::setKey, desiredFields); writeIfNeeded("name", project.name(), compBuilder::setName, desiredFields); writeIfNeeded("creationDate", project.getCreatedAt(), compBuilder::setCreationDate, desiredFields); writeIfNeeded("visibility", project.isPrivate() ? PRIVATE.getLabel() : PUBLIC.getLabel(), compBuilder::setVisibility, desiredFields); return compBuilder.build(); }).collect(toList()); }
private boolean isPrivateInDb(ComponentDto project) { return dbClient.componentDao().selectByUuid(dbTester.getSession(), project.uuid()).get().isPrivate(); }
/** * Grant permission on given project */ public UserPermissionDto insertProjectPermissionOnUser(UserDto user, String permission, ComponentDto project) { checkArgument(project.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission), "%s can't be granted on a public project", permission); checkArgument(project.getMainBranchProjectUuid() == null, "Permissions can't be granted on branches"); UserPermissionDto dto = new UserPermissionDto(project.getOrganizationUuid(), permission, user.getId(), project.getId()); db.getDbClient().userPermissionDao().insert(db.getSession(), dto); db.commit(); return dto; }
@Test public void execute_updates_permission_of_specified_project_in_indexes_when_changing_visibility() { ComponentDto project = randomPublicOrPrivateProject(); boolean initiallyPrivate = project.isPrivate(); userSessionRule.addProjectPermission(UserRole.ADMIN, project); request.setParam(PARAM_PROJECT, project.getDbKey()) .setParam(PARAM_VISIBILITY, initiallyPrivate ? PUBLIC : PRIVATE) .execute(); assertThat(projectIndexers.hasBeenCalled(project.uuid(), ProjectIndexer.Cause.PERMISSION_CHANGE)).isTrue(); }
@Test public void execute_does_not_update_permission_of_specified_project_in_indexes_if_already_has_specified_visibility() { ComponentDto project = randomPublicOrPrivateProject(); boolean initiallyPrivate = project.isPrivate(); userSessionRule.addProjectPermission(UserRole.ADMIN, project); request.setParam(PARAM_PROJECT, project.getDbKey()) .setParam(PARAM_VISIBILITY, initiallyPrivate ? PRIVATE : PUBLIC) .execute(); assertThat(projectIndexers.hasBeenCalled(project.uuid())).isFalse(); }
private void addPermissions(OrganizationDto organization, ComponentDto root) { if (!root.isPrivate()) { dbTester.users().insertProjectPermissionOnAnyone("foo1", root); dbTester.users().insertPermissionOnAnyone(organization, "not project level"); } GroupDto group = dbTester.users().insertGroup(organization); dbTester.users().insertProjectPermissionOnGroup(group, "bar", root); dbTester.users().insertPermissionOnGroup(group, "not project level"); UserDto user = dbTester.users().insertUser(); dbTester.users().insertProjectPermissionOnUser(user, "doh", root); dbTester.users().insertPermissionOnUser(user, OrganizationPermission.SCAN); assertThat(dbTester.countRowsOfTable("group_roles")).isEqualTo(root.isPrivate() ? 2 : 4); assertThat(dbTester.countRowsOfTable("user_roles")).isEqualTo(2); }
private static CreateWsResponse toCreateResponse(ComponentDto componentDto) { return CreateWsResponse.newBuilder() .setProject(CreateWsResponse.Project.newBuilder() .setKey(componentDto.getDbKey()) .setName(componentDto.name()) .setQualifier(componentDto.qualifier()) .setVisibility(Visibility.getLabel(componentDto.isPrivate()))) .build(); }
public GroupPermissionDto insertProjectPermissionOnGroup(GroupDto group, String permission, ComponentDto project) { checkArgument(group.getOrganizationUuid().equals(project.getOrganizationUuid()), "Different organizations"); checkArgument(project.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission), "%s can't be granted on a public project", permission); checkArgument(project.getMainBranchProjectUuid() == null, "Permissions can't be granted on branches"); GroupPermissionDto dto = new GroupPermissionDto() .setOrganizationUuid(group.getOrganizationUuid()) .setGroupId(group.getId()) .setRole(permission) .setResourceId(project.getId()); db.getDbClient().groupPermissionDao().insert(db.getSession(), dto); db.commit(); return dto; }
@Test public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() { ComponentDto project = prepareProject(p -> p.setPrivate(true)); ComponentDto module = newModuleDto(project).setPrivate(false); db.components().insertComponent(module); ComponentDto dir = db.components().insertComponent(newDirectory(module, "DEFG", "Directory").setDbKey("DIR").setPrivate(true)); treeRootHolder.setRoot(createSampleProjectComponentTree(project)); underTest.execute(new TestComputationStepContext()); Stream.of(project.uuid(), module.uuid(), dir.uuid()) .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(db.getSession(), uuid).get().isPrivate()) .describedAs("for uuid " + uuid) .isEqualTo(true)); }
private static void writeProjects(JsonWriter json, List<ComponentDto> projects, Set<String> fieldsToReturn) { json.name("projects"); json.beginArray(); for (ComponentDto project : projects) { json.beginObject(); json.prop("uuid", project.uuid()); writeIfWished(json, "key", project.getDbKey(), fieldsToReturn); writeIfWished(json, "name", project.name(), fieldsToReturn); writeIfWished(json, "creationDate", project.getCreatedAt(), fieldsToReturn); writeIfWished(json, "visibility", project.isPrivate() ? PRIVATE.getLabel() : PUBLIC.getLabel(), fieldsToReturn); json.endObject(); } json.endArray(); }
@Test public void persist_private_flag_true_when_creating_project() { OrganizationDto organization = db.organizations().insert(); NewComponent project = NewComponent.newComponentBuilder() .setKey(DEFAULT_PROJECT_KEY) .setName(DEFAULT_PROJECT_NAME) .setOrganizationUuid(organization.getUuid()) .setPrivate(true) .build(); ComponentDto returned = underTest.create(db.getSession(), project, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate()); }
@Test public void persist_private_flag_false_when_creating_project() { OrganizationDto organization = db.organizations().insert(); NewComponent project = NewComponent.newComponentBuilder() .setKey(DEFAULT_PROJECT_KEY) .setName(DEFAULT_PROJECT_NAME) .setOrganizationUuid(organization.getUuid()) .setPrivate(false) .build(); ComponentDto returned = underTest.create(db.getSession(), project, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate()); }