Optional<ComponentDto> opt = dbClient.componentDao().selectByKey(dbSession, effectiveProjectKey);
ensureOrganizationIsConsistent(opt, organizationDto);
ComponentDto project = opt.or(() -> createProject(dbSession, organizationDto.getUuid(), projectKey, projectBranch, projectName));
- userSession.checkComponentPermission(SCAN_EXECUTION, project);
+ checkScanPermission(project);
return submitReport(dbSession, reportInput, project);
}
}
+ private void checkScanPermission(ComponentDto project) {
+ // this is a specific and inconsistent behavior. For legacy reasons, "technical users"
+ // defined on an organization should be able to analyze a project even if
+ // they don't have the direct permission on the project.
+ // That means that dropping the permission on the project does not have any effects
+ // if user has still the permission on the organization
+ if (!userSession.hasComponentPermission(SCAN_EXECUTION, project) &&
+ !userSession.hasOrganizationPermission(project.getOrganizationUuid(), SCAN_EXECUTION)) {
+ throw insufficientPrivilegesException();
+ }
+ }
+
private OrganizationDto getOrganizationDtoOrFail(DbSession dbSession, String organizationKey) {
return dbClient.organizationDao().selectByKey(dbSession, organizationKey)
.orElseThrow(() -> new NotFoundException(format("Organization with key '%s' does not exist", organizationKey)));
import org.sonar.ce.queue.CeQueue;
import org.sonar.ce.queue.CeQueueImpl;
import org.sonar.ce.queue.CeTaskSubmit;
-import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.ce.CeTaskTypes;
}
@Test
- public void submit_a_report_on_existing_project_with_scan_permission_on_organization() {
- userSession.setGlobalPermissions(SCAN_EXECUTION);
-
- ComponentDto project = db.components().insertProject(db.getDefaultOrganization());
+ public void user_with_scan_permission_on_organization_is_allowed_to_submit_a_report_on_existing_project() {
+ OrganizationDto org = db.organizations().insert();
+ ComponentDto project = db.components().insertProject(org);
+ userSession.addOrganizationPermission(org, SCAN_EXECUTION);
mockSuccessfulPrepareSubmitCall();
- underTest.submit(defaultOrganizationKey, project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
+ underTest.submit(org.getKey(), project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
verify(queue).submit(any(CeTaskSubmit.class));
}
@Test
public void fail_with_forbidden_exception_when_no_scan_permission() {
- userSession.setGlobalPermissions(GlobalPermissions.QUALITY_GATE_ADMIN);
-
thrown.expect(ForbiddenException.class);
+
underTest.submit(defaultOrganizationKey, PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
}