import static com.google.common.base.Preconditions.checkState;
import static java.util.Arrays.asList;
+import static org.sonar.db.component.ComponentTesting.newApplication;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.db.component.ComponentTesting.newPublicProjectDto;
import static org.sonar.db.component.ComponentTesting.newView;
return insertComponentImpl(newView(organizationDto, uuid), false, noExtraConfiguration());
}
+ public ComponentDto insertApplication(OrganizationDto organizationDto) {
+ return insertComponentImpl(newApplication(organizationDto), false, noExtraConfiguration());
+ }
+
+ public ComponentDto insertApplication(OrganizationDto organizationDto, Consumer<ComponentDto> dtoPopulator) {
+ return insertComponentImpl(newApplication(organizationDto), false, dtoPopulator);
+ }
+
private static <T> Consumer<T> noExtraConfiguration() {
return (t) -> {
};
.setQualifier(Qualifiers.VIEW);
}
+ public static ComponentDto newApplication(OrganizationDto organizationDto) {
+ return newView(organizationDto.getUuid(), Uuids.createFast())
+ .setQualifier(Qualifiers.APP);
+ }
+
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)
String qualifier = component.qualifier();
DefaultTemplatesResolverImpl.ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(defaultTemplates);
- if (Qualifiers.PROJECT.equals(qualifier)) {
- return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
- } else if (Qualifiers.VIEW.equals(qualifier)) {
- String viewDefaultTemplateUuid = resolvedDefaultTemplates.getView().orElseThrow(
- () -> new IllegalStateException("Attempt to create a view when Governance plugin is not installed"));
- return dbClient.permissionTemplateDao().selectByUuid(dbSession, viewDefaultTemplateUuid);
- } else {
- throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
+ switch (qualifier) {
+ case Qualifiers.PROJECT:
+ return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
+ case Qualifiers.VIEW:
+ case Qualifiers.APP:
+ String viewDefaultTemplateUuid = resolvedDefaultTemplates.getView().orElseThrow(
+ () -> new IllegalStateException("Attempt to create a view when Governance plugin is not installed"));
+ return dbClient.permissionTemplateDao().selectByUuid(dbSession, viewDefaultTemplateUuid);
+ default:
+ throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
}
}
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.sonar.api.resources.Qualifiers.APP;
import static org.sonar.api.resources.Qualifiers.VIEW;
public class ComponentUpdaterTest {
projectIndexers);
@Test
- public void should_persist_and_index_when_creating_project() throws Exception {
+ public void persist_and_index_when_creating_project() throws Exception {
NewComponent project = NewComponent.newComponentBuilder()
.setKey(DEFAULT_PROJECT_KEY)
.setName(DEFAULT_PROJECT_NAME)
}
@Test
- public void should_persist_private_flag_true_when_creating_project() throws Exception {
+ public void persist_private_flag_true_when_creating_project() throws Exception {
OrganizationDto organization = db.organizations().insert();
NewComponent project = NewComponent.newComponentBuilder()
.setKey(DEFAULT_PROJECT_KEY)
}
@Test
- public void should_persist_private_flag_false_when_creating_project() throws Exception {
+ public void persist_private_flag_false_when_creating_project() throws Exception {
OrganizationDto organization = db.organizations().insert();
NewComponent project = NewComponent.newComponentBuilder()
.setKey(DEFAULT_PROJECT_KEY)
}
@Test
- public void should_apply_default_permission_template() throws Exception {
+ public void persist_and_index_when_creating_view() {
+ NewComponent view = NewComponent.newComponentBuilder()
+ .setKey("view-key")
+ .setName("view-name")
+ .setQualifier(VIEW)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build();
+
+ ComponentDto returned = underTest.create(db.getSession(), view, null);
+
+ ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
+ assertThat(loaded.getKey()).isEqualTo("view-key");
+ assertThat(loaded.name()).isEqualTo("view-name");
+ assertThat(loaded.qualifier()).isEqualTo("VW");
+ verify(projectIndexers).hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION);
+ }
+
+ @Test
+ public void create_application() {
+ NewComponent view = NewComponent.newComponentBuilder()
+ .setKey("app-key")
+ .setName("app-name")
+ .setQualifier(APP)
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+ .build();
+
+ ComponentDto returned = underTest.create(db.getSession(), view, null);
+
+ ComponentDto loaded = db.getDbClient().componentDao().selectByKey(db.getSession(), returned.key()).get();
+ assertThat(loaded.getKey()).isEqualTo("app-key");
+ assertThat(loaded.name()).isEqualTo("app-name");
+ assertThat(loaded.qualifier()).isEqualTo("APP");
+ verify(projectIndexers).hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION);
+ }
+
+ @Test
+ public void apply_default_permission_template() throws Exception {
int userId = 42;
NewComponent project = NewComponent.newComponentBuilder()
.setKey(DEFAULT_PROJECT_KEY)
}
@Test
- public void should_add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() throws Exception {
+ public void add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() throws Exception {
UserDto userDto = db.users().insertUser();
NewComponent project = NewComponent.newComponentBuilder()
.setKey(DEFAULT_PROJECT_KEY)
.build(),
null);
}
-
- @Test
- public void persist_and_index_when_creating_view() {
- NewComponent view = NewComponent.newComponentBuilder()
- .setKey("view-key")
- .setName("view-name")
- .setQualifier(VIEW)
- .setOrganizationUuid(db.getDefaultOrganization().getUuid())
- .build();
-
- ComponentDto returned = underTest.create(db.getSession(), view, null);
-
- ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
- assertThat(loaded.getKey()).isEqualTo("view-key");
- assertThat(loaded.name()).isEqualTo("view-name");
- assertThat(loaded.qualifier()).isEqualTo("VW");
- assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue();
- }
-
}
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
+import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
+import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS;
public class PermissionTemplateServiceTest {
ComponentDto publicProject = dbTester.components().insertPublicProject(organization);
UserDto user = dbTester.users().insertUser();
ProjectPermissions.ALL
- .forEach(perm -> dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, perm));
+ .forEach(perm -> dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, perm));
dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, "p1");
dbTester.organizations().setDefaultTemplates(organization, permissionTemplate.getUuid(), null);
underTest.applyDefault(session, organization.getUuid(), publicProject, user.getId());
assertThat(selectProjectPermissionsOfUser(user, publicProject))
- .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, GlobalPermissions.SCAN_EXECUTION);
+ .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, GlobalPermissions.SCAN_EXECUTION);
}
@Test
.containsOnly("p1", UserRole.USER, UserRole.CODEVIEWER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, GlobalPermissions.SCAN_EXECUTION);
}
+ @Test
+ public void apply_template_on_view() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ ComponentDto view = dbTester.components().insertView(organization);
+ PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+ GroupDto group = dbTester.users().insertGroup(organization);
+ dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, ADMINISTER.getKey());
+ dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, PROVISION_PROJECTS.getKey());
+ dbTester.organizations().setDefaultTemplates(organization, permissionTemplate.getUuid(), null);
+
+ underTest.applyDefault(session, organization.getUuid(), view, null);
+
+ assertThat(selectProjectPermissionsOfGroup(organization, group, view))
+ .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
+ }
+
+ @Test
+ public void apply_default_template_on_view() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ ComponentDto view = dbTester.components().insertView(organization);
+ PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+ PermissionTemplateDto viewPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+ GroupDto group = dbTester.users().insertGroup(organization);
+ dbTester.permissionTemplates().addGroupToTemplate(viewPermissionTemplate, group, ADMINISTER.getKey());
+ dbTester.permissionTemplates().addGroupToTemplate(viewPermissionTemplate, group, PROVISION_PROJECTS.getKey());
+ dbTester.organizations().setDefaultTemplates(organization, projectPermissionTemplate.getUuid(), viewPermissionTemplate.getUuid());
+
+ underTest.applyDefault(session, organization.getUuid(), view, null);
+
+ assertThat(selectProjectPermissionsOfGroup(organization, group, view))
+ .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
+ }
+
+ @Test
+ public void apply_project_default_template_on_view_when_no_view_default_template() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ ComponentDto view = dbTester.components().insertView(organization);
+ PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+ GroupDto group = dbTester.users().insertGroup(organization);
+ dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey());
+ dbTester.organizations().setDefaultTemplates(organization, projectPermissionTemplate.getUuid(), null);
+
+ underTest.applyDefault(session, organization.getUuid(), view, null);
+
+ assertThat(selectProjectPermissionsOfGroup(organization, group, view)).containsOnly(PROVISION_PROJECTS.getKey());
+ }
+
+ @Test
+ public void apply_template_on_applications() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ ComponentDto application = dbTester.components().insertApplication(organization);
+ PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+ GroupDto group = dbTester.users().insertGroup(organization);
+ dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, ADMINISTER.getKey());
+ dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, PROVISION_PROJECTS.getKey());
+ dbTester.organizations().setDefaultTemplates(organization, permissionTemplate.getUuid(), null);
+
+ underTest.applyDefault(session, organization.getUuid(), application, null);
+
+ assertThat(selectProjectPermissionsOfGroup(organization, group, application))
+ .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
+ }
+
+ @Test
+ public void apply_default_view_template_on_application() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ ComponentDto application = dbTester.components().insertApplication(organization);
+ PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+ PermissionTemplateDto viewPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+ GroupDto group = dbTester.users().insertGroup(organization);
+ dbTester.permissionTemplates().addGroupToTemplate(viewPermissionTemplate, group, ADMINISTER.getKey());
+ dbTester.permissionTemplates().addGroupToTemplate(viewPermissionTemplate, group, PROVISION_PROJECTS.getKey());
+ dbTester.organizations().setDefaultTemplates(organization, projectPermissionTemplate.getUuid(), viewPermissionTemplate.getUuid());
+
+ underTest.applyDefault(session, organization.getUuid(), application, null);
+
+ assertThat(selectProjectPermissionsOfGroup(organization, group, application))
+ .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
+ }
+
+ @Test
+ public void apply_project_default_template_on_application_when_no_application_default_template() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ ComponentDto application = dbTester.components().insertApplication(organization);
+ PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+ GroupDto group = dbTester.users().insertGroup(organization);
+ dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey());
+ dbTester.organizations().setDefaultTemplates(organization, projectPermissionTemplate.getUuid(), null);
+
+ underTest.applyDefault(session, organization.getUuid(), application, null);
+
+ assertThat(selectProjectPermissionsOfGroup(organization, group, application)).containsOnly(PROVISION_PROJECTS.getKey());
+ }
+
@Test
public void apply_permission_template() {
OrganizationDto organization = dbTester.organizations().insert();
package org.sonar.server.permission.ws.template;
import org.junit.rules.ExternalResource;
-import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.ResourceType;
import org.sonar.api.resources.ResourceTypeTree;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.db.organization.DefaultTemplates;
+import static org.sonar.api.resources.Qualifiers.APP;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
+import static org.sonar.api.resources.Qualifiers.VIEW;
+
public class DefaultTemplatesResolverRule extends ExternalResource implements DefaultTemplatesResolver {
- private static final DefaultTemplatesResolver WITH_VIEWS = new DefaultTemplatesResolverImpl(
+ private static final DefaultTemplatesResolver WITH_GOV = new DefaultTemplatesResolverImpl(
new ResourceTypes(new ResourceTypeTree[] {
ResourceTypeTree.builder()
- .addType(ResourceType.builder(Qualifiers.PROJECT).build())
+ .addType(ResourceType.builder(PROJECT).build())
+ .build(),
+ ResourceTypeTree.builder()
+ .addType(ResourceType.builder(VIEW).build())
.build(),
ResourceTypeTree.builder()
- .addType(ResourceType.builder(Qualifiers.VIEW).build())
- .build()}));
- private static final DefaultTemplatesResolver WITHOUT_VIEWS = new DefaultTemplatesResolverImpl(
+ .addType(ResourceType.builder(APP).build())
+ .build()
+ }));
+ private static final DefaultTemplatesResolver WITHOUT_GOV = new DefaultTemplatesResolverImpl(
new ResourceTypes(new ResourceTypeTree[] {ResourceTypeTree.builder()
- .addType(ResourceType.builder(Qualifiers.PROJECT).build())
+ .addType(ResourceType.builder(PROJECT).build())
.build()}));
private final boolean governanceInitiallyInstalled;
@Override
public DefaultTemplatesResolverImpl.ResolvedDefaultTemplates resolve(DefaultTemplates defaultTemplates) {
if (governanceInstalled) {
- return WITH_VIEWS.resolve(defaultTemplates);
+ return WITH_GOV.resolve(defaultTemplates);
}
- return WITHOUT_VIEWS.resolve(defaultTemplates);
+ return WITHOUT_GOV.resolve(defaultTemplates);
}
}