From cf507c746df0fe8ceb3a1db2f1f4d0a4718f0ceb Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Mon, 4 Jul 2016 15:49:50 +0200 Subject: [PATCH] SONAR-7835 Update WS API of permissions/users --- .../ws/PermissionsWsParametersBuilder.java | 5 +- .../server/permission/ws/UsersAction.java | 29 +++--- .../ws/client/permission/UsersWsRequest.java | 92 +++++++++++++++++++ 3 files changed, 107 insertions(+), 19 deletions(-) create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UsersWsRequest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsParametersBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsParametersBuilder.java index e4b8dee414d..4acfc3b4513 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsParametersBuilder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsParametersBuilder.java @@ -20,6 +20,7 @@ package org.sonar.server.permission.ws; import org.sonar.api.server.ws.WebService.NewAction; +import org.sonar.api.server.ws.WebService.NewParam; import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.permission.ProjectPermissions; import org.sonar.core.util.Uuids; @@ -63,8 +64,8 @@ public class PermissionsWsParametersBuilder { .setRequired(true); } - public static void createProjectPermissionParameter(NewAction action) { - action.createParam(PARAM_PERMISSION) + public static NewParam createProjectPermissionParameter(NewAction action) { + return action.createParam(PARAM_PERMISSION) .setDescription(PROJECT_PERMISSION_PARAM_DESCRIPTION) .setRequired(true); } 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 2dff7435242..40f50538e05 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 @@ -25,7 +25,6 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; -import org.sonar.api.server.ws.WebService.SelectionMode; import org.sonar.api.utils.Paging; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -36,13 +35,11 @@ import org.sonar.server.permission.PermissionFinder; import org.sonar.server.user.UserSession; import org.sonarqube.ws.WsPermissions; import org.sonarqube.ws.WsPermissions.UsersWsResponse; -import org.sonarqube.ws.client.permission.OldUsersWsRequest; +import org.sonarqube.ws.client.permission.UsersWsRequest; -import static com.google.common.base.MoreObjects.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.createProjectParameters; @@ -54,6 +51,8 @@ import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_P public class UsersAction implements PermissionsWsAction { + private static final int MAX_SIZE = 100; + private final DbClient dbClient; private final UserSession userSession; private final PermissionFinder permissionFinder; @@ -70,14 +69,12 @@ public class UsersAction implements PermissionsWsAction { public void define(WebService.NewController context) { WebService.NewAction action = context.createAction("users2") .setSince("5.2") - .setDescription(String.format("Lists the users that have been granted the specified permission as individual users rather than through group affiliation.
" + - "This service defaults to global permissions, but can be limited to project permissions by providing project id or project key.
" + - "If the query parameter '%s' is specified, the '%s' parameter is forced to '%s'.
" + - "It requires administration permissions to access.
", - Param.TEXT_QUERY, Param.SELECTED, SelectionMode.ALL.value())) - .addPagingParams(100) + .setDescription("Lists the users with their permissions as individual users rather than through group affiliation.
" + + "This service defaults to global permissions, but can be limited to project permissions by providing project id or project key.
" + + "This service defaults to all users, but can be limited to users with a specific permission by providing the desired permission.
" + + "It requires administration permissions to access.") + .addPagingParams(20, MAX_SIZE) .addSearchQuery("stas", "names") - .addSelectionModeParam() .setInternal(true) .setResponseExample(getClass().getResource("users-example.json")) .setHandler(this); @@ -92,7 +89,7 @@ public class UsersAction implements PermissionsWsAction { writeProtobuf(usersWsResponse, wsRequest, wsResponse); } - private UsersWsResponse doHandle(OldUsersWsRequest request) { + private UsersWsResponse doHandle(UsersWsRequest request) { Optional wsProjectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()); validatePermission(request.getPermission(), wsProjectRef); DbSession dbSession = dbClient.openSession(false); @@ -109,12 +106,11 @@ public class UsersAction implements PermissionsWsAction { } } - private static OldUsersWsRequest toUsersWsRequest(Request request) { - return new OldUsersWsRequest() + 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)); @@ -143,12 +139,11 @@ public class UsersAction implements PermissionsWsAction { return userResponse.build(); } - private static PermissionQuery buildPermissionQuery(OldUsersWsRequest request, Optional project) { + private static PermissionQuery buildPermissionQuery(UsersWsRequest request, Optional project) { PermissionQuery.Builder permissionQuery = PermissionQuery.builder() .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/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UsersWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UsersWsRequest.java new file mode 100644 index 00000000000..390e6c49ee7 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UsersWsRequest.java @@ -0,0 +1,92 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.sonarqube.ws.client.permission; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +public class UsersWsRequest { + private String permission; + private String projectId; + private String projectKey; + private String query; + private Integer page; + private Integer pageSize; + + @CheckForNull + public String getPermission() { + return permission; + } + + public UsersWsRequest setPermission(@Nullable String permission) { + this.permission = permission; + return this; + } + + @CheckForNull + public String getProjectId() { + return projectId; + } + + public UsersWsRequest setProjectId(@Nullable String projectId) { + this.projectId = projectId; + return this; + } + + @CheckForNull + public String getProjectKey() { + return projectKey; + } + + public UsersWsRequest setProjectKey(@Nullable String projectKey) { + this.projectKey = projectKey; + return this; + } + + @CheckForNull + public String getQuery() { + return query; + } + + public UsersWsRequest setQuery(@Nullable String query) { + this.query = query; + return this; + } + + @CheckForNull + public Integer getPage() { + return page; + } + + public UsersWsRequest setPage(int page) { + this.page = page; + return this; + } + + @CheckForNull + public Integer getPageSize() { + return pageSize; + } + + public UsersWsRequest setPageSize(int pageSize) { + this.pageSize = pageSize; + return this; + } +} -- 2.39.5