]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10323 Fix permission on api/project_branches/list
authorEric Hartmann <hartmann.eric@gmail.com>
Thu, 15 Feb 2018 16:31:55 +0000 (17:31 +0100)
committerEric Hartmann <hartmann.eric@gmail.Com>
Wed, 21 Feb 2018 14:01:01 +0000 (15:01 +0100)
server/sonar-server/src/main/java/org/sonar/server/branch/ws/ListAction.java
server/sonar-server/src/main/java/org/sonar/server/setting/ws/ValuesAction.java
server/sonar-server/src/test/java/org/sonar/server/branch/ws/ListActionTest.java

index 999eacf33b694c6b04856c5b22939b3b3e6c6b33..ca822de18f8e512d52ba7879d6aba305368e8739 100644 (file)
@@ -52,6 +52,7 @@ import static java.util.Collections.singletonList;
 import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
 import static org.sonar.api.resources.Qualifiers.PROJECT;
 import static org.sonar.api.utils.DateUtils.formatDateTime;
+import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
 import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
@@ -60,6 +61,7 @@ import static org.sonar.db.component.BranchType.SHORT;
 import static org.sonar.server.branch.ws.BranchesWs.addProjectParam;
 import static org.sonar.server.branch.ws.ProjectBranchesParameters.ACTION_LIST;
 import static org.sonar.server.branch.ws.ProjectBranchesParameters.PARAM_PROJECT;
+import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
 
 public class ListAction implements BranchWsAction {
 
@@ -80,7 +82,7 @@ public class ListAction implements BranchWsAction {
     WebService.NewAction action = context.createAction(ACTION_LIST)
       .setSince("6.6")
       .setDescription("List the branches of a project.<br/>" +
-        "Requires 'Administer' rights on the specified project.")
+        "Requires 'Browse' or 'Execute analysis' rights on the specified project.")
       .setResponseExample(Resources.getResource(getClass(), "list-example.json"))
       .setHandler(this);
 
@@ -93,7 +95,7 @@ public class ListAction implements BranchWsAction {
 
     try (DbSession dbSession = dbClient.openSession(false)) {
       ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
-      userSession.checkComponentPermission(UserRole.USER, project);
+      checkPermission(project);
       checkArgument(project.isEnabled() && PROJECT.equals(project.qualifier()), "Invalid project key");
 
       Collection<BranchDto> branches = dbClient.branchDao().selectByComponent(dbSession, project);
@@ -158,4 +160,11 @@ public class ListAction implements BranchWsAction {
     }
     builder.setStatus(statusBuilder);
   }
+
+  private void checkPermission(ComponentDto component) {
+    if (!userSession.hasComponentPermission(UserRole.USER, component) &&
+      !userSession.hasComponentPermission(SCAN_EXECUTION, component)) {
+      throw insufficientPrivilegesException();
+    }
+  }
 }
index 7a6aba35e753959539d79a9ff6d4b77f6b7f909c..273e8a8a568965b427df9c3d459b05aabefa1514 100644 (file)
@@ -51,9 +51,11 @@ import static org.sonar.api.CoreProperties.SERVER_ID;
 import static org.sonar.api.CoreProperties.SERVER_STARTTIME;
 import static org.sonar.api.PropertyType.PROPERTY_SET;
 import static org.sonar.api.web.UserRole.USER;
+import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
 import static org.sonar.server.setting.ws.SettingsWsParameters.PARAM_BRANCH;
 import static org.sonar.server.setting.ws.SettingsWsParameters.PARAM_COMPONENT;
 import static org.sonar.server.setting.ws.SettingsWsParameters.PARAM_KEYS;
+import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
 import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
@@ -87,7 +89,7 @@ public class ValuesAction implements SettingsWsAction {
       .setDescription("List settings values.<br>" +
         "If no value has been set for a setting, then the default value is returned.<br>" +
         "The settings from conf/sonar.properties are excluded from results.<br>" +
-        "Requires 'Browse' permission when a component is specified<br/>",
+        "Requires 'Browse' or 'Execute Analysis' permission when a component is specified<br/>",
         "To access licensed settings, authentication is required<br/>" +
           "To access secured settings, one of the following permissions is required: " +
           "<ul>" +
@@ -152,7 +154,9 @@ public class ValuesAction implements SettingsWsAction {
       return Optional.empty();
     }
     ComponentDto component = componentFinder.getByKeyAndOptionalBranch(dbSession, componentKey, valuesRequest.getBranch());
-    userSession.checkComponentPermission(USER, component);
+    if (!userSession.hasComponentPermission(USER, component) && !userSession.hasComponentPermission(SCAN_EXECUTION, component)) {
+      throw insufficientPrivilegesException();
+    }
     return Optional.of(component);
   }
 
index 3c23e496ca2e2231628813e63b75682cfab8d345..937466ea2140b7b14bd516929077862388543572 100644 (file)
@@ -68,6 +68,7 @@ import static org.sonar.api.rules.RuleType.CODE_SMELL;
 import static org.sonar.api.rules.RuleType.VULNERABILITY;
 import static org.sonar.api.utils.DateUtils.dateToLong;
 import static org.sonar.api.utils.DateUtils.parseDateTime;
+import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
 import static org.sonar.test.JsonAssert.assertJson;
 import static org.sonarqube.ws.ProjectBranches.Branch.Status;
 
@@ -125,6 +126,25 @@ public class ListActionTest {
     assertJson(json).isSimilarTo(ws.getDef().responseExampleAsString());
   }
 
+  @Test
+  public void test_with_SCAN_EXCUTION_permission() {
+    ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("sonarqube"));
+    ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setKey("feature/bar").setBranchType(BranchType.LONG));
+    ComponentDto shortLivingBranch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo").setBranchType(BranchType.SHORT).setMergeBranchUuid(longLivingBranch.uuid()));
+    userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
+
+    db.getDbClient().snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(longLivingBranch).setLast(true).setCreatedAt(DateUtils.parseDateTime("2017-04-01T01:15:42+0100").getTime()));
+    db.getDbClient().snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(shortLivingBranch).setLast(true).setCreatedAt(DateUtils.parseDateTime("2017-04-03T13:37:00+0100").getTime()));
+    db.commit();
+
+    String json = ws.newRequest()
+      .setParam("project", project.getDbKey())
+      .execute()
+      .getInput();
+
+    assertJson(json).isSimilarTo(ws.getDef().responseExampleAsString());
+  }
+
   @Test
   public void main_branch() {
     ComponentDto project = db.components().insertMainBranch();