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.

CreateAction.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.api.user.UserGroupValidation;
  27. import org.sonar.core.util.UuidFactory;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.user.GroupDto;
  31. import org.sonar.server.user.UserSession;
  32. import org.sonarqube.ws.UserGroups;
  33. import static java.lang.String.format;
  34. import static org.sonar.api.user.UserGroupValidation.GROUP_NAME_MAX_LENGTH;
  35. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
  36. import static org.sonar.server.usergroups.ws.GroupWsSupport.DESCRIPTION_MAX_LENGTH;
  37. import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_DESCRIPTION;
  38. import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_NAME;
  39. import static org.sonar.server.usergroups.ws.GroupWsSupport.toProtobuf;
  40. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  41. public class CreateAction implements UserGroupsWsAction {
  42. private final DbClient dbClient;
  43. private final UserSession userSession;
  44. private final GroupWsSupport support;
  45. private final UuidFactory uuidFactory;
  46. public CreateAction(DbClient dbClient, UserSession userSession, GroupWsSupport support, UuidFactory uuidFactory) {
  47. this.dbClient = dbClient;
  48. this.userSession = userSession;
  49. this.support = support;
  50. this.uuidFactory = uuidFactory;
  51. }
  52. @Override
  53. public void define(NewController controller) {
  54. NewAction action = controller.createAction("create")
  55. .setDescription("Create a group.<br>" +
  56. "Requires the following permission: 'Administer System'.")
  57. .setHandler(this)
  58. .setPost(true)
  59. .setResponseExample(getClass().getResource("create-example.json"))
  60. .setSince("5.2")
  61. .setChangelog(
  62. new Change("8.4", "Field 'id' format in the response changes from integer to string."));
  63. action.createParam(PARAM_GROUP_NAME)
  64. .setRequired(true)
  65. .setMaximumLength(GROUP_NAME_MAX_LENGTH)
  66. .setDescription(format("Name for the new group. A group name cannot be larger than %d characters and must be unique. " +
  67. "The value 'anyone' (whatever the case) is reserved and cannot be used.", GROUP_NAME_MAX_LENGTH))
  68. .setExampleValue("sonar-users");
  69. action.createParam(PARAM_GROUP_DESCRIPTION)
  70. .setMaximumLength(DESCRIPTION_MAX_LENGTH)
  71. .setDescription(format("Description for the new group. A group description cannot be larger than %d characters.", DESCRIPTION_MAX_LENGTH))
  72. .setExampleValue("Default group for new users");
  73. }
  74. @Override
  75. public void handle(Request request, Response response) throws Exception {
  76. try (DbSession dbSession = dbClient.openSession(false)) {
  77. userSession.checkPermission(ADMINISTER);
  78. GroupDto group = new GroupDto()
  79. .setUuid(uuidFactory.create())
  80. .setName(request.mandatoryParam(PARAM_GROUP_NAME))
  81. .setDescription(request.param(PARAM_GROUP_DESCRIPTION));
  82. // validations
  83. UserGroupValidation.validateGroupName(group.getName());
  84. support.checkNameDoesNotExist(dbSession, group.getName());
  85. dbClient.groupDao().insert(dbSession, group);
  86. dbSession.commit();
  87. writeResponse(request, response, group);
  88. }
  89. }
  90. private void writeResponse(Request request, Response response, GroupDto group) {
  91. UserGroups.CreateWsResponse.Builder respBuilder = UserGroups.CreateWsResponse.newBuilder();
  92. // 'default' is always false as it's not possible to create a default group
  93. respBuilder.setGroup(toProtobuf(group, 0, false));
  94. writeProtobuf(respBuilder.build(), request, response);
  95. }
  96. }