From ac1f9b00062a3f0a9e8dec4c6273e42c90007e6a Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 2 Feb 2017 15:36:36 +0100 Subject: [PATCH] SONAR-8608 use the component's org when creating a component user perm --- .../server/permission/ws/AddGroupAction.java | 2 +- .../server/permission/ws/AddUserAction.java | 12 +++++++--- .../server/permission/ws/GroupsAction.java | 2 +- .../permission/ws/PermissionWsSupport.java | 11 +++++++--- .../permission/ws/RemoveGroupAction.java | 2 +- .../permission/ws/RemoveUserAction.java | 2 +- .../ws/SearchProjectPermissionsAction.java | 2 +- .../server/permission/ws/UsersAction.java | 2 +- .../permission/ws/AddUserActionTest.java | 22 +++++++++++++++---- 9 files changed, 41 insertions(+), 16 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java index a6bd635bedd..8182375c38b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java @@ -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 = support.findProject(dbSession, request); + Optional projectId = support.findProjectId(dbSession, request); checkProjectAdmin(userSession, group.getOrganizationUuid(), projectId); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserAction.java index 5562e2b0e3d..2534304e207 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserAction.java @@ -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 = support.findProject(dbSession, request); + Optional 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 = project.map(ProjectId::new); checkProjectAdmin(userSession, org.getUuid(), projectId); PermissionChange change = new UserPermissionChange( diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java index 2e22e180021..c1e622b1943 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java @@ -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 = support.findProject(dbSession, request); + Optional projectId = support.findProjectId(dbSession, request); checkProjectAdmin(userSession, org.getUuid(), projectId); PermissionQuery query = buildPermissionQuery(request, projectId); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java index 30522a2e620..faf6cbb7829 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java @@ -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 findProject(DbSession dbSession, Request request) { + public Optional findProjectId(DbSession dbSession, Request request) { + return findProject(dbSession, request) + .map(ProjectId::new); + } + + public Optional 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(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupAction.java index 3b743868df6..ff81daef00f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupAction.java @@ -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 = support.findProject(dbSession, request); + Optional projectId = support.findProjectId(dbSession, request); checkProjectAdmin(userSession, group.getOrganizationUuid(), projectId); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java index 6886f4bb23f..8dffdecc736 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java @@ -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 = support.findProject(dbSession, request); + Optional projectId = support.findProjectId(dbSession, request); OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY)); checkProjectAdmin(userSession, org.getUuid(), projectId); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsAction.java index 05474fddf18..c5d667d7349 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsAction.java @@ -126,7 +126,7 @@ public class SearchProjectPermissionsAction implements PermissionsWsAction { com.google.common.base.Optional projectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()); Optional projectId; if (projectRef.isPresent()) { - projectId = Optional.of(wsSupport.findProject(dbSession, projectRef.get())); + projectId = Optional.of(wsSupport.findProjectId(dbSession, projectRef.get())); } else { projectId = Optional.empty(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java index 16924c5ac76..99d011614a8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java @@ -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 = support.findProject(dbSession, request); + Optional projectId = support.findProjectId(dbSession, request); checkProjectAdmin(userSession, org.getUuid(), projectId); PermissionQuery query = buildPermissionQuery(request, projectId); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java index fefa5fc1978..058e9f29123 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java @@ -59,7 +59,7 @@ public class AddUserActionTest extends BasePermissionWsTest { } @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 { 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); } -- 2.39.5