]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8716 Use UserSession#hasComponentPermission(String,ComponentDto)
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 29 Jan 2017 19:24:32 +0000 (20:24 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 1 Feb 2017 16:11:52 +0000 (17:11 +0100)
15 files changed:
server/sonar-server/src/main/java/org/sonar/server/batch/ProjectDataLoader.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/ShowAction.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java
server/sonar-server/src/main/java/org/sonar/server/projectlink/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ProjectStatusAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java
server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingsPermissionPredicates.java
server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/GetByProjectActionTest.java

index 32e1f90a7e72990d722494ce13cc26598eed1906..a4e8e6d0d1a3713c0e701716aee4876dd6136535 100644 (file)
@@ -69,8 +69,8 @@ public class ProjectDataLoader {
         throw new BadRequestException(format("Key '%s' belongs to a component which is not a Project", query.getModuleKey()));
       }
 
-      boolean hasScanPerm = userSession.hasComponentUuidPermission(SCAN_EXECUTION, module.projectUuid());
-      boolean hasBrowsePerm = userSession.hasComponentUuidPermission(USER, module.projectUuid());
+      boolean hasScanPerm = userSession.hasComponentPermission(SCAN_EXECUTION, module);
+      boolean hasBrowsePerm = userSession.hasComponentPermission(USER, module);
       checkPermission(query.isIssuesMode(), hasScanPerm, hasBrowsePerm);
 
       ComponentDto project = getProject(module, session);
index eb9dee249a16cbde791212191d8907ff90630f2e..844aabc2edd4ec230837c80625791fac23a6cf60 100644 (file)
@@ -145,7 +145,7 @@ public class AppAction implements RequestHandler {
   }
 
   private static void appendPermissions(JsonWriter json, ComponentDto component, UserSession userSession) {
-    boolean hasBrowsePermission = userSession.hasComponentUuidPermission(UserRole.USER, component.uuid());
+    boolean hasBrowsePermission = userSession.hasComponentPermission(UserRole.USER, component);
     json.prop("canMarkAsFavourite", userSession.isLoggedIn() && hasBrowsePermission);
   }
 
index e6b362538c26fe7d99c3a0d1d08e15c9efa63a35..f088cb6b5bcd56e1638dc0ab8ba82b589eaff269 100644 (file)
@@ -35,7 +35,6 @@ import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.WsComponents.ShowWsResponse;
 import org.sonarqube.ws.client.component.ShowWsRequest;
 
-import static com.google.common.base.MoreObjects.firstNonNull;
 import static java.lang.String.format;
 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
 import static org.sonar.server.component.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
@@ -119,8 +118,7 @@ public class ShowAction implements ComponentsWsAction {
 
   private ComponentDto getComponentByUuidOrKey(DbSession dbSession, ShowWsRequest request) {
     ComponentDto component = componentFinder.getByUuidOrKey(dbSession, request.getId(), request.getKey(), ParamNames.ID_AND_KEY);
-    String projectUuid = firstNonNull(component.projectUuid(), component.uuid());
-    userSession.checkComponentUuidPermission(UserRole.USER, projectUuid);
+    userSession.checkComponentPermission(UserRole.USER, component);
     return component;
   }
 }
index 1b6f3db1c3b12a86a52c587de256838004474772..4e2d2e3fdb0265155e218aa3ac07af95f740adb1 100644 (file)
@@ -51,7 +51,6 @@ import org.sonarqube.ws.WsComponents;
 import org.sonarqube.ws.WsComponents.TreeWsResponse;
 import org.sonarqube.ws.client.component.TreeWsRequest;
 
-import static com.google.common.base.MoreObjects.firstNonNull;
 import static com.google.common.collect.FluentIterable.from;
 import static com.google.common.collect.Sets.newHashSet;
 import static java.lang.String.CASE_INSENSITIVE_ORDER;
@@ -189,8 +188,7 @@ public class TreeAction implements ComponentsWsAction {
   }
 
   private void checkPermissions(ComponentDto baseComponent) {
-    String projectUuid = firstNonNull(baseComponent.projectUuid(), baseComponent.uuid());
-    userSession.checkComponentUuidPermission(UserRole.USER, projectUuid);
+    userSession.checkComponentPermission(UserRole.USER, baseComponent);
   }
 
   private static TreeWsResponse buildResponse(ComponentDto baseComponent, OrganizationDto organizationDto, List<ComponentDto> components,
index 9e89a6812ac99eb82bc18ded5bd93f1c92491492..081b333437b0544a7138c003b4b1fc6ffe3fb583 100644 (file)
@@ -54,7 +54,6 @@ import org.sonarqube.ws.WsMeasures;
 import org.sonarqube.ws.WsMeasures.ComponentWsResponse;
 import org.sonarqube.ws.client.measure.ComponentWsRequest;
 
-import static com.google.common.base.MoreObjects.firstNonNull;
 import static java.lang.String.format;
 import static java.util.Collections.emptyList;
 import static java.util.Collections.emptyMap;
@@ -258,7 +257,6 @@ public class ComponentAction implements MeasuresWsAction {
   }
 
   private void checkPermissions(ComponentDto baseComponent) {
-    String projectUuid = firstNonNull(baseComponent.projectUuid(), baseComponent.uuid());
-    userSession.checkComponentUuidPermission(UserRole.USER, projectUuid);
+    userSession.checkComponentPermission(UserRole.USER, baseComponent);
   }
 }
index 4fe6125defea9fbecefead67db1a1255c3c2bb7a..76cfde04de0429149d6fbe3e34d5aa612992897c 100644 (file)
@@ -61,7 +61,6 @@ import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.WsMeasures;
 import org.sonarqube.ws.client.measure.ComponentTreeWsRequest;
 
-import static com.google.common.base.MoreObjects.firstNonNull;
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.collect.Sets.newHashSet;
 import static java.lang.String.format;
@@ -298,8 +297,7 @@ public class ComponentTreeDataLoader {
   }
 
   private void checkPermissions(ComponentDto baseComponent) {
-    String projectUuid = firstNonNull(baseComponent.projectUuid(), baseComponent.uuid());
-    userSession.checkComponentUuidPermission(UserRole.USER, projectUuid);
+    userSession.checkComponentPermission(UserRole.USER, baseComponent);
   }
 
   private enum IsFileComponent implements Predicate<ComponentDto> {
index 2e041d135d4a69916bfebd54830b3e625081a0f6..931da4263f6abb559a9ebb2c2f31b0fa3df50e5a 100644 (file)
@@ -124,8 +124,8 @@ public class SearchAction implements ProjectLinksWsAction {
       request.getProjectKey(),
       ComponentFinder.ParamNames.PROJECT_ID_AND_KEY);
 
-    if (!userSession.hasComponentUuidPermission(UserRole.ADMIN, component.uuid()) &&
-      !userSession.hasComponentUuidPermission(UserRole.USER, component.uuid())) {
+    if (!userSession.hasComponentPermission(UserRole.ADMIN, component) &&
+      !userSession.hasComponentPermission(UserRole.USER, component)) {
       throw insufficientPrivilegesException();
     }
 
index cdeb026538bece59663764657892532e0d0a42b6..743d9d1c5a16686fd3e57c98a47e3cd68b50394c 100644 (file)
@@ -148,7 +148,7 @@ public class IndexAction implements WsAction {
   }
 
   private boolean hasAdminPermission(Optional<ComponentDto> component) {
-    return component.isPresent() ? userSession.hasComponentUuidPermission(ADMIN, component.get().uuid()) : userSession.hasPermission(GlobalPermissions.SYSTEM_ADMIN);
+    return component.isPresent() ? userSession.hasComponentPermission(ADMIN, component.get()) : userSession.hasPermission(GlobalPermissions.SYSTEM_ADMIN);
   }
 
   private List<PropertyDto> loadGlobalSettings(DbSession dbSession, Optional<String> key) {
index 4743bb2871776249418cf47e56d342118d1c1f21..fcaa6e38ae10e901a6f90658154b8859fac558a2 100644 (file)
@@ -271,7 +271,7 @@ public class QualityGates {
   private void checkPermission(Long projectId, DbSession session) {
     ComponentDto project = componentDao.selectOrFailById(session, projectId);
     if (!userSession.hasPermission(GlobalPermissions.QUALITY_GATE_ADMIN)
-      && !userSession.hasComponentUuidPermission(UserRole.ADMIN, project.uuid())) {
+      && !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
       throw new ForbiddenException("Insufficient privileges");
     }
   }
index 977e9120e9c5ca0495ad067214456e4b03d7a8df..1be33bba7ffdba846ebfc12e6faaf5535918e50a 100644 (file)
@@ -92,8 +92,8 @@ public class GetByProjectAction implements QualityGatesWsAction {
   private ComponentDto getProject(DbSession dbSession, String projectUuid, String projectKey) {
     ComponentDto project = componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey, ParamNames.PROJECT_ID_AND_KEY);
 
-    if (!userSession.hasComponentUuidPermission(UserRole.USER, projectUuid) &&
-      !userSession.hasComponentUuidPermission(UserRole.ADMIN, projectUuid)) {
+    if (!userSession.hasComponentPermission(UserRole.USER, project) &&
+      !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
       throw insufficientPrivilegesException();
     }
 
index 2db5c5456eb39f106811e496dbc0758e644c83c0..9177ea6ef6738833bb699dd5bbb6547e93f6ab4f 100644 (file)
@@ -114,7 +114,7 @@ public class ProjectStatusAction implements QualityGatesWsAction {
     DbSession dbSession = dbClient.openSession(false);
     try {
       ProjectAndSnapshot projectAndSnapshot = getProjectAndSnapshot(dbSession, request);
-      checkPermission(projectAndSnapshot.project.uuid());
+      checkPermission(projectAndSnapshot.project);
       Optional<String> measureData = getQualityGateDetailsMeasureData(dbSession, projectAndSnapshot.project);
 
       return ProjectStatusWsResponse.newBuilder()
@@ -178,9 +178,9 @@ public class ProjectStatusAction implements QualityGatesWsAction {
     return projectStatusWsRequest;
   }
 
-  private void checkPermission(String projectUuid) {
-    if (!userSession.hasComponentUuidPermission(UserRole.ADMIN, projectUuid) &&
-      !userSession.hasComponentUuidPermission(UserRole.USER, projectUuid)) {
+  private void checkPermission(ComponentDto project) {
+    if (!userSession.hasComponentPermission(UserRole.ADMIN, project) &&
+      !userSession.hasComponentPermission(UserRole.USER, project)) {
       throw insufficientPrivilegesException();
     }
   }
index 318c11ee04e41afd7b9bb22c3ed14db479e5c472..b44cdc1f5fb5563557595e79e3ad980f58e9243c 100644 (file)
@@ -119,7 +119,7 @@ public class SelectAction implements QualityGatesWsAction {
       .or(() -> componentFinder.getByUuidOrKey(dbSession, projectId, projectKey, ParamNames.PROJECT_ID_AND_KEY));
 
     if (!userSession.hasPermission(GlobalPermissions.QUALITY_GATE_ADMIN) &&
-      !userSession.hasComponentUuidPermission(UserRole.ADMIN, project.uuid())) {
+      !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
       throw insufficientPrivilegesException();
     }
 
index 1bad7a11a392427ab35e93570f82a1d6c0c37377..105694599137945619cc77470abfc07584c3735a 100644 (file)
@@ -68,6 +68,6 @@ public class SettingsPermissionPredicates {
   }
 
   private boolean hasPermission(String permission, Optional<ComponentDto> component) {
-    return userSession.hasPermission(permission) || (component.isPresent() && userSession.hasComponentUuidPermission(permission, component.get().uuid()));
+    return userSession.hasPermission(permission) || (component.isPresent() && userSession.hasComponentPermission(permission, component.get()));
   }
 }
index 0e7bdb081cfb4e378b2f4d671bf561911065da83..e658d3207e580367b5328a6d4bce214ce2d264ac 100644 (file)
@@ -129,7 +129,7 @@ public class ComponentAction implements NavigationWsAction {
     String componentKey = request.mandatoryParam(PARAM_COMPONENT_KEY);
     try (DbSession session = dbClient.openSession(false)) {
       ComponentDto component = componentFinder.getByKey(session, componentKey);
-      if (!(userSession.hasComponentUuidPermission(USER, component.projectUuid()) || userSession.hasComponentUuidPermission(ADMIN, component.projectUuid()))) {
+      if (!(userSession.hasComponentPermission(USER, component) || userSession.hasComponentPermission(ADMIN, component))) {
         throw new ForbiddenException("Insufficient privileges");
       }
       OrganizationDto organizationDto = componentFinder.getOrganization(session, component);
@@ -140,7 +140,7 @@ public class ComponentAction implements NavigationWsAction {
       writeComponent(json, session, component, organizationDto, analysis.orElse(null));
       writeProfiles(json, session, component);
       writeQualityGate(json, session, component);
-      if (userSession.hasComponentUuidPermission(ADMIN, component.projectUuid()) || userSession.hasPermission(QUALITY_PROFILE_ADMIN)) {
+      if (userSession.hasComponentPermission(ADMIN, component) || userSession.hasPermission(QUALITY_PROFILE_ADMIN)) {
         writeConfiguration(json, component);
       }
       writeBreadCrumbs(json, session, component);
@@ -200,7 +200,7 @@ public class ComponentAction implements NavigationWsAction {
     json.name("extensions").beginArray();
     Predicate<Page> isAuthorized = page -> {
       String requiredPermission = page.isAdmin() ? UserRole.ADMIN : UserRole.USER;
-      return userSession.hasComponentUuidPermission(requiredPermission, component.uuid());
+      return userSession.hasComponentPermission(requiredPermission, component);
     };
     pages.stream()
       .filter(isAuthorized)
@@ -209,7 +209,7 @@ public class ComponentAction implements NavigationWsAction {
   }
 
   private void writeConfiguration(JsonWriter json, ComponentDto component) {
-    boolean isAdmin = userSession.hasComponentUuidPermission(ADMIN, component.projectUuid());
+    boolean isAdmin = userSession.hasComponentPermission(ADMIN, component);
 
     json.name("configuration").beginObject();
     writeConfigPageAccess(json, isAdmin, component);
index cbaa6dd3809ea278f0d98bc20d51f26a6218ddc3..47c240ea118a2d1bf140d31ed65e0993c1fd1fe4 100644 (file)
@@ -62,9 +62,9 @@ public class GetByProjectActionTest {
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
-  ComponentDbTester componentDb = new ComponentDbTester(db);
-  DbClient dbClient = db.getDbClient();
-  DbSession dbSession = db.getSession();
+  private ComponentDbTester componentDb = new ComponentDbTester(db);
+  private DbClient dbClient = db.getDbClient();
+  private DbSession dbSession = db.getSession();
 
   private WsActionTester ws = new WsActionTester(
     new GetByProjectAction(userSession, dbClient, new ComponentFinder(dbClient), new QualityGateFinder(dbClient)));
@@ -85,7 +85,7 @@ public class GetByProjectActionTest {
 
   @Test
   public void empty_response() {
-    ComponentDto project = componentDb.insertComponent(newProjectDto(db.getDefaultOrganization()));
+    ComponentDto project = componentDb.insertProject();
     insertQualityGate("Another QG");
 
     String result = ws.newRequest().setParam(PARAM_PROJECT_ID, project.uuid()).execute().getInput();
@@ -135,8 +135,8 @@ public class GetByProjectActionTest {
 
   @Test
   public void get_with_project_admin_permission() {
-    ComponentDto project = componentDb.insertComponent(newProjectDto(db.organizations().insert()));
-    userSession.anonymous().addProjectUuidPermissions(UserRole.USER, project.uuid());
+    ComponentDto project = componentDb.insertProject();
+    userSession.login().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
     QualityGateDto dbQualityGate = insertQualityGate("Sonar way");
     setDefaultQualityGate(dbQualityGate.getId());
 
@@ -146,9 +146,9 @@ public class GetByProjectActionTest {
   }
 
   @Test
-  public void get_with_project_browse_permission() {
-    ComponentDto project = componentDb.insertComponent(newProjectDto(db.getDefaultOrganization()));
-    userSession.anonymous().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+  public void get_with_project_user_permission() {
+    ComponentDto project = componentDb.insertProject();
+    userSession.login().addProjectUuidPermissions(UserRole.USER, project.uuid());
     QualityGateDto dbQualityGate = insertQualityGate("Sonar way");
     setDefaultQualityGate(dbQualityGate.getId());