import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.api.web.UserRole;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest;
import static com.google.common.base.Strings.isNullOrEmpty;
-import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
-import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkRequest;
MSG_ONE_PARAMETER_ONLY + "<br />" +
"The different statuses returned are: %s. The %s status is returned when there is no quality gate associated with the analysis.<br />" +
"Returns an HTTP code 404 if the analysis associated with the task is not found or does not exist.<br />" +
- "Requires 'Administer System' or 'Execute Analysis' permission.", QG_STATUSES_ONE_LINE, ProjectStatusWsResponse.Status.NONE))
+ "Requires one of the following permissions:" +
+ "<ul>" +
+ "<li>'Administer System'</li>" +
+ "<li>'Administer' rights on the specified project</li>" +
+ "<li>'Browse' on the specified project</li>" +
+ "</ul>", QG_STATUSES_ONE_LINE, ProjectStatusWsResponse.Status.NONE))
.setResponseExample(getClass().getResource("project_status-example.json"))
.setSince("5.3")
.setHandler(this);
}
private void checkPermission(String projectUuid) {
- if (!userSession.hasPermission(SYSTEM_ADMIN)
- && !userSession.hasComponentUuidPermission(SCAN_EXECUTION, projectUuid)) {
+ if (!userSession.hasComponentUuidPermission(UserRole.ADMIN, projectUuid) &&
+ !userSession.hasComponentUuidPermission(UserRole.USER, projectUuid)) {
throw insufficientPrivilegesException();
}
}
import org.junit.rules.ExpectedException;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
-import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
@Test
public void json_example() throws IOException {
- userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN);
+ userSession.setGlobalPermissions(SYSTEM_ADMIN);
ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid"));
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project)
@Test
public void return_status_by_project_id() throws IOException {
- userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN);
+ userSession.setGlobalPermissions(SYSTEM_ADMIN);
ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid"));
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project)
@Test
public void return_status_by_project_key() throws IOException {
- userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN);
+ userSession.setGlobalPermissions(SYSTEM_ADMIN);
ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid").setKey("project-key"));
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project)
@Test
public void return_undefined_status_if_measure_is_not_found() {
- userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN);
+ userSession.setGlobalPermissions(SYSTEM_ADMIN);
ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid"));
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project));
@Test
public void return_undefined_status_if_snapshot_is_not_found() {
- userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN);
+ userSession.setGlobalPermissions(SYSTEM_ADMIN);
componentDb.insertComponent(newProjectDto("project-uuid"));
ProjectStatusWsResponse result = callByProjectUuid("project-uuid");
}
@Test
- public void not_fail_with_system_admin_permission() {
- userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN);
+ public void not_fail_with_project_admin_permission() {
+ userSession.addProjectUuidPermissions(UserRole.ADMIN, "project-uuid");
ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid"));
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project));
}
@Test
- public void not_fail_with_global_scan_permission() {
- userSession.login("john").setGlobalPermissions(SCAN_EXECUTION);
+ public void not_fail_with_browse_permission() {
+ userSession.addProjectUuidPermissions(UserRole.USER, "project-uuid");
ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid"));
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project));
call(snapshot.getId().toString());
}
- @Test
- public void not_fail_with_project_scan_permission() {
- ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid"));
- SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project));
- dbSession.commit();
-
- userSession.login("john").addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
-
- call(snapshot.getId().toString());
- }
-
@Test
public void fail_if_no_snapshot_id_found() {
- userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN);
+ userSession.setGlobalPermissions(SYSTEM_ADMIN);
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("Analysis with id 'task-uuid' is not found");
@Test
public void fail_if_insufficient_privileges() {
- userSession.login("john").setGlobalPermissions(PROVISIONING);
+ userSession.setGlobalPermissions(PROVISIONING);
ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid"));
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project));