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.5KB

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