]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8134 support of orgs in PermissionPrivilegeChecker
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 14 Oct 2016 08:01:30 +0000 (10:01 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 16 Oct 2016 17:10:46 +0000 (19:10 +0200)
server/sonar-server/src/main/java/org/sonar/server/permission/PermissionPrivilegeChecker.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java

index f470a7c590ce9126502aa93f2ded2fc84b5fc6b6..b46d31f8399af84922d40668cbabaa16ad5ee2b6 100644 (file)
@@ -43,7 +43,26 @@ public class PermissionPrivilegeChecker {
     }
   }
 
-  public static void checkAdministrationPermission(UserSession userSession, Optional<ProjectId> projectId) {
+  /**
+   * Checks that user is administrator of the specified project, or of the specified organization if project is not
+   * defined.
+   * @throws org.sonar.server.exceptions.ForbiddenException if user is not administrator
+   */
+  public static void checkProjectAdmin(UserSession userSession, String organizationUuid, Optional<ProjectId> projectId) {
+    userSession.checkLoggedIn();
+    if (!projectId.isPresent() || !userSession.hasComponentUuidPermission(UserRole.ADMIN, projectId.get().getUuid())) {
+      userSession.checkOrganizationPermission(organizationUuid, GlobalPermissions.SYSTEM_ADMIN);
+    }
+  }
+
+  /**
+   * Checks that user is administrator of the specified project, or of system if project is not
+   * defined.
+   * @throws org.sonar.server.exceptions.ForbiddenException if user is not administrator
+   * @deprecated does not support organizations. Replaced by {@link #checkProjectAdmin(UserSession, String, Optional)}
+   */
+  @Deprecated
+  public static void checkProjectAdmin(UserSession userSession, Optional<ProjectId> projectId) {
     userSession.checkLoggedIn();
     if (!projectId.isPresent() || !userSession.hasComponentUuidPermission(UserRole.ADMIN, projectId.get().getUuid())) {
       userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
index faec9a17056c17dddcfd30d5ff45645722dfa97d..8fe71b5e4d3b04e575a87daf532a2d397007a2bb 100644 (file)
@@ -33,7 +33,7 @@ import org.sonar.server.user.UserSession;
 import org.sonar.server.usergroups.ws.GroupIdOrAnyone;
 
 import static java.util.Arrays.asList;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkAdministratorOfProjectOrOrganization;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createGroupIdParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createGroupNameParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
@@ -79,7 +79,7 @@ public class AddGroupAction implements PermissionsWsAction {
       GroupIdOrAnyone group = support.findGroup(dbSession, request);
       Optional<ProjectId> projectId = support.findProject(dbSession, request);
 
-      checkAdministratorOfProjectOrOrganization(userSession, group.getOrganizationUuid(), projectId);
+      checkProjectAdmin(userSession, group.getOrganizationUuid(), projectId);
 
       PermissionChange change = new GroupPermissionChange(
         PermissionChange.Operation.ADD,
index d6155c7b5a45b19a978fbaddfb46c02584908730..08325de06da0bf7c37d49666198802a7cdbbdfac 100644 (file)
@@ -34,7 +34,7 @@ import org.sonar.server.permission.UserPermissionChange;
 import org.sonar.server.user.UserSession;
 
 import static java.util.Arrays.asList;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkAdministratorOfProjectOrOrganization;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameters;
@@ -82,7 +82,7 @@ public class AddUserAction implements PermissionsWsAction {
       Optional<ProjectId> projectId = support.findProject(dbSession, request);
       OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
 
-      checkAdministratorOfProjectOrOrganization(userSession, org.getUuid(), projectId);
+      checkProjectAdmin(userSession, org.getUuid(), projectId);
 
       PermissionChange change = new UserPermissionChange(
         PermissionChange.Operation.ADD,
index 1b6862a34b315f68855b8a99f8b2fb3d4848d58e..49a8715ec5055ee00b437a78e54e2f9a30deb882 100644 (file)
@@ -46,7 +46,7 @@ import static java.util.Collections.emptyList;
 import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
 import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
 import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkAdministrationPermission;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameters;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
@@ -87,7 +87,7 @@ public class GroupsAction implements PermissionsWsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       Optional<ProjectId> projectId = support.findProject(dbSession, request);
-      checkAdministrationPermission(userSession, projectId);
+      checkProjectAdmin(userSession, projectId);
 
       PermissionQuery query = buildPermissionQuery(request, projectId);
       // TODO validatePermission(groupsRequest.getPermission(), wsProjectRef);
index aab5803873814ccb847f7a92ad50828944e29207..89e52894966a58412324dac6bd8954ba6b8795ce 100644 (file)
@@ -33,7 +33,7 @@ import org.sonar.server.user.UserSession;
 import org.sonar.server.usergroups.ws.GroupIdOrAnyone;
 
 import static java.util.Arrays.asList;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkAdministrationPermission;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createGroupIdParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createGroupNameParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
@@ -79,7 +79,7 @@ public class RemoveGroupAction implements PermissionsWsAction {
       GroupIdOrAnyone group = support.findGroup(dbSession, request);
       Optional<ProjectId> projectId = support.findProject(dbSession, request);
 
-      checkAdministrationPermission(userSession, projectId);
+      checkProjectAdmin(userSession, projectId);
 
       PermissionChange change = new GroupPermissionChange(
         PermissionChange.Operation.REMOVE,
index 69b11ec718b58c077473e26d53fc2a6b8f74410f..e931111f633025aacc954b786d0c33ccdce5f060 100644 (file)
@@ -34,7 +34,7 @@ import org.sonar.server.permission.UserPermissionChange;
 import org.sonar.server.user.UserSession;
 
 import static java.util.Arrays.asList;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkAdministrationPermission;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameters;
@@ -82,7 +82,7 @@ public class RemoveUserAction implements PermissionsWsAction {
       Optional<ProjectId> projectId = support.findProject(dbSession, request);
       OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
 
-      checkAdministrationPermission(userSession, projectId);
+      checkProjectAdmin(userSession, projectId);
 
       PermissionChange change = new UserPermissionChange(
         PermissionChange.Operation.REMOVE,
index 31177960f81b93debf094019c0731cc3be231f61..c6c5522be119cb7427d46a0a7ce7cc10e2e86267 100644 (file)
@@ -40,7 +40,7 @@ import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse;
 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse.Project;
 import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
 
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkAdministrationPermission;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameters;
 import static org.sonar.server.permission.ws.ProjectWsRef.newOptionalWsProjectRef;
@@ -63,7 +63,7 @@ public class SearchProjectPermissionsAction implements PermissionsWsAction {
   private final PermissionWsSupport wsSupport;
 
   public SearchProjectPermissionsAction(DbClient dbClient, UserSession userSession, I18n i18n, ResourceTypes resourceTypes,
-                                        SearchProjectPermissionsDataLoader dataLoader, PermissionWsSupport wsSupport) {
+    SearchProjectPermissionsDataLoader dataLoader, PermissionWsSupport wsSupport) {
     this.dbClient = dbClient;
     this.userSession = userSession;
     this.i18n = i18n;
@@ -126,7 +126,7 @@ public class SearchProjectPermissionsAction implements PermissionsWsAction {
     } else {
       projectId = Optional.empty();
     }
-    checkAdministrationPermission(userSession, projectId);
+    checkProjectAdmin(userSession, projectId);
   }
 
   private SearchProjectPermissionsWsResponse buildResponse(SearchProjectPermissionsData data) {
index 635e5937affefc93edb5e38f30e6474a098fce2f..56da7853fdd0694c5058180025abae9014d00d99 100644 (file)
@@ -44,7 +44,7 @@ import static java.util.Collections.emptyList;
 import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
 import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
 import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkAdministrationPermission;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateGlobalPermission;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
@@ -90,7 +90,7 @@ public class UsersAction implements PermissionsWsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       Optional<ProjectId> projectId = support.findProject(dbSession, request);
-      checkAdministrationPermission(userSession, projectId);
+      checkProjectAdmin(userSession, projectId);
 
       PermissionQuery query = buildPermissionQuery(request, projectId);
       List<UserDto> users = findUsers(dbSession, query);