]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8608 use the component's org when creating a component user perm
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 2 Feb 2017 14:36:36 +0000 (15:36 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 7 Feb 2017 08:31:51 +0000 (09:31 +0100)
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/PermissionWsSupport.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
server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java

index a6bd635bedd98e9668565f8697e95ebbc6e3a7eb..8182375c38b6d63f09de5afa961a86dbed7b19b0 100644 (file)
@@ -83,7 +83,7 @@ public class AddGroupAction implements PermissionsWsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       GroupIdOrAnyone group = support.findGroup(dbSession, request);
-      Optional<ProjectId> projectId = support.findProject(dbSession, request);
+      Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
 
       checkProjectAdmin(userSession, group.getOrganizationUuid(), projectId);
 
index 5562e2b0e3dd6d9fe9c50f3c900f271aae990dbc..2534304e2073e1c2ff550f1ffbdd9db623bad87e 100644 (file)
@@ -25,7 +25,9 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.component.ComponentDto;
 import org.sonar.db.organization.OrganizationDto;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.permission.PermissionChange;
 import org.sonar.server.permission.PermissionUpdater;
 import org.sonar.server.permission.ProjectId;
@@ -84,11 +86,15 @@ public class AddUserAction implements PermissionsWsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       UserId user = support.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
-      Optional<ProjectId> projectId = support.findProject(dbSession, request);
+      Optional<ComponentDto> project = support.findProject(dbSession, request);
       String organizationKey = request.param(PARAM_ORGANIZATION_KEY);
-      checkArgument(!projectId.isPresent() || organizationKey == null, "Organization must not be set when project is set.");
-      OrganizationDto org = support.findOrganization(dbSession, organizationKey);
+      checkArgument(!project.isPresent() || organizationKey == null, "Organization must not be set when project is set.");
+      OrganizationDto org = project
+        .map(dto -> dbClient.organizationDao().selectByUuid(dbSession, dto.getOrganizationUuid()))
+        .orElseGet(() -> Optional.ofNullable(support.findOrganization(dbSession, organizationKey)))
+        .orElseThrow(() -> new NotFoundException(String.format("Organization with key '%s' not found", organizationKey)));
 
+      Optional<ProjectId> projectId = project.map(ProjectId::new);
       checkProjectAdmin(userSession, org.getUuid(), projectId);
 
       PermissionChange change = new UserPermissionChange(
index 2e22e180021428086396811fed0b1439ecd88e3d..c1e622b19439e3a69e40605c80a1ad8006e6e99e 100644 (file)
@@ -95,7 +95,7 @@ public class GroupsAction implements PermissionsWsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
-      Optional<ProjectId> projectId = support.findProject(dbSession, request);
+      Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
       checkProjectAdmin(userSession, org.getUuid(), projectId);
 
       PermissionQuery query = buildPermissionQuery(request, projectId);
index 30522a2e6204c594e3046a0c07955eb2dc11cf4b..faf6cbb7829e44133c71034afcc73e164ff2e471 100644 (file)
@@ -64,17 +64,22 @@ public class PermissionWsSupport {
   /**
    * @throws org.sonar.server.exceptions.NotFoundException if a project does not exist
    */
-  public ProjectId findProject(DbSession dbSession, ProjectWsRef ref) {
+  public ProjectId findProjectId(DbSession dbSession, ProjectWsRef ref) {
     ComponentDto project = componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, ref.uuid(), ref.key(), resourceTypes);
     return new ProjectId(project.getId(), project.uuid());
   }
 
-  public Optional<ProjectId> findProject(DbSession dbSession, Request request) {
+  public Optional<ProjectId> findProjectId(DbSession dbSession, Request request) {
+    return findProject(dbSession, request)
+      .map(ProjectId::new);
+  }
+
+  public Optional<ComponentDto> findProject(DbSession dbSession, Request request) {
     String uuid = request.param(PermissionsWsParameters.PARAM_PROJECT_ID);
     String key = request.param(PermissionsWsParameters.PARAM_PROJECT_KEY);
     if (uuid != null || key != null) {
       ProjectWsRef ref = ProjectWsRef.newWsProjectRef(uuid, key);
-      return Optional.of(findProject(dbSession, ref));
+      return Optional.of(componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, ref.uuid(), ref.key(), resourceTypes));
     }
     return Optional.empty();
   }
index 3b743868df67e7e6078e8e2525774a8dd92af637..ff81daef00f9d77646c66dffce825d25ef296b16 100644 (file)
@@ -83,7 +83,7 @@ public class RemoveGroupAction implements PermissionsWsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       GroupIdOrAnyone group = support.findGroup(dbSession, request);
-      Optional<ProjectId> projectId = support.findProject(dbSession, request);
+      Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
 
       checkProjectAdmin(userSession, group.getOrganizationUuid(), projectId);
 
index 6886f4bb23fe460816ecbaf9dbfa9737e5c98102..8dffdecc73605913963ae5c85c8e2871896c035c 100644 (file)
@@ -83,7 +83,7 @@ public class RemoveUserAction implements PermissionsWsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       UserId user = support.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
-      Optional<ProjectId> projectId = support.findProject(dbSession, request);
+      Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
       OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
 
       checkProjectAdmin(userSession, org.getUuid(), projectId);
index 05474fddf18e589a6f8f6e398a1728625b2e5ec4..c5d667d73498c6aaaea8e640dd32b106807e5ed9 100644 (file)
@@ -126,7 +126,7 @@ public class SearchProjectPermissionsAction implements PermissionsWsAction {
     com.google.common.base.Optional<ProjectWsRef> projectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
     Optional<ProjectId> projectId;
     if (projectRef.isPresent()) {
-      projectId = Optional.of(wsSupport.findProject(dbSession, projectRef.get()));
+      projectId = Optional.of(wsSupport.findProjectId(dbSession, projectRef.get()));
     } else {
       projectId = Optional.empty();
     }
index 16924c5ac767d5a7dc4ed43209ea8eefcfeea164..99d011614a8cd986daacb35c1556bdfc14a889e9 100644 (file)
@@ -100,7 +100,7 @@ public class UsersAction implements PermissionsWsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
-      Optional<ProjectId> projectId = support.findProject(dbSession, request);
+      Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
       checkProjectAdmin(userSession, org.getUuid(), projectId);
 
       PermissionQuery query = buildPermissionQuery(request, projectId);
index fefa5fc19789de2a4de03f2323d7279a9dd98b00..058e9f291230dfc46e2027db20cbdf9ee092df55 100644 (file)
@@ -59,7 +59,7 @@ public class AddUserActionTest extends BasePermissionWsTest<AddUserAction> {
   }
 
   @Test
-  public void add_permission_to_user() throws Exception {
+  public void add_permission_to_user_on_default_organization_if_organization_is_not_specified() throws Exception {
     loginAsAdmin(db.getDefaultOrganization());
     newRequest()
       .setParam(PARAM_USER_LOGIN, user.getLogin())
@@ -69,18 +69,32 @@ public class AddUserActionTest extends BasePermissionWsTest<AddUserAction> {
     assertThat(db.users().selectGlobalPermissionsOfUser(user, db.getDefaultOrganization())).containsOnly(SYSTEM_ADMIN);
   }
 
+  @Test
+  public void add_permission_to_user_on_specified_organization() throws Exception {
+    OrganizationDto organization = db.organizations().insert();
+    loginAsAdmin(organization);
+    newRequest()
+      .setParam(PARAM_ORGANIZATION_KEY, organization.getKey())
+      .setParam(PARAM_USER_LOGIN, user.getLogin())
+      .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+      .execute();
+
+    assertThat(db.users().selectGlobalPermissionsOfUser(user, organization)).containsOnly(SYSTEM_ADMIN);
+  }
+
   @Test
   public void add_permission_to_project_referenced_by_its_id() throws Exception {
-    ComponentDto project = db.components().insertProject();
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = db.components().insertProject(organization);
 
-    loginAsAdmin(db.getDefaultOrganization());
+    loginAsAdmin(organization);
     newRequest()
       .setParam(PARAM_USER_LOGIN, user.getLogin())
       .setParam(PARAM_PROJECT_ID, project.uuid())
       .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
       .execute();
 
-    assertThat(db.users().selectGlobalPermissionsOfUser(user, db.getDefaultOrganization())).isEmpty();
+    assertThat(db.users().selectGlobalPermissionsOfUser(user, organization)).isEmpty();
     assertThat(db.users().selectProjectPermissionsOfUser(user, project)).containsOnly(SYSTEM_ADMIN);
   }