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.

UpdateVisibilityAction.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 com.google.common.collect.ImmutableSet;
  22. import java.util.Set;
  23. import org.sonar.api.resources.Qualifiers;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.api.web.UserRole;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.component.BranchMapper;
  31. import org.sonar.db.component.ComponentDto;
  32. import org.sonar.db.component.ComponentMapper;
  33. import org.sonar.db.organization.OrganizationDto;
  34. import org.sonar.db.permission.GroupPermissionDto;
  35. import org.sonar.db.permission.UserPermissionDto;
  36. import org.sonar.server.component.ComponentFinder;
  37. import org.sonar.server.es.ProjectIndexer;
  38. import org.sonar.server.es.ProjectIndexers;
  39. import org.sonar.server.project.Visibility;
  40. import org.sonar.server.user.UserSession;
  41. import org.sonarqube.ws.client.project.ProjectsWsParameters;
  42. import static java.lang.String.format;
  43. import static java.util.Collections.singletonList;
  44. import static org.sonar.api.web.UserRole.PUBLIC_PERMISSIONS;
  45. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  46. import static org.sonar.server.ws.WsUtils.checkRequest;
  47. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT;
  48. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_VISIBILITY;
  49. public class UpdateVisibilityAction implements ProjectsWsAction {
  50. private static final Set<String> AUTHORIZED_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.APP);
  51. private final DbClient dbClient;
  52. private final ComponentFinder componentFinder;
  53. private final UserSession userSession;
  54. private final ProjectIndexers projectIndexers;
  55. private final ProjectsWsSupport projectsWsSupport;
  56. public UpdateVisibilityAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession,
  57. ProjectIndexers projectIndexers, ProjectsWsSupport projectsWsSupport) {
  58. this.dbClient = dbClient;
  59. this.componentFinder = componentFinder;
  60. this.userSession = userSession;
  61. this.projectIndexers = projectIndexers;
  62. this.projectsWsSupport = projectsWsSupport;
  63. }
  64. public void define(WebService.NewController context) {
  65. WebService.NewAction action = context.createAction(ProjectsWsParameters.ACTION_UPDATE_VISIBILITY)
  66. .setDescription("Updates visibility of a project.<br>" +
  67. "Requires 'Project administer' permission on the specified project")
  68. .setSince("6.4")
  69. .setPost(true)
  70. .setHandler(this);
  71. action.createParam(PARAM_PROJECT)
  72. .setDescription("Project key")
  73. .setExampleValue(KEY_PROJECT_EXAMPLE_001)
  74. .setRequired(true);
  75. action.createParam(PARAM_VISIBILITY)
  76. .setDescription("New visibility")
  77. .setPossibleValues(Visibility.getLabels())
  78. .setRequired(true);
  79. }
  80. @Override
  81. public void handle(Request request, Response response) throws Exception {
  82. userSession.checkLoggedIn();
  83. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  84. boolean changeToPrivate = Visibility.isPrivate(request.mandatoryParam(PARAM_VISIBILITY));
  85. try (DbSession dbSession = dbClient.openSession(false)) {
  86. ComponentDto component = componentFinder.getByKey(dbSession, projectKey);
  87. checkRequest(component.isRootProject() && AUTHORIZED_QUALIFIERS.contains(component.qualifier()), "Component must be a project, a portfolio or an application");
  88. userSession.checkComponentPermission(UserRole.ADMIN, component);
  89. checkRequest(noPendingTask(dbSession, component), "Component visibility can't be changed as long as it has background task(s) pending or in progress");
  90. if (changeToPrivate != component.isPrivate()) {
  91. OrganizationDto organization = dbClient.organizationDao().selectByUuid(dbSession, component.getOrganizationUuid())
  92. .orElseThrow(() -> new IllegalStateException(format("Could not find organization with uuid '%s' of project '%s'", component.getOrganizationUuid(), projectKey)));
  93. projectsWsSupport.checkCanUpdateProjectsVisibility(organization, changeToPrivate);
  94. setPrivateForRootComponentUuid(dbSession, component.uuid(), changeToPrivate);
  95. if (changeToPrivate) {
  96. updatePermissionsToPrivate(dbSession, component);
  97. } else {
  98. updatePermissionsToPublic(dbSession, component);
  99. }
  100. projectIndexers.commitAndIndex(dbSession, singletonList(component), ProjectIndexer.Cause.PERMISSION_CHANGE);
  101. }
  102. response.noContent();
  103. }
  104. }
  105. private void setPrivateForRootComponentUuid(DbSession dbSession, String uuid, boolean isPrivate) {
  106. dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, uuid, isPrivate);
  107. ComponentMapper mapper = dbSession.getMapper(ComponentMapper.class);
  108. dbSession.getMapper(BranchMapper.class).selectByProjectUuid(uuid)
  109. .stream()
  110. .filter(branch -> !uuid.equals(branch.getUuid()))
  111. .forEach(branch -> mapper.setPrivateForRootComponentUuid(branch.getUuid(), isPrivate));
  112. }
  113. private boolean noPendingTask(DbSession dbSession, ComponentDto rootComponent) {
  114. // FIXME this is probably broken in case a branch is passed to the WS
  115. return dbClient.ceQueueDao().selectByMainComponentUuid(dbSession, rootComponent.uuid()).isEmpty();
  116. }
  117. private void updatePermissionsToPrivate(DbSession dbSession, ComponentDto component) {
  118. // delete project permissions for group AnyOne
  119. dbClient.groupPermissionDao().deleteByRootComponentIdAndGroupId(dbSession, component.getId(), null);
  120. // grant UserRole.CODEVIEWER and UserRole.USER to any group or user with at least one permission on project
  121. PUBLIC_PERMISSIONS.forEach(permission -> {
  122. dbClient.groupPermissionDao().selectGroupIdsWithPermissionOnProjectBut(dbSession, component.getId(), permission)
  123. .forEach(groupId -> insertProjectPermissionOnGroup(dbSession, component, permission, groupId));
  124. dbClient.userPermissionDao().selectUserIdsWithPermissionOnProjectBut(dbSession, component.getId(), permission)
  125. .forEach(userId -> insertProjectPermissionOnUser(dbSession, component, permission, userId));
  126. });
  127. }
  128. private void insertProjectPermissionOnUser(DbSession dbSession, ComponentDto component, String permission, Integer userId) {
  129. dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(component.getOrganizationUuid(), permission, userId, component.getId()));
  130. }
  131. private void insertProjectPermissionOnGroup(DbSession dbSession, ComponentDto component, String permission, Integer groupId) {
  132. dbClient.groupPermissionDao().insert(dbSession, new GroupPermissionDto()
  133. .setOrganizationUuid(component.getOrganizationUuid())
  134. .setResourceId(component.getId())
  135. .setGroupId(groupId)
  136. .setRole(permission));
  137. }
  138. private void updatePermissionsToPublic(DbSession dbSession, ComponentDto component) {
  139. PUBLIC_PERMISSIONS.forEach(permission -> {
  140. // delete project group permission for UserRole.CODEVIEWER and UserRole.USER
  141. dbClient.groupPermissionDao().deleteByRootComponentIdAndPermission(dbSession, component.getId(), permission);
  142. // delete project user permission for UserRole.CODEVIEWER and UserRole.USER
  143. dbClient.userPermissionDao().deleteProjectPermissionOfAnyUser(dbSession, component.getId(), permission);
  144. });
  145. }
  146. }