import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentService;
import org.sonar.server.component.NewComponent;
+import org.sonar.server.permission.PermissionService;
import org.sonar.server.user.UserSession;
@ServerSide
private final UserSession userSession;
private final ReportFiles reportFiles;
private final ComponentService componentService;
+ private final PermissionService permissionService;
public ReportSubmitter(CeQueue queue, UserSession userSession, ReportFiles reportFiles,
- ComponentService componentService) {
+ ComponentService componentService, PermissionService permissionService) {
this.queue = queue;
this.userSession = userSession;
this.reportFiles = reportFiles;
this.componentService = componentService;
+ this.permissionService = permissionService;
}
public CeTask submit(String projectKey, @Nullable String projectBranch, @Nullable String projectName, InputStream reportInput) {
newProject.setQualifier(Qualifiers.PROJECT);
// no need to verify the permission "provisioning" as it's already handled by componentService
project = componentService.create(newProject);
+ permissionService.applyDefaultPermissionTemplate(project.getKey());
}
// the report file must be saved before submitting the task
}
}
+ /**
+ * Important - this method checks caller permissions
+ */
public void applyDefaultPermissionTemplate(final String componentKey) {
- userSession.checkLoggedIn();
-
DbSession session = dbClient.openSession(false);
try {
ComponentDto component = componentFinder.getByKey(session, componentKey);
applyPermissionTemplate(query);
}
+ /**
+ * Important - this method checks caller permissions
+ */
public void applyPermissionTemplate(ApplyPermissionTemplateQuery query) {
DbSession dbSession = dbClient.openSession(false);
try {
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentService;
import org.sonar.server.component.NewComponent;
+import org.sonar.server.permission.PermissionService;
import org.sonar.server.tester.UserSessionRule;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
public class ReportSubmitterTest {
CeQueue queue = mock(CeQueueImpl.class);
ReportFiles reportFiles = mock(ReportFiles.class);
ComponentService componentService = mock(ComponentService.class);
- ReportSubmitter underTest = new ReportSubmitter(queue, userSession, reportFiles, componentService);
+ PermissionService permissionService = mock(PermissionService.class);
+ ReportSubmitter underTest = new ReportSubmitter(queue, userSession, reportFiles, componentService, permissionService);
@Test
public void submit_a_report_on_existing_project() {
underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
+ verifyZeroInteractions(permissionService);
verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
@Override
protected boolean matchesSafely(CeTaskSubmit submit) {
when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder("TASK_1"));
userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.PROVISIONING);
when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(null);
- when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid("P1"));
+ when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid("P1").setKey("MY_PROJECT"));
underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
+ verify(permissionService).applyDefaultPermissionTemplate("MY_PROJECT");
verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
@Override
protected boolean matchesSafely(CeTaskSubmit submit) {