You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoveUserAction.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.usergroups.ws;
  21. import org.sonar.api.server.ws.Change;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService.NewAction;
  25. import org.sonar.api.server.ws.WebService.NewController;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.permission.OrganizationPermission;
  29. import org.sonar.db.user.GroupDto;
  30. import org.sonar.db.user.UserDto;
  31. import org.sonar.server.user.UserSession;
  32. import static java.lang.String.format;
  33. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  34. import static org.sonar.server.exceptions.NotFoundException.checkFound;
  35. import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_ID;
  36. import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_NAME;
  37. import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_LOGIN;
  38. import static org.sonar.server.usergroups.ws.GroupWsSupport.defineGroupWsParameters;
  39. import static org.sonar.server.usergroups.ws.GroupWsSupport.defineLoginWsParameter;
  40. public class RemoveUserAction implements UserGroupsWsAction {
  41. private final DbClient dbClient;
  42. private final UserSession userSession;
  43. private final GroupWsSupport support;
  44. public RemoveUserAction(DbClient dbClient, UserSession userSession, GroupWsSupport support) {
  45. this.dbClient = dbClient;
  46. this.userSession = userSession;
  47. this.support = support;
  48. }
  49. @Override
  50. public void define(NewController context) {
  51. NewAction action = context.createAction("remove_user")
  52. .setDescription(format("Remove a user from a group.<br />" +
  53. "'%s' or '%s' must be provided.<br>" +
  54. "Requires the following permission: 'Administer System'.",
  55. PARAM_GROUP_ID, PARAM_GROUP_NAME))
  56. .setHandler(this)
  57. .setPost(true)
  58. .setSince("5.2")
  59. .setChangelog(
  60. new Change("8.4", "Parameter 'id' is deprecated. Format changes from integer to string. Use 'name' instead."));
  61. defineGroupWsParameters(action);
  62. defineLoginWsParameter(action);
  63. }
  64. @Override
  65. public void handle(Request request, Response response) throws Exception {
  66. userSession.checkLoggedIn();
  67. try (DbSession dbSession = dbClient.openSession(false)) {
  68. GroupDto group = support.findGroupDto(dbSession, request);
  69. userSession.checkPermission(OrganizationPermission.ADMINISTER);
  70. support.checkGroupIsNotDefault(dbSession, group);
  71. String login = request.mandatoryParam(PARAM_LOGIN);
  72. UserDto user = getUser(dbSession, login);
  73. ensureLastAdminIsNotRemoved(dbSession, group, user);
  74. dbClient.userGroupDao().delete(dbSession, group.getUuid(), user.getUuid());
  75. dbSession.commit();
  76. response.noContent();
  77. }
  78. }
  79. /**
  80. * Ensure that there are still users with admin global permission if user is removed from the group.
  81. */
  82. private void ensureLastAdminIsNotRemoved(DbSession dbSession, GroupDto group, UserDto user) {
  83. int remainingAdmins = dbClient.authorizationDao().countUsersWithGlobalPermissionExcludingGroupMember(dbSession,
  84. OrganizationPermission.ADMINISTER.getKey(), group.getUuid(), user.getUuid());
  85. checkRequest(remainingAdmins > 0, "The last administrator user cannot be removed");
  86. }
  87. private UserDto getUser(DbSession dbSession, String userLogin) {
  88. return checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, userLogin),
  89. "User with login '%s' is not found'", userLogin);
  90. }
  91. }