}
}
+ /**
+ * @deprecated replaced by {@link #applyDefault(DbSession, ComponentDto, Long)}, which <b>does not
+ * verify that user is authorized to administrate the component</b>.
+ */
+ @Deprecated
public void applyDefaultPermissionTemplate(DbSession session, String componentKey) {
ComponentDto component = componentFinder.getByKey(session, componentKey);
ResourceDto provisioned = dbClient.resourceDao().selectProvisionedProject(session, componentKey);
indexProjectPermissions(dbSession, projects.stream().map(ComponentDto::uuid).collect(Collectors.toList()));
}
+ /**
+ * Apply the default permission template to component, whatever it already exists (and has permissions) or if it's
+ * provisioned (and has no permissions yet).
+ *
+ * @param dbSession
+ * @param component
+ * @param projectCreatorUserId id of the user who creates the project, only if project is provisioned. He will
+ * benefit from the permissions defined in the template for "project creator".
+ */
+ public void applyDefault(DbSession dbSession, ComponentDto component, @Nullable Long projectCreatorUserId) {
+ permissionRepository.applyDefaultPermissionTemplate(dbSession, component, projectCreatorUserId);
+ dbSession.commit();
+ indexProjectPermissions(dbSession, asList(component.uuid()));
+ }
+
private void indexProjectPermissions(DbSession dbSession, List<String> projectUuids) {
permissionIndexer.index(dbSession, projectUuids);
}
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ public DbTester db = DbTester.create(System2.INSTANCE);
private CeQueue queue = mock(CeQueueImpl.class);
private ComponentService componentService = mock(ComponentService.class);
private PermissionService permissionService = mock(PermissionService.class);
- private ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentService, permissionService, dbTester.getDbClient());
+ private ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentService, permissionService, db.getDbClient());
@Test
public void submit_a_report_on_existing_project() {
userSession.setGlobalPermissions(SCAN_EXECUTION);
- ComponentDto project = dbTester.components().insertProject();
+ ComponentDto project = db.components().insertProject();
+
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
underTest.submit(project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
userSession.setGlobalPermissions(SCAN_EXECUTION, PROVISIONING);
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
- when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
+ ComponentDto createdProject = new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY);
+ when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(createdProject);
when(permissionService.wouldCurrentUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
.thenReturn(true);
underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
verifyReportIsPersisted(TASK_UUID);
- verify(permissionService).applyDefaultPermissionTemplate(any(DbSession.class), eq(PROJECT_KEY));
+ verify(permissionService).applyDefault(any(DbSession.class), eq(createdProject), anyLong());
verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
@Override
protected boolean matchesSafely(CeTaskSubmit submit) {
@Test
public void submit_a_report_on_existing_project_with_global_scan_permission() {
- ComponentDto project = dbTester.components().insertProject();
+ ComponentDto project = db.components().insertProject();
userSession.setGlobalPermissions(SCAN_EXECUTION);
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
@Test
public void submit_a_report_on_existing_project_with_project_scan_permission() {
- ComponentDto project = dbTester.components().insertProject();
+ ComponentDto project = db.components().insertProject();
userSession.addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
}
private void verifyReportIsPersisted(String taskUuid) {
- assertThat(dbTester.selectFirst("select task_uuid from ce_task_input where task_uuid='" + taskUuid + "'")).isNotNull();
+ assertThat(db.selectFirst("select task_uuid from ce_task_input where task_uuid='" + taskUuid + "'")).isNotNull();
}
}