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.

UserPermissionChanger.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.permission;
  21. import java.util.List;
  22. import java.util.Optional;
  23. import org.sonar.db.DbClient;
  24. import org.sonar.db.DbSession;
  25. import org.sonar.db.permission.UserPermissionDto;
  26. import static org.sonar.api.web.UserRole.PUBLIC_PERMISSIONS;
  27. import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
  28. import static org.sonar.server.permission.PermissionChange.Operation.ADD;
  29. import static org.sonar.server.permission.PermissionChange.Operation.REMOVE;
  30. import static org.sonar.server.ws.WsUtils.checkRequest;
  31. /**
  32. * Adds and removes user permissions. Both global and project scopes are supported.
  33. */
  34. public class UserPermissionChanger {
  35. private final DbClient dbClient;
  36. public UserPermissionChanger(DbClient dbClient) {
  37. this.dbClient = dbClient;
  38. }
  39. public boolean apply(DbSession dbSession, UserPermissionChange change) {
  40. ensureConsistencyWithVisibility(change);
  41. if (isImplicitlyAlreadyDone(change)) {
  42. return false;
  43. }
  44. switch (change.getOperation()) {
  45. case ADD:
  46. return addPermission(dbSession, change);
  47. case REMOVE:
  48. return removePermission(dbSession, change);
  49. default:
  50. throw new UnsupportedOperationException("Unsupported permission change: " + change.getOperation());
  51. }
  52. }
  53. private static boolean isImplicitlyAlreadyDone(UserPermissionChange change) {
  54. return change.getProjectId()
  55. .map(projectId -> isImplicitlyAlreadyDone(projectId, change))
  56. .orElse(false);
  57. }
  58. private static boolean isImplicitlyAlreadyDone(ProjectId projectId, UserPermissionChange change) {
  59. return isAttemptToAddPublicPermissionToPublicComponent(change, projectId);
  60. }
  61. private static boolean isAttemptToAddPublicPermissionToPublicComponent(UserPermissionChange change, ProjectId projectId) {
  62. return !projectId.isPrivate()
  63. && change.getOperation() == ADD
  64. && PUBLIC_PERMISSIONS.contains(change.getPermission());
  65. }
  66. private static void ensureConsistencyWithVisibility(UserPermissionChange change) {
  67. change.getProjectId()
  68. .ifPresent(projectId -> checkRequest(
  69. !isAttemptToRemovePublicPermissionFromPublicComponent(change, projectId),
  70. "Permission %s can't be removed from a public component", change.getPermission()));
  71. }
  72. private static boolean isAttemptToRemovePublicPermissionFromPublicComponent(UserPermissionChange change, ProjectId projectId) {
  73. return !projectId.isPrivate()
  74. && change.getOperation() == REMOVE
  75. && PUBLIC_PERMISSIONS.contains(change.getPermission());
  76. }
  77. private boolean addPermission(DbSession dbSession, UserPermissionChange change) {
  78. if (loadExistingPermissions(dbSession, change).contains(change.getPermission())) {
  79. return false;
  80. }
  81. UserPermissionDto dto = new UserPermissionDto(change.getOrganizationUuid(), change.getPermission(), change.getUserId().getId(), change.getNullableProjectId());
  82. dbClient.userPermissionDao().insert(dbSession, dto);
  83. return true;
  84. }
  85. private boolean removePermission(DbSession dbSession, UserPermissionChange change) {
  86. if (!loadExistingPermissions(dbSession, change).contains(change.getPermission())) {
  87. return false;
  88. }
  89. checkOtherAdminsExist(dbSession, change);
  90. Optional<ProjectId> projectId = change.getProjectId();
  91. if (projectId.isPresent()) {
  92. dbClient.userPermissionDao().deleteProjectPermission(dbSession, change.getUserId().getId(), change.getPermission(), projectId.get().getId());
  93. } else {
  94. dbClient.userPermissionDao().deleteGlobalPermission(dbSession, change.getUserId().getId(), change.getPermission(), change.getOrganizationUuid());
  95. }
  96. return true;
  97. }
  98. private List<String> loadExistingPermissions(DbSession dbSession, UserPermissionChange change) {
  99. Optional<ProjectId> projectId = change.getProjectId();
  100. if (projectId.isPresent()) {
  101. return dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession,
  102. change.getUserId().getId(),
  103. projectId.get().getId());
  104. }
  105. return dbClient.userPermissionDao().selectGlobalPermissionsOfUser(dbSession,
  106. change.getUserId().getId(),
  107. change.getOrganizationUuid());
  108. }
  109. private void checkOtherAdminsExist(DbSession dbSession, UserPermissionChange change) {
  110. if (SYSTEM_ADMIN.equals(change.getPermission()) && !change.getProjectId().isPresent()) {
  111. int remaining = dbClient.authorizationDao().countUsersWithGlobalPermissionExcludingUserPermission(dbSession,
  112. change.getOrganizationUuid(), change.getPermission(), change.getUserId().getId());
  113. checkRequest(remaining > 0, "Last user with permission '%s'. Permission cannot be removed.", SYSTEM_ADMIN);
  114. }
  115. }
  116. }