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.

GroupWsSupport.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 java.util.Optional;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.user.GroupDto;
  27. import org.sonar.server.exceptions.NotFoundException;
  28. import org.sonar.server.permission.GroupUuid;
  29. import org.sonar.server.permission.GroupUuidOrAnyone;
  30. import org.sonar.server.usergroups.DefaultGroupFinder;
  31. import org.sonarqube.ws.UserGroups;
  32. import static com.google.common.base.Preconditions.checkArgument;
  33. import static java.util.Optional.ofNullable;
  34. import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
  35. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  36. import static org.sonar.server.exceptions.NotFoundException.checkFound;
  37. import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
  38. /**
  39. * Factorizes code about user groups between web services
  40. */
  41. public class GroupWsSupport {
  42. static final String PARAM_GROUP_ID = "id";
  43. static final String PARAM_GROUP_NAME = "name";
  44. static final String PARAM_GROUP_DESCRIPTION = "description";
  45. static final String PARAM_LOGIN = "login";
  46. // Database column size should be 500 (since migration #353),
  47. // but on some instances, column size is still 200,
  48. // hence the validation is done with 200
  49. static final int DESCRIPTION_MAX_LENGTH = 200;
  50. private final DbClient dbClient;
  51. private final DefaultGroupFinder defaultGroupFinder;
  52. public GroupWsSupport(DbClient dbClient, DefaultGroupFinder defaultGroupFinder) {
  53. this.dbClient = dbClient;
  54. this.defaultGroupFinder = defaultGroupFinder;
  55. }
  56. /**
  57. * Find a group by its id (parameter {@link #PARAM_GROUP_ID}) or group name (parameter {@link #PARAM_GROUP_NAME}). The virtual
  58. * group "Anyone" is not supported.
  59. *
  60. * @throws NotFoundException if parameters are missing/incorrect, if the requested group does not exist
  61. * or if the virtual group "Anyone" is requested.
  62. */
  63. public GroupUuid findGroup(DbSession dbSession, Request request) {
  64. return GroupUuid.from(findGroupDto(dbSession, request));
  65. }
  66. public GroupDto findGroupDto(DbSession dbSession, Request request) {
  67. String uuid = request.param(PARAM_GROUP_ID);
  68. String name = request.param(PARAM_GROUP_NAME);
  69. return findGroupDto(dbSession, GroupWsRef.create(uuid, name));
  70. }
  71. public GroupDto findGroupDto(DbSession dbSession, GroupWsRef ref) {
  72. if (ref.hasUuid()) {
  73. GroupDto group = dbClient.groupDao().selectByUuid(dbSession, ref.getUuid());
  74. checkFound(group, "No group with id '%s'", ref.getUuid());
  75. return group;
  76. }
  77. Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, ref.getName());
  78. checkFoundWithOptional(group, "No group with name '%s'", ref.getName());
  79. return group.get();
  80. }
  81. public GroupUuidOrAnyone findGroupOrAnyone(DbSession dbSession, GroupWsRef ref) {
  82. if (ref.hasUuid()) {
  83. GroupDto group = dbClient.groupDao().selectByUuid(dbSession, ref.getUuid());
  84. checkFound(group, "No group with id '%s'", ref.getUuid());
  85. return GroupUuidOrAnyone.from(group);
  86. }
  87. if (ref.isAnyone()) {
  88. return GroupUuidOrAnyone.forAnyone();
  89. }
  90. Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, ref.getName());
  91. checkFoundWithOptional(group, "No group with name '%s'", ref.getName());
  92. return GroupUuidOrAnyone.from(group.get());
  93. }
  94. void checkNameDoesNotExist(DbSession dbSession, String name) {
  95. // There is no database constraint on column groups.name
  96. // because MySQL cannot create a unique index
  97. // on a UTF-8 VARCHAR larger than 255 characters on InnoDB
  98. checkRequest(!dbClient.groupDao().selectByName(dbSession, name).isPresent(), "Group '%s' already exists", name);
  99. }
  100. void checkGroupIsNotDefault(DbSession dbSession, GroupDto groupDto) {
  101. GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession);
  102. checkArgument(!defaultGroup.getUuid().equals(groupDto.getUuid()), "Default group '%s' cannot be used to perform this action", groupDto.getName());
  103. }
  104. static UserGroups.Group.Builder toProtobuf(GroupDto group, int membersCount, boolean isDefault) {
  105. UserGroups.Group.Builder wsGroup = UserGroups.Group.newBuilder()
  106. .setId(group.getUuid())
  107. .setName(group.getName())
  108. .setMembersCount(membersCount)
  109. .setDefault(isDefault);
  110. ofNullable(group.getDescription()).ifPresent(wsGroup::setDescription);
  111. return wsGroup;
  112. }
  113. static void defineGroupWsParameters(WebService.NewAction action) {
  114. defineGroupIdWsParameter(action);
  115. defineGroupNameWsParameter(action);
  116. }
  117. private static void defineGroupIdWsParameter(WebService.NewAction action) {
  118. action.createParam(PARAM_GROUP_ID)
  119. .setDescription("Group id, use 'name' instead")
  120. .setDeprecatedSince("8.4")
  121. .setExampleValue(UUID_EXAMPLE_01);
  122. }
  123. private static void defineGroupNameWsParameter(WebService.NewAction action) {
  124. action.createParam(PARAM_GROUP_NAME)
  125. .setDescription("Group name")
  126. .setExampleValue("sonar-administrators");
  127. }
  128. static WebService.NewParam defineLoginWsParameter(WebService.NewAction action) {
  129. return action.createParam(PARAM_LOGIN)
  130. .setDescription("User login")
  131. .setExampleValue("g.hopper");
  132. }
  133. }