From f92a18e60d3f94aa0ac09bd529a1d21c2eaeadf3 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Mon, 4 Jul 2016 15:38:09 +0200 Subject: [PATCH] SONAR-7835 Rename UsersAction to OldUsersAction --- .../authorisation/PermissionSearchTest.java | 4 +- .../server/permission/ws/OldUsersAction.java | 159 +++++++++++++ .../permission/ws/PermissionsWsModule.java | 2 +- .../permission/ws/TemplateUsersAction.java | 16 +- .../server/permission/ws/UsersAction.java | 12 +- .../permission/ws/old-users-example.json | 22 ++ .../permission/ws/OldUsersActionTest.java | 218 ++++++++++++++++++ ...sWsRequest.java => OldUsersWsRequest.java} | 16 +- .../client/permission/PermissionsService.java | 2 +- .../src/main/protobuf/ws-permissions.proto | 14 ++ .../permission/PermissionsServiceTest.java | 2 +- 11 files changed, 440 insertions(+), 27 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/permission/ws/OldUsersAction.java create mode 100644 server/sonar-server/src/main/resources/org/sonar/server/permission/ws/old-users-example.json create mode 100644 server/sonar-server/src/test/java/org/sonar/server/permission/ws/OldUsersActionTest.java rename sonar-ws/src/main/java/org/sonarqube/ws/client/permission/{UsersWsRequest.java => OldUsersWsRequest.java} (81%) diff --git a/it/it-tests/src/test/java/it/authorisation/PermissionSearchTest.java b/it/it-tests/src/test/java/it/authorisation/PermissionSearchTest.java index bf9c454777b..24e0fede38b 100644 --- a/it/it-tests/src/test/java/it/authorisation/PermissionSearchTest.java +++ b/it/it-tests/src/test/java/it/authorisation/PermissionSearchTest.java @@ -45,7 +45,7 @@ import org.sonarqube.ws.client.permission.RemoveGroupFromTemplateWsRequest; import org.sonarqube.ws.client.permission.RemoveProjectCreatorFromTemplateWsRequest; import org.sonarqube.ws.client.permission.RemoveUserFromTemplateWsRequest; import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest; -import org.sonarqube.ws.client.permission.UsersWsRequest; +import org.sonarqube.ws.client.permission.OldUsersWsRequest; import util.QaOnly; import static org.assertj.core.api.Assertions.assertThat; @@ -106,7 +106,7 @@ public class PermissionSearchTest { assertThat(searchGlobalPermissionsWsResponse.getPermissionsList().get(0).getGroupsCount()).isEqualTo(2); WsPermissions.UsersWsResponse users = permissionsWsClient - .users(new UsersWsRequest() + .users(new OldUsersWsRequest() .setPermission("admin")); assertThat(users.getUsersList()).extracting("login").contains(LOGIN); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/OldUsersAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/OldUsersAction.java new file mode 100644 index 00000000000..830964e8993 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/OldUsersAction.java @@ -0,0 +1,159 @@ +/* + * 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.sonar.server.permission.ws; + +import com.google.common.base.Optional; +import java.util.List; +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; +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.user.UserSession; +import org.sonarqube.ws.WsPermissions; +import org.sonarqube.ws.WsPermissions.OldUsersWsResponse; +import org.sonarqube.ws.client.permission.OldUsersWsRequest; + +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; +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 OldUsersAction implements PermissionsWsAction { + + private final DbClient dbClient; + private final UserSession userSession; + private final PermissionFinder permissionFinder; + private final PermissionDependenciesFinder dependenciesFinder; + + public OldUsersAction(DbClient dbClient, UserSession userSession, PermissionFinder permissionFinder, PermissionDependenciesFinder dependenciesFinder) { + this.dbClient = dbClient; + this.userSession = userSession; + this.permissionFinder = permissionFinder; + this.dependenciesFinder = dependenciesFinder; + } + + @Override + public void define(WebService.NewController context) { + WebService.NewAction action = context.createAction("users") + .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) + .addSearchQuery("stas", "names") + .addSelectionModeParam() + .setInternal(true) + .setResponseExample(getClass().getResource("old-users-example.json")) + .setHandler(this); + + createPermissionParameter(action); + createProjectParameters(action); + } + + @Override + public void handle(Request wsRequest, Response wsResponse) throws Exception { + OldUsersWsResponse usersWsResponse = doHandle(toUsersWsRequest(wsRequest)); + writeProtobuf(usersWsResponse, wsRequest, wsResponse); + } + + private OldUsersWsResponse doHandle(OldUsersWsRequest request) { + Optional wsProjectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()); + validatePermission(request.getPermission(), wsProjectRef); + DbSession dbSession = dbClient.openSession(false); + try { + Optional 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 usersWithPermission = permissionFinder.findUsersWithPermission(dbSession, permissionQuery); + return buildResponse(usersWithPermission, forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal(total)); + } finally { + dbClient.closeSession(dbSession); + } + } + + private static OldUsersWsRequest toUsersWsRequest(Request request) { + return new OldUsersWsRequest() + .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 OldUsersWsResponse buildResponse(List usersWithPermission, Paging paging) { + OldUsersWsResponse.Builder userResponse = OldUsersWsResponse.newBuilder(); + WsPermissions.OldUser.Builder user = WsPermissions.OldUser.newBuilder(); + for (UserWithPermissionDto userWithPermission : usersWithPermission) { + userResponse.addUsers( + user + .clear() + .setLogin(userWithPermission.getLogin()) + .setName(nullToEmpty(userWithPermission.getName())) + .setEmail(nullToEmpty(userWithPermission.getEmail())) + .setSelected(userWithPermission.getPermission() != null)); + } + + userResponse.getPagingBuilder() + .clear() + .setPageIndex(paging.pageIndex()) + .setPageSize(paging.pageSize()) + .setTotal(paging.total()) + .build(); + + return userResponse.build(); + } + + private static PermissionQuery buildPermissionQuery(OldUsersWsRequest 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()); + } + + return permissionQuery.build(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsModule.java index 7a74309f47e..c4fe7ff6e08 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsModule.java @@ -45,7 +45,7 @@ public class PermissionsWsModule extends Module { AddUserAction.class, RemoveGroupAction.class, RemoveUserAction.class, - UsersAction.class, + OldUsersAction.class, GroupsAction.class, SearchGlobalPermissionsAction.class, SearchProjectPermissionsAction.class, 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 47428ce121f..bd63a924e07 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,17 +32,17 @@ 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.UsersWsResponse; +import org.sonarqube.ws.WsPermissions.OldUser; +import org.sonarqube.ws.WsPermissions.OldUsersWsResponse; import static java.lang.String.format; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; import static org.sonar.server.permission.ws.PermissionQueryParser.fromSelectionModeToMembership; import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters; import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; public class TemplateUsersAction implements PermissionsWsAction { @@ -86,7 +86,7 @@ public class TemplateUsersAction implements PermissionsWsAction { PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, templateRef); PermissionQuery query = buildQuery(wsRequest, template); - WsPermissions.UsersWsResponse templateUsersResponse = buildResponse(dbSession, query, template); + WsPermissions.OldUsersWsResponse templateUsersResponse = buildResponse(dbSession, query, template); writeProtobuf(templateUsersResponse, wsRequest, wsResponse); } finally { dbClient.closeSession(dbSession); @@ -106,11 +106,11 @@ public class TemplateUsersAction implements PermissionsWsAction { .build(); } - private WsPermissions.UsersWsResponse buildResponse(DbSession dbSession, PermissionQuery query, PermissionTemplateDto template) { + private OldUsersWsResponse buildResponse(DbSession dbSession, PermissionQuery query, PermissionTemplateDto template) { List usersWithPermission = dbClient.permissionTemplateDao().selectUsers(dbSession, query, template.getId(), query.pageOffset(), query.pageSize()); int total = dbClient.permissionTemplateDao().countUsers(dbSession, query, template.getId()); - UsersWsResponse.Builder responseBuilder = UsersWsResponse.newBuilder(); + OldUsersWsResponse.Builder responseBuilder = OldUsersWsResponse.newBuilder(); for (UserWithPermissionDto userWithPermission : usersWithPermission) { responseBuilder.addUsers(userDtoToUserResponse(userWithPermission)); } @@ -124,8 +124,8 @@ public class TemplateUsersAction implements PermissionsWsAction { return responseBuilder.build(); } - private static User userDtoToUserResponse(UserWithPermissionDto userWithPermission) { - User.Builder userBuilder = User.newBuilder(); + private static OldUser userDtoToUserResponse(UserWithPermissionDto userWithPermission) { + OldUser.Builder userBuilder = OldUser.newBuilder(); userBuilder.setLogin(userWithPermission.getLogin()); String email = userWithPermission.getEmail(); if (email != null) { 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 1e8303d7b54..2dff7435242 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 @@ -36,7 +36,7 @@ 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.UsersWsRequest; +import org.sonarqube.ws.client.permission.OldUsersWsRequest; import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Strings.nullToEmpty; @@ -68,7 +68,7 @@ public class UsersAction implements PermissionsWsAction { @Override public void define(WebService.NewController context) { - WebService.NewAction action = context.createAction("users") + 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.
" + @@ -92,7 +92,7 @@ public class UsersAction implements PermissionsWsAction { writeProtobuf(usersWsResponse, wsRequest, wsResponse); } - private UsersWsResponse doHandle(UsersWsRequest request) { + private UsersWsResponse doHandle(OldUsersWsRequest request) { Optional wsProjectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()); validatePermission(request.getPermission(), wsProjectRef); DbSession dbSession = dbClient.openSession(false); @@ -109,8 +109,8 @@ public class UsersAction implements PermissionsWsAction { } } - private static UsersWsRequest toUsersWsRequest(Request request) { - return new UsersWsRequest() + private static OldUsersWsRequest toUsersWsRequest(Request request) { + return new OldUsersWsRequest() .setPermission(request.mandatoryParam(PARAM_PERMISSION)) .setProjectId(request.param(PARAM_PROJECT_ID)) .setProjectKey(request.param(PARAM_PROJECT_KEY)) @@ -143,7 +143,7 @@ public class UsersAction implements PermissionsWsAction { return userResponse.build(); } - private static PermissionQuery buildPermissionQuery(UsersWsRequest request, Optional project) { + private static PermissionQuery buildPermissionQuery(OldUsersWsRequest request, Optional project) { PermissionQuery.Builder permissionQuery = PermissionQuery.builder() .permission(request.getPermission()) .pageIndex(request.getPage()) diff --git a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/old-users-example.json b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/old-users-example.json new file mode 100644 index 00000000000..5f13cd53286 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/old-users-example.json @@ -0,0 +1,22 @@ +{ + "users": [ + { + "login": "admin", + "name": "Administrator", + "email": "admin@admin.com", + "selected": true + }, + { + "login": "george.orwell", + "name": "George Orwell", + "email": "george.orwell@1984.net", + "selected": true + } + ], + "paging": { + "pageSize": 100, + "total": 2, + "pageIndex": 1 + } +} + diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/OldUsersActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/OldUsersActionTest.java new file mode 100644 index 00000000000..25059e6fdfc --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/OldUsersActionTest.java @@ -0,0 +1,218 @@ +/* + * 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.sonar.server.permission.ws; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.server.ws.WebService.Param; +import org.sonar.api.server.ws.WebService.SelectionMode; +import org.sonar.api.utils.System2; +import org.sonar.api.web.UserRole; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ResourceTypesRule; +import org.sonar.db.user.UserDto; +import org.sonar.db.user.UserRoleDto; +import org.sonar.server.component.ComponentFinder; +import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.ForbiddenException; +import org.sonar.server.exceptions.UnauthorizedException; +import org.sonar.server.permission.PermissionFinder; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.usergroups.ws.UserGroupFinder; +import org.sonar.server.ws.WsActionTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.web.UserRole.ISSUE_ADMIN; +import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; +import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; +import static org.sonar.db.component.ComponentTesting.newProjectDto; +import static org.sonar.db.user.UserTesting.newUserDto; +import static org.sonar.test.JsonAssert.assertJson; +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 OldUsersActionTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Rule + public UserSessionRule userSession = UserSessionRule.standalone(); + + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); + WsActionTester ws; + OldUsersAction underTest; + + @Before + public void setUp() { + PermissionFinder permissionFinder = new PermissionFinder(dbClient); + PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes); + underTest = new OldUsersAction(dbClient, userSession, permissionFinder, dependenciesFinder); + ws = new WsActionTester(underTest); + + userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN); + } + + @Test + public void search_for_users_with_response_example() { + UserDto user1 = insertUser(new UserDto().setLogin("admin").setName("Administrator").setEmail("admin@admin.com")); + UserDto user2 = insertUser(new UserDto().setLogin("george.orwell").setName("George Orwell").setEmail("george.orwell@1984.net")); + insertUserRole(new UserRoleDto().setRole(SCAN_EXECUTION).setUserId(user1.getId())); + insertUserRole(new UserRoleDto().setRole(SCAN_EXECUTION).setUserId(user2.getId())); + dbSession.commit(); + + String result = ws.newRequest().setParam("permission", "scan").execute().getInput(); + + assertJson(result).isSimilarTo(getClass().getResource("users-example.json")); + } + + @Test + public void search_for_users_with_one_permission() { + insertUsers(); + String result = ws.newRequest().setParam("permission", "scan").execute().getInput(); + + assertJson(result).isSimilarTo(getClass().getResource("UsersActionTest/users.json")); + } + + @Test + public void search_for_users_with_permission_on_project() { + dbClient.componentDao().insert(dbSession, newProjectDto("project-uuid").setKey("project-key")); + ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, "project-uuid"); + UserDto user = insertUser(newUserDto().setLogin("project-user-login").setName("project-user-name")); + insertUserRole(new UserRoleDto().setRole(ISSUE_ADMIN).setUserId(user.getId()).setResourceId(project.getId())); + dbSession.commit(); + userSession.login().addProjectUuidPermissions(SYSTEM_ADMIN, "project-uuid"); + + String result = ws.newRequest() + .setParam(PARAM_PERMISSION, ISSUE_ADMIN) + .setParam(PARAM_PROJECT_ID, "project-uuid") + .execute().getInput(); + + assertThat(result).contains("project-user-login") + .doesNotContain("login-1"); + } + + @Test + public void search_for_users_with_query_as_a_parameter() { + insertUsers(); + String result = ws.newRequest() + .setParam("permission", "scan") + .setParam(Param.TEXT_QUERY, "ame-1") + .execute().getInput(); + + assertThat(result).contains("login-1") + .doesNotContain("login-2") + .doesNotContain("login-3"); + } + + @Test + public void search_for_users_with_select_as_a_parameter() { + insertUsers(); + String result = ws.newRequest() + .setParam("permission", "scan") + .setParam(Param.SELECTED, SelectionMode.ALL.value()) + .execute().getInput(); + + assertThat(result).contains("login-1", "login-2", "login-3"); + } + + @Test + public void fail_if_project_permission_without_project() { + expectedException.expect(BadRequestException.class); + + ws.newRequest() + .setParam(PARAM_PERMISSION, UserRole.ISSUE_ADMIN) + .setParam(Param.SELECTED, SelectionMode.ALL.value()) + .execute(); + } + + @Test + public void fail_if_permission_parameter_is_not_filled() { + expectedException.expect(IllegalArgumentException.class); + + ws.newRequest().execute(); + } + + @Test + public void fail_if_insufficient_privileges() { + expectedException.expect(ForbiddenException.class); + userSession.login("login"); + + ws.newRequest() + .setParam("permission", SYSTEM_ADMIN) + .execute(); + } + + @Test + public void fail_if_not_logged_in() { + expectedException.expect(UnauthorizedException.class); + userSession.anonymous(); + + ws.newRequest() + .setParam("permission", SYSTEM_ADMIN) + .execute(); + } + + @Test + public void fail_if_project_uuid_and_project_key_are_provided() { + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("Project id or project key can be provided, not both."); + dbClient.componentDao().insert(dbSession, newProjectDto("project-uuid").setKey("project-key")); + dbSession.commit(); + + ws.newRequest() + .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) + .setParam(PARAM_PROJECT_ID, "project-uuid") + .setParam(PARAM_PROJECT_KEY, "project-key") + .execute(); + } + + private UserDto insertUser(UserDto userDto) { + UserDto user = dbClient.userDao().insert(dbSession, userDto.setActive(true)); + dbSession.commit(); + return user; + } + + private void insertUserRole(UserRoleDto userRoleDto) { + dbClient.roleDao().insertUserRole(dbSession, userRoleDto); + dbSession.commit(); + } + + private void insertUsers() { + UserDto user1 = insertUser(new UserDto().setLogin("login-1").setName("name-1").setEmail("email-1")); + UserDto user2 = insertUser(new UserDto().setLogin("login-2").setName("name-2").setEmail("email-2")); + UserDto user3 = insertUser(new UserDto().setLogin("login-3").setName("name-3").setEmail("email-3")); + insertUserRole(new UserRoleDto().setRole(SCAN_EXECUTION).setUserId(user1.getId())); + insertUserRole(new UserRoleDto().setRole(SCAN_EXECUTION).setUserId(user2.getId())); + insertUserRole(new UserRoleDto().setRole(SYSTEM_ADMIN).setUserId(user3.getId())); + dbSession.commit(); + } +} 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/OldUsersWsRequest.java similarity index 81% rename from sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UsersWsRequest.java rename to sonar-ws/src/main/java/org/sonarqube/ws/client/permission/OldUsersWsRequest.java index 2abe8177969..d17f32d54e8 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UsersWsRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/OldUsersWsRequest.java @@ -24,7 +24,7 @@ import javax.annotation.Nullable; import static java.util.Objects.requireNonNull; -public class UsersWsRequest { +public class OldUsersWsRequest { private String permission; private String projectId; private String projectKey; @@ -37,7 +37,7 @@ public class UsersWsRequest { return permission; } - public UsersWsRequest setPermission(String permission) { + public OldUsersWsRequest setPermission(String permission) { this.permission = requireNonNull(permission); return this; } @@ -47,7 +47,7 @@ public class UsersWsRequest { return projectId; } - public UsersWsRequest setProjectId(@Nullable String projectId) { + public OldUsersWsRequest setProjectId(@Nullable String projectId) { this.projectId = projectId; return this; } @@ -57,7 +57,7 @@ public class UsersWsRequest { return projectKey; } - public UsersWsRequest setProjectKey(@Nullable String projectKey) { + public OldUsersWsRequest setProjectKey(@Nullable String projectKey) { this.projectKey = projectKey; return this; } @@ -67,7 +67,7 @@ public class UsersWsRequest { return selected; } - public UsersWsRequest setSelected(@Nullable String selected) { + public OldUsersWsRequest setSelected(@Nullable String selected) { this.selected = selected; return this; } @@ -77,7 +77,7 @@ public class UsersWsRequest { return query; } - public UsersWsRequest setQuery(@Nullable String query) { + public OldUsersWsRequest setQuery(@Nullable String query) { this.query = query; return this; } @@ -87,7 +87,7 @@ public class UsersWsRequest { return page; } - public UsersWsRequest setPage(int page) { + public OldUsersWsRequest setPage(int page) { this.page = page; return this; } @@ -97,7 +97,7 @@ public class UsersWsRequest { return pageSize; } - public UsersWsRequest setPageSize(int pageSize) { + public OldUsersWsRequest setPageSize(int pageSize) { this.pageSize = pageSize; return this; } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsService.java index b39aca3bfc8..2ce33d1fef6 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsService.java @@ -213,7 +213,7 @@ public class PermissionsService extends BaseService { .setParam(PARAM_PROJECT_KEY_PATTERN, request.getProjectKeyPattern()), UpdateTemplateWsResponse.parser()); } - public UsersWsResponse users(UsersWsRequest request) { + public UsersWsResponse users(OldUsersWsRequest request) { return call(new GetRequest(path("users")) .setParam(PARAM_PERMISSION, request.getPermission()) .setParam(PARAM_PROJECT_ID, request.getProjectId()) diff --git a/sonar-ws/src/main/protobuf/ws-permissions.proto b/sonar-ws/src/main/protobuf/ws-permissions.proto index 75b9f3a1c8d..7477ee25d34 100644 --- a/sonar-ws/src/main/protobuf/ws-permissions.proto +++ b/sonar-ws/src/main/protobuf/ws-permissions.proto @@ -26,6 +26,13 @@ option java_package = "org.sonarqube.ws"; option java_outer_classname = "WsPermissions"; option optimize_for = SPEED; +// WS api/permissions/users for internal use only +// and WS api/permissions/template_users for internal use only +message OldUsersWsResponse { + optional sonarqube.ws.commons.Paging paging = 1; + repeated OldUser users = 2; +} + // WS api/permissions/users for internal use only // and WS api/permissions/template_users for internal use only message UsersWsResponse { @@ -98,6 +105,13 @@ message PermissionTemplate { repeated Permission permissions = 7; } +message OldUser { + optional string login = 1; + optional string name = 2; + optional string email = 3; + optional bool selected = 4; +} + message User { optional string login = 1; optional string name = 2; diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/permission/PermissionsServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/permission/PermissionsServiceTest.java index f82ae59934b..f9bdee7f197 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/permission/PermissionsServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/permission/PermissionsServiceTest.java @@ -431,7 +431,7 @@ public class PermissionsServiceTest { @Test public void users_does_GET_on_Ws_users() { - underTest.users(new UsersWsRequest() + underTest.users(new OldUsersWsRequest() .setPermission(PERMISSION_VALUE) .setProjectId(PROJECT_ID_VALUE) .setProjectKey(PROJECT_KEY_VALUE) -- 2.39.5