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.

UpdateDefaultVisibilityAction.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.project.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;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.server.project.ProjectDefaultVisibility;
  28. import org.sonar.server.project.Visibility;
  29. import org.sonar.server.user.UserSession;
  30. import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
  31. public class UpdateDefaultVisibilityAction implements ProjectsWsAction {
  32. static final String ACTION = "update_default_visibility";
  33. static final String PARAM_PROJECT_VISIBILITY = "projectVisibility";
  34. private final UserSession userSession;
  35. private final DbClient dbClient;
  36. private final ProjectDefaultVisibility projectDefaultVisibility;
  37. public UpdateDefaultVisibilityAction(UserSession userSession, DbClient dbClient, ProjectDefaultVisibility projectDefaultVisibility) {
  38. this.userSession = userSession;
  39. this.dbClient = dbClient;
  40. this.projectDefaultVisibility = projectDefaultVisibility;
  41. }
  42. @Override
  43. public void define(WebService.NewController context) {
  44. WebService.NewAction action = context.createAction(ACTION)
  45. .setPost(true)
  46. .setDescription("Update the default visibility for new projects.<br/>Requires System Administrator privileges")
  47. .setChangelog(
  48. new Change("7.3", "This WS used to be located at /api/organizations/update_project_visibility"))
  49. .setInternal(true)
  50. .setSince("6.4")
  51. .setHandler(this);
  52. action.createParam(PARAM_PROJECT_VISIBILITY)
  53. .setRequired(true)
  54. .setDescription("Default visibility for projects")
  55. .setPossibleValues(Visibility.getLabels());
  56. }
  57. @Override
  58. public void handle(Request request, Response response) throws Exception {
  59. String newDefaultProjectVisibility = request.mandatoryParam(PARAM_PROJECT_VISIBILITY);
  60. try (DbSession dbSession = dbClient.openSession(false)) {
  61. if (!userSession.isSystemAdministrator()) {
  62. throw insufficientPrivilegesException();
  63. }
  64. projectDefaultVisibility.set(dbSession, newDefaultProjectVisibility);
  65. dbSession.commit();
  66. }
  67. response.noContent();
  68. }
  69. }