diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-13 11:32:52 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-17 13:41:04 +0100 |
commit | 40cf347e9aa2ded35a8798b8aec0abbf2e993183 (patch) | |
tree | 14661c60f1cb83e73ef1bdc041405014282a98d1 /server | |
parent | ad4cf4fb0a36a1b12e9f2719e9fee8a7960a5ee4 (diff) | |
download | sonarqube-40cf347e9aa2ded35a8798b8aec0abbf2e993183.tar.gz sonarqube-40cf347e9aa2ded35a8798b8aec0abbf2e993183.zip |
SONAR-6947 api/permissions/users use UsersWsRequest
Diffstat (limited to 'server')
6 files changed, 51 insertions, 239 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequest.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequest.java deleted file mode 100644 index f9a09cce5d7..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequest.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.server.permission.ws; - -import com.google.common.base.Optional; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.WebService.SelectionMode; -import org.sonar.server.usergroups.ws.WsGroupRef; - -import static org.sonar.api.server.ws.WebService.Param.PAGE; -import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; -import static org.sonar.api.server.ws.WebService.Param.SELECTED; -import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; -import static org.sonar.server.permission.ws.PermissionRequestValidator.validateGlobalPermission; -import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN; - -class PermissionRequest { - private final String permission; - private final String userLogin; - private final WsGroupRef group; - private final Optional<WsProjectRef> project; - private final Integer page; - private final Integer pageSize; - private final String selected; - private final String query; - - private PermissionRequest(Builder builder) { - permission = builder.permission; - userLogin = builder.userLogin; - group = builder.group; - project = builder.project; - page = builder.page; - pageSize = builder.pageSize; - selected = builder.selected; - query = builder.query; - } - - public static class Builder { - - private final Request request; - - private boolean withUser; - private boolean withGroup; - private boolean withPagination; - - private String permission; - private String userLogin; - - private WsGroupRef group; - private Optional<WsProjectRef> project; - private Integer page; - private Integer pageSize; - private String selected; - private String query; - - public Builder(Request request) { - this.request = request; - } - - PermissionRequest build() { - permission = request.mandatoryParam(PARAM_PERMISSION); - setUserLogin(request); - setGroup(request); - setProject(request); - setPaging(request); - setSelected(request); - setQuery(request); - checkPermissionParameter(); - - return new PermissionRequest(this); - } - - public Builder withUser() { - this.withUser = true; - return this; - } - - public Builder withGroup() { - this.withGroup = true; - return this; - } - - public Builder withPagination() { - this.withPagination = true; - return this; - } - - private void setQuery(Request request) { - if (request.hasParam(TEXT_QUERY)) { - query = request.param(TEXT_QUERY); - if (query != null) { - selected = SelectionMode.ALL.value(); - } - } - } - - private void setSelected(Request request) { - if (request.hasParam(SELECTED)) { - selected = request.mandatoryParam(SELECTED); - } - } - - private void setPaging(Request request) { - if (withPagination) { - page = request.mandatoryParamAsInt(PAGE); - pageSize = request.mandatoryParamAsInt(PAGE_SIZE); - } - } - - private void setUserLogin(Request request) { - if (withUser) { - userLogin = request.mandatoryParam(PARAM_USER_LOGIN); - } - } - - private void setGroup(Request request) { - if (withGroup) { - this.group = WsGroupRef.newWsGroupRefFromPermissionRequest(request); - } - } - - private void setProject(Request request) { - this.project = WsProjectRef.newOptionalWsProjectRef(request); - } - - private void checkPermissionParameter() { - if (project.isPresent()) { - validateProjectPermission(permission); - } else { - validateGlobalPermission(permission); - } - } - } - - public String permission() { - return permission; - } - - public String userLogin() { - return userLogin; - } - - public WsGroupRef group() { - return group; - } - - public Optional<WsProjectRef> project() { - return project; - } - - public Integer page() { - return page; - } - - public Integer pageSize() { - return pageSize; - } - - public String selected() { - return selected; - } - - public String query() { - return query; - } -} 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 731ee73888b..5d61994d0a2 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 @@ -42,6 +42,7 @@ import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobal import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentKey; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentUuid; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameter; +import static org.sonar.server.permission.ws.WsProjectRef.newOptionalWsProjectRef; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY; @@ -78,12 +79,12 @@ public class SearchProjectPermissionsAction implements PermissionsWsAction { @Override public void handle(Request wsRequest, Response wsResponse) throws Exception { - checkRequestAndPermissions(wsRequest); SearchProjectPermissionsWsResponse searchProjectPermissionsWsResponse = doHandle(toSearchProjectPermissionsWsRequest(wsRequest)); writeProtobuf(searchProjectPermissionsWsResponse, wsRequest, wsResponse); } private SearchProjectPermissionsWsResponse doHandle(SearchProjectPermissionsWsRequest request) { + checkRequestAndPermissions(request); DbSession dbSession = dbClient.openSession(false); try { SearchProjectPermissionsData data = dataLoader.load(request); @@ -102,8 +103,8 @@ public class SearchProjectPermissionsAction implements PermissionsWsAction { .setQuery(request.param(Param.TEXT_QUERY)); } - private void checkRequestAndPermissions(Request wsRequest) { - Optional<WsProjectRef> project = WsProjectRef.newOptionalWsProjectRef(wsRequest); + private void checkRequestAndPermissions(SearchProjectPermissionsWsRequest request) { + Optional<WsProjectRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()); boolean hasProject = project.isPresent(); boolean hasProjectUuid = hasProject && project.get().uuid() != null; boolean hasProjectKey = hasProject && project.get().key() != null; diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java index 0a2dc2669a6..420d708044f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/TemplateUsersAction.java @@ -32,8 +32,9 @@ import org.sonar.db.permission.PermissionQuery; import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.permission.UserWithPermissionDto; import org.sonar.server.user.UserSession; +import org.sonarqube.ws.WsPermissions; import org.sonarqube.ws.WsPermissions.User; -import org.sonarqube.ws.WsPermissions.WsUsersResponse; +import org.sonarqube.ws.WsPermissions.UsersWsResponse; import static java.lang.String.format; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; @@ -86,7 +87,7 @@ public class TemplateUsersAction implements PermissionsWsAction { PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, templateRef); PermissionQuery query = buildQuery(wsRequest, template); - WsUsersResponse templateUsersResponse = buildResponse(dbSession, query, template); + WsPermissions.UsersWsResponse templateUsersResponse = buildResponse(dbSession, query, template); writeProtobuf(templateUsersResponse, wsRequest, wsResponse); } finally { dbClient.closeSession(dbSession); @@ -106,11 +107,11 @@ public class TemplateUsersAction implements PermissionsWsAction { .build(); } - private WsUsersResponse buildResponse(DbSession dbSession, PermissionQuery query, PermissionTemplateDto template) { + private WsPermissions.UsersWsResponse buildResponse(DbSession dbSession, PermissionQuery query, PermissionTemplateDto template) { List<UserWithPermissionDto> usersWithPermission = dbClient.permissionTemplateDao().selectUsers(dbSession, query, template.getId(), query.pageOffset(), query.pageSize()); int total = dbClient.permissionTemplateDao().countUsers(dbSession, query, template.getId()); - WsUsersResponse.Builder responseBuilder = WsUsersResponse.newBuilder(); + UsersWsResponse.Builder responseBuilder = UsersWsResponse.newBuilder(); for (UserWithPermissionDto userWithPermission : usersWithPermission) { responseBuilder.addUsers(userDtoToUserResponse(userWithPermission)); } 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 f1462147476..f92c508e800 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 @@ -34,19 +34,24 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.permission.PermissionQuery; import org.sonar.db.permission.UserWithPermissionDto; import org.sonar.server.permission.PermissionFinder; -import org.sonar.server.permission.ws.PermissionRequest.Builder; import org.sonar.server.user.UserSession; import org.sonarqube.ws.WsPermissions; -import org.sonarqube.ws.WsPermissions.WsUsersResponse; +import org.sonarqube.ws.WsPermissions.UsersWsResponse; +import org.sonarqube.ws.client.permission.UsersWsRequest; import static com.google.common.base.Objects.firstNonNull; import static com.google.common.base.Strings.nullToEmpty; import static org.sonar.api.utils.Paging.forPageIndex; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentDto; import static org.sonar.server.permission.ws.PermissionQueryParser.fromSelectionModeToMembership; +import static org.sonar.server.permission.ws.PermissionRequestValidator.validatePermission; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameter; +import static org.sonar.server.permission.ws.WsProjectRef.newOptionalWsProjectRef; import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY; public class UsersAction implements PermissionsWsAction { @@ -84,25 +89,40 @@ public class UsersAction implements PermissionsWsAction { @Override public void handle(Request wsRequest, Response wsResponse) throws Exception { + UsersWsResponse usersWsResponse = doHandle(toUsersWsRequest(wsRequest)); + writeProtobuf(usersWsResponse, wsRequest, wsResponse); + } + + private UsersWsResponse doHandle(UsersWsRequest request) { + Optional<WsProjectRef> wsProjectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()); + validatePermission(request.getPermission(), wsProjectRef); DbSession dbSession = dbClient.openSession(false); try { - PermissionRequest request = new Builder(wsRequest).withPagination().build(); - Optional<ComponentDto> project = dependenciesFinder.searchProject(dbSession, request.project()); + Optional<ComponentDto> project = dependenciesFinder.searchProject(dbSession, wsProjectRef); checkProjectAdminUserByComponentDto(userSession, project); PermissionQuery permissionQuery = buildPermissionQuery(request, project); Long projectIdIfPresent = project.isPresent() ? project.get().getId() : null; int total = dbClient.permissionDao().countUsers(dbSession, permissionQuery, projectIdIfPresent); List<UserWithPermissionDto> usersWithPermission = permissionFinder.findUsersWithPermission(dbSession, permissionQuery); - WsUsersResponse wsUsersResponse = buildResponse(usersWithPermission, forPageIndex(request.page()).withPageSize(request.pageSize()).andTotal(total)); - - writeProtobuf(wsUsersResponse, wsRequest, wsResponse); + return buildResponse(usersWithPermission, forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal(total)); } finally { dbClient.closeSession(dbSession); } } - private static WsUsersResponse buildResponse(List<UserWithPermissionDto> usersWithPermission, Paging paging) { - WsUsersResponse.Builder userResponse = WsUsersResponse.newBuilder(); + private static UsersWsRequest toUsersWsRequest(Request request) { + return new UsersWsRequest() + .setPermission(request.mandatoryParam(PARAM_PERMISSION)) + .setProjectId(request.param(PARAM_PROJECT_ID)) + .setProjectKey(request.param(PARAM_PROJECT_KEY)) + .setSelected(request.param(Param.SELECTED)) + .setQuery(request.param(Param.TEXT_QUERY)) + .setPage(request.mandatoryParamAsInt(Param.PAGE)) + .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)); + } + + private static UsersWsResponse buildResponse(List<UserWithPermissionDto> usersWithPermission, Paging paging) { + UsersWsResponse.Builder userResponse = UsersWsResponse.newBuilder(); WsPermissions.User.Builder user = WsPermissions.User.newBuilder(); for (UserWithPermissionDto userWithPermission : usersWithPermission) { userResponse.addUsers( @@ -124,13 +144,13 @@ public class UsersAction implements PermissionsWsAction { return userResponse.build(); } - private static PermissionQuery buildPermissionQuery(PermissionRequest request, Optional<ComponentDto> project) { + private static PermissionQuery buildPermissionQuery(UsersWsRequest request, Optional<ComponentDto> project) { PermissionQuery.Builder permissionQuery = PermissionQuery.builder() - .permission(request.permission()) - .pageIndex(request.page()) - .pageSize(request.pageSize()) - .membership(fromSelectionModeToMembership(firstNonNull(request.selected(), SelectionMode.SELECTED.value()))) - .search(request.query()); + .permission(request.getPermission()) + .pageIndex(request.getPage()) + .pageSize(request.getPageSize()) + .membership(fromSelectionModeToMembership(firstNonNull(request.getSelected(), SelectionMode.SELECTED.value()))) + .search(request.getQuery()); if (project.isPresent()) { permissionQuery.component(project.get().getKey()); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/WsProjectRef.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/WsProjectRef.java index 6384e26b997..5b8f4178410 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/WsProjectRef.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/WsProjectRef.java @@ -23,11 +23,8 @@ package org.sonar.server.permission.ws; import com.google.common.base.Optional; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import org.sonar.api.server.ws.Request; import static org.sonar.server.ws.WsUtils.checkRequest; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY; /** * Project identifiers from a WS request. Guaranties the project id and project key are not provided at the same time. @@ -43,18 +40,6 @@ public class WsProjectRef { checkRequest(this.uuid != null ^ this.key != null, "Project id or project key can be provided, not both."); } - private WsProjectRef(Request wsRequest) { - this(wsRequest.param(PARAM_PROJECT_ID), wsRequest.param(PARAM_PROJECT_KEY)); - } - - public static Optional<WsProjectRef> newOptionalWsProjectRef(Request wsRequest) { - if (!hasProjectParam(wsRequest)) { - return Optional.absent(); - } - - return Optional.of(new WsProjectRef(wsRequest)); - } - public static Optional<WsProjectRef> newOptionalWsProjectRef(@Nullable String uuid, @Nullable String key) { if (uuid == null && key == null) { return Optional.absent(); @@ -63,12 +48,6 @@ public class WsProjectRef { return Optional.of(new WsProjectRef(uuid, key)); } - public static WsProjectRef newWsProjectRef(Request wsRequest) { - checkRequest(hasProjectParam(wsRequest), MSG_ID_OR_KEY_MUST_BE_PROVIDED); - - return new WsProjectRef(wsRequest); - } - public static WsProjectRef newWsProjectRef(@Nullable String uuid, @Nullable String key) { checkRequest(uuid == null ^ key == null, MSG_ID_OR_KEY_MUST_BE_PROVIDED); return new WsProjectRef(uuid, key); @@ -83,8 +62,4 @@ public class WsProjectRef { public String key() { return this.key; } - - private static boolean hasProjectParam(Request wsRequest) { - return wsRequest.hasParam(PARAM_PROJECT_ID) || wsRequest.hasParam(PARAM_PROJECT_KEY); - } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java index 134a26555aa..949ab12b8ed 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java @@ -50,7 +50,8 @@ import org.sonar.server.usergroups.ws.UserGroupFinder; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; import org.sonar.test.DbTests; -import org.sonarqube.ws.WsPermissions.WsUsersResponse; +import org.sonarqube.ws.WsPermissions; +import org.sonarqube.ws.WsPermissions.UsersWsResponse; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.api.web.UserRole.ADMIN; @@ -58,7 +59,7 @@ import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTem import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateUserDto; import static org.sonarqube.ws.MediaTypes.PROTOBUF; import static org.sonar.test.JsonAssert.assertJson; -import static org.sonarqube.ws.WsPermissions.WsUsersResponse.parseFrom; +import static org.sonarqube.ws.WsPermissions.UsersWsResponse.parseFrom; @Category(DbTests.class) public class TemplateUsersActionTest { @@ -126,7 +127,7 @@ public class TemplateUsersActionTest { .setMediaType(PROTOBUF) .execute().getInputStream(); - WsUsersResponse response = parseFrom(responseStream); + UsersWsResponse response = parseFrom(responseStream); assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2"); } @@ -139,7 +140,7 @@ public class TemplateUsersActionTest { .setMediaType(PROTOBUF) .execute().getInputStream(); - WsUsersResponse response = parseFrom(responseStream); + UsersWsResponse response = parseFrom(responseStream); assertThat(response.getUsersList()).extracting("login").containsOnly("login-1"); } @@ -152,7 +153,7 @@ public class TemplateUsersActionTest { .setMediaType(PROTOBUF) .execute().getInputStream(); - WsUsersResponse response = parseFrom(responseStream); + WsPermissions.UsersWsResponse response = parseFrom(responseStream); assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2", "login-3"); assertThat(response.getUsers(2).getSelected()).isFalse(); @@ -168,7 +169,7 @@ public class TemplateUsersActionTest { .setMediaType(PROTOBUF) .execute().getInputStream(); - WsUsersResponse response = parseFrom(responseStream); + WsPermissions.UsersWsResponse response = parseFrom(responseStream); assertThat(response.getUsersList()).extracting("login").containsOnly("login-2"); } |