+++ /dev/null
-/*
- * 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.OldPermissionQuery;
-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. <br />" +
- "This service defaults to global permissions, but can be limited to project permissions by providing project id or project key.<br /> " +
- "If the query parameter '%s' is specified, the '%s' parameter is forced to '%s'.<br />" +
- "It requires administration permissions to access.<br />",
- 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> wsProjectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
- validatePermission(request.getPermission(), wsProjectRef);
- DbSession dbSession = dbClient.openSession(false);
- try {
- Optional<ComponentDto> project = dependenciesFinder.searchProject(dbSession, wsProjectRef);
- checkProjectAdminUserByComponentDto(userSession, project);
- OldPermissionQuery 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);
- 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<UserWithPermissionDto> 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 OldPermissionQuery buildPermissionQuery(OldUsersWsRequest request, Optional<ComponentDto> project) {
- OldPermissionQuery.Builder permissionQuery = OldPermissionQuery.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();
- }
-}
AddUserAction.class,
RemoveGroupAction.class,
RemoveUserAction.class,
- OldUsersAction.class,
UsersAction.class,
GroupsAction.class,
SearchGlobalPermissionsAction.class,
@Override
public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("users2")
+ WebService.NewAction action = context.createAction("users")
.setSince("5.2")
.setDescription("Lists the users with their permissions as individual users rather than through group affiliation.<br>" +
"This service defaults to global permissions, but can be limited to project permissions by providing project id or project key.<br> " +
+++ /dev/null
-{
- "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
- }
-}
-
+++ /dev/null
-/*
- * 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.Ignore;
-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.UserPermissionDto;
-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
- @Ignore("will be deleted")
- 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 UserPermissionDto().setPermission(SCAN_EXECUTION).setUserId(user1.getId()));
- insertUserRole(new UserPermissionDto().setPermission(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
- @Ignore("will be deleted")
- 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 UserPermissionDto().setPermission(ISSUE_ADMIN).setUserId(user.getId()).setComponentId(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(UserPermissionDto userPermissionDto) {
- dbClient.roleDao().insertUserRole(dbSession, userPermissionDto);
- 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 UserPermissionDto().setPermission(SCAN_EXECUTION).setUserId(user1.getId()));
- insertUserRole(new UserPermissionDto().setPermission(SCAN_EXECUTION).setUserId(user2.getId()));
- insertUserRole(new UserPermissionDto().setPermission(SYSTEM_ADMIN).setUserId(user3.getId()));
- dbSession.commit();
- }
-}
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new PermissionsWsModule().configure(container);
- assertThat(container.size()).isEqualTo(2 + 30);
+ assertThat(container.size()).isEqualTo(2 + 29);
}
}
import org.sonarqube.ws.WsPermissions;
import org.sonarqube.ws.WsPermissions.CreateTemplateWsResponse;
-import org.sonarqube.ws.WsPermissions.OldUsersWsResponse;
import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse;
import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse;
import org.sonarqube.ws.WsPermissions.UpdateTemplateWsResponse;
.setParam(PARAM_PROJECT_KEY_PATTERN, request.getProjectKeyPattern()), UpdateTemplateWsResponse.parser());
}
- public OldUsersWsResponse oldUsers(OldUsersWsRequest request) {
- return call(new GetRequest(path("users"))
- .setParam(PARAM_PERMISSION, request.getPermission())
- .setParam(PARAM_PROJECT_ID, request.getProjectId())
- .setParam(PARAM_PROJECT_KEY, request.getProjectKey())
- .setParam("selected", request.getSelected())
- .setParam("p", request.getPage())
- .setParam("ps", request.getPageSize())
- .setParam("q", request.getQuery()), OldUsersWsResponse.parser());
- }
-
public UsersWsResponse users(UsersWsRequest request) {
- return call(new GetRequest(path("users2"))
+ return call(new GetRequest(path("users"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_PROJECT_ID, request.getProjectId())
.setParam(PARAM_PROJECT_KEY, request.getProjectKey())
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
+// 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 {
optional sonarqube.ws.commons.Paging paging = 1;
repeated User users = 2;
import org.junit.Rule;
import org.junit.Test;
import org.sonarqube.ws.WsPermissions;
-import org.sonarqube.ws.WsPermissions.OldUsersWsResponse;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.ServiceTester;
.andNoOtherParam();
}
- @Test
- public void users_does_GET_on_Ws_users() {
- underTest.oldUsers(new OldUsersWsRequest()
- .setPermission(PERMISSION_VALUE)
- .setProjectId(PROJECT_ID_VALUE)
- .setProjectKey(PROJECT_KEY_VALUE)
- .setSelected(SELECTED_VALUE)
- .setPage(PAGE_VALUE)
- .setPageSize(PAGE_SIZE_VALUE)
- .setQuery(QUERY_VALUE)
- );
-
- assertThat(serviceTester.getGetParser()).isSameAs(OldUsersWsResponse.parser());
- GetRequest getRequest = serviceTester.getGetRequest();
- serviceTester.assertThat(getRequest)
- .hasPath("users")
- .hasParam(PARAM_PERMISSION, PERMISSION_VALUE)
- .hasParam(PARAM_PROJECT_ID, PROJECT_ID_VALUE)
- .hasParam(PARAM_PROJECT_KEY, PROJECT_KEY_VALUE)
- .hasParam(PARAM_SELECTED, SELECTED_VALUE)
- .hasParam(PARAM_P, PAGE_VALUE)
- .hasParam(PARAM_PS, PAGE_SIZE_VALUE)
- .hasParam(PARAM_Q, QUERY_VALUE)
- .andNoOtherParam();
- }
-
@Test
public void add_project_creator_to_template() {
underTest.addProjectCreatorToTemplate(AddProjectCreatorToTemplateWsRequest.builder()