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.

AuthorizationDao.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.db.permission;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.Set;
  24. import javax.annotation.Nullable;
  25. import org.sonar.db.Dao;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.EmailSubscriberDto;
  28. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  29. import static org.sonar.db.DatabaseUtils.executeLargeInputsIntoSet;
  30. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
  31. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
  32. /**
  33. * The SQL requests used to verify authorization (the permissions
  34. * granted to users)
  35. *
  36. * @see GroupPermissionDao for CRUD of table group_roles
  37. * @see UserPermissionDao for CRUD of table user_roles
  38. */
  39. public class AuthorizationDao implements Dao {
  40. /**
  41. * Loads all the global permissions granted to user
  42. */
  43. public Set<String> selectGlobalPermissions(DbSession dbSession, String userUuid) {
  44. return mapper(dbSession).selectGlobalPermissions(userUuid);
  45. }
  46. /**
  47. * Loads all the permissions granted to anonymous user for the specified organization
  48. */
  49. public Set<String> selectGlobalPermissionsOfAnonymous(DbSession dbSession) {
  50. return mapper(dbSession).selectGlobalPermissionsOfAnonymous();
  51. }
  52. /**
  53. * Loads all the permissions granted to logged-in user for the specified project <strong>stored in *_ROLES
  54. * tables</strong>.
  55. * An empty Set is returned if user has no permissions on the project.
  56. *
  57. * <strong>This method does not support public components</strong>
  58. */
  59. public Set<String> selectProjectPermissions(DbSession dbSession, String projectUuid, String userUuid) {
  60. return mapper(dbSession).selectProjectPermissions(projectUuid, userUuid);
  61. }
  62. /**
  63. * Loads all the permissions granted to anonymous for the specified project <strong>stored in *_ROLES
  64. * tables</strong>.
  65. * An empty Set is returned if anonymous user has no permissions on the project.
  66. *
  67. * <strong>This method does not support public components</strong>
  68. */
  69. public Set<String> selectProjectPermissionsOfAnonymous(DbSession dbSession, String projectUuid) {
  70. return mapper(dbSession).selectProjectPermissionsOfAnonymous(projectUuid);
  71. }
  72. /**
  73. * The number of users who will still have the permission if the group {@code excludedGroupUuid}
  74. * is deleted. The anyone virtual group is not taken into account.
  75. */
  76. public int countUsersWithGlobalPermissionExcludingGroup(DbSession dbSession, String permission, String excludedGroupUuid) {
  77. return mapper(dbSession).countUsersWithGlobalPermissionExcludingGroup(permission, excludedGroupUuid);
  78. }
  79. /**
  80. * The number of users who will still have the permission if the user {@code excludedUserId}
  81. * is deleted. The anyone virtual group is not taken into account.
  82. */
  83. public int countUsersWithGlobalPermissionExcludingUser(DbSession dbSession, String permission, String excludedUserUuid) {
  84. return mapper(dbSession).countUsersWithGlobalPermissionExcludingUser(permission, excludedUserUuid);
  85. }
  86. /**
  87. * The list of users who have the global permission.
  88. * The anyone virtual group is not taken into account.
  89. */
  90. public List<String> selectUserUuidsWithGlobalPermission(DbSession dbSession, String permission) {
  91. return mapper(dbSession).selectUserUuidsWithGlobalPermission(permission);
  92. }
  93. /**
  94. * The number of users who will still have the permission if the user {@code userId}
  95. * is removed from group {@code groupUuid}. The anyone virtual group is not taken into account.
  96. * Contrary to {@link #countUsersWithGlobalPermissionExcludingUser(DbSession, String, String)}, user
  97. * still exists and may have the permission directly or through other groups.
  98. */
  99. public int countUsersWithGlobalPermissionExcludingGroupMember(DbSession dbSession, String permission, String groupUuid, String userUuid) {
  100. return mapper(dbSession).countUsersWithGlobalPermissionExcludingGroupMember(permission, groupUuid, userUuid);
  101. }
  102. /**
  103. * The number of users who will still have the permission if the permission {@code permission}
  104. * is removed from user {@code userId}. The anyone virtual group is not taken into account.
  105. * Contrary to {@link #countUsersWithGlobalPermissionExcludingUser(DbSession, String, String)}, user
  106. * still exists and may have the permission through groups.
  107. */
  108. public int countUsersWithGlobalPermissionExcludingUserPermission(DbSession dbSession, String permission, String userUuid) {
  109. return mapper(dbSession).countUsersWithGlobalPermissionExcludingUserPermission(permission, userUuid);
  110. }
  111. public Set<String> keepAuthorizedProjectUuids(DbSession dbSession, Collection<String> projectUuids, @Nullable String userUuid, String permission) {
  112. return executeLargeInputsIntoSet(
  113. projectUuids,
  114. partition -> {
  115. if (userUuid == null) {
  116. return mapper(dbSession).keepAuthorizedProjectUuidsForAnonymous(permission, partition);
  117. }
  118. return mapper(dbSession).keepAuthorizedProjectUuidsForUser(userUuid, permission, partition);
  119. },
  120. partitionSize -> partitionSize / 2);
  121. }
  122. /**
  123. * Keep only authorized user that have the given permission on a given project.
  124. * Please Note that if the permission is 'Anyone' is NOT taking into account by this method.
  125. */
  126. public Collection<String> keepAuthorizedUsersForRoleAndProject(DbSession dbSession, Collection<String> userUuids, String role, String projectUuid) {
  127. return executeLargeInputs(
  128. userUuids,
  129. partitionOfIds -> mapper(dbSession).keepAuthorizedUsersForRoleAndProject(role, projectUuid, partitionOfIds),
  130. partitionSize -> partitionSize / 3);
  131. }
  132. public Set<EmailSubscriberDto> selectQualityProfileAdministratorLogins(DbSession dbSession) {
  133. return mapper(dbSession).selectEmailSubscribersWithGlobalPermission(ADMINISTER_QUALITY_PROFILES.getKey());
  134. }
  135. /**
  136. * Used by license notifications
  137. */
  138. public Set<EmailSubscriberDto> selectGlobalAdministerEmailSubscribers(DbSession dbSession) {
  139. return mapper(dbSession).selectEmailSubscribersWithGlobalPermission(ADMINISTER.getKey());
  140. }
  141. public Set<String> keepAuthorizedLoginsOnProject(DbSession dbSession, Set<String> logins, String projectKey, String permission) {
  142. return executeLargeInputsIntoSet(
  143. logins,
  144. partitionOfLogins -> mapper(dbSession).keepAuthorizedLoginsOnProject(partitionOfLogins, projectKey, permission),
  145. partitionSize -> partitionSize / 3);
  146. }
  147. private static AuthorizationMapper mapper(DbSession dbSession) {
  148. return dbSession.getMapper(AuthorizationMapper.class);
  149. }
  150. }