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.

DeleteAction.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 org.sonar.api.server.ws.Request;
  22. import org.sonar.api.server.ws.Response;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.api.web.UserRole;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.component.ComponentDto;
  28. import org.sonar.db.permission.OrganizationPermission;
  29. import org.sonar.server.component.ComponentCleanerService;
  30. import org.sonar.server.component.ComponentFinder;
  31. import org.sonar.server.project.ProjectLifeCycleListeners;
  32. import org.sonar.server.user.UserSession;
  33. import static java.util.Collections.singleton;
  34. import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_PROJECT;
  35. import static org.sonar.server.project.Project.from;
  36. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  37. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT;
  38. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT_ID;
  39. public class DeleteAction implements ProjectsWsAction {
  40. private static final String ACTION = "delete";
  41. private final ComponentCleanerService componentCleanerService;
  42. private final ComponentFinder componentFinder;
  43. private final DbClient dbClient;
  44. private final UserSession userSession;
  45. private final ProjectLifeCycleListeners projectLifeCycleListeners;
  46. public DeleteAction(ComponentCleanerService componentCleanerService, ComponentFinder componentFinder, DbClient dbClient,
  47. UserSession userSession, ProjectLifeCycleListeners projectLifeCycleListeners) {
  48. this.componentCleanerService = componentCleanerService;
  49. this.componentFinder = componentFinder;
  50. this.dbClient = dbClient;
  51. this.userSession = userSession;
  52. this.projectLifeCycleListeners = projectLifeCycleListeners;
  53. }
  54. @Override
  55. public void define(WebService.NewController context) {
  56. WebService.NewAction action = context
  57. .createAction(ACTION)
  58. .setPost(true)
  59. .setDescription("Delete a project.<br> " +
  60. "Requires 'Administer System' permission or 'Administer' permission on the project.")
  61. .setSince("5.2")
  62. .setHandler(this);
  63. action
  64. .createParam(PARAM_PROJECT_ID)
  65. .setDescription("Project ID")
  66. .setDeprecatedKey("id", "6.4")
  67. .setDeprecatedSince("6.4")
  68. .setExampleValue("ce4c03d6-430f-40a9-b777-ad877c00aa4d");
  69. action
  70. .createParam(PARAM_PROJECT)
  71. .setDescription("Project key")
  72. .setDeprecatedKey("key", "6.4")
  73. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  74. }
  75. @Override
  76. public void handle(Request request, Response response) throws Exception {
  77. // fail-fast if not logged in
  78. userSession.checkLoggedIn();
  79. String uuid = request.param(PARAM_PROJECT_ID);
  80. String key = request.param(PARAM_PROJECT);
  81. try (DbSession dbSession = dbClient.openSession(false)) {
  82. ComponentDto project = componentFinder.getByUuidOrKey(dbSession, uuid, key, PROJECT_ID_AND_PROJECT);
  83. checkPermission(project);
  84. componentCleanerService.delete(dbSession, project);
  85. projectLifeCycleListeners.onProjectsDeleted(singleton(from(project)));
  86. }
  87. response.noContent();
  88. }
  89. private void checkPermission(ComponentDto project) {
  90. if (!userSession.hasComponentPermission(UserRole.ADMIN, project)) {
  91. userSession.checkPermission(OrganizationPermission.ADMINISTER, project.getOrganizationUuid());
  92. }
  93. }
  94. }