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.

MemberUpdater.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.server.organization;
  21. import java.util.HashSet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.alm.ALM;
  28. import org.sonar.db.alm.OrganizationAlmBindingDto;
  29. import org.sonar.db.organization.OrganizationDto;
  30. import org.sonar.db.organization.OrganizationMemberDto;
  31. import org.sonar.db.user.UserDto;
  32. import org.sonar.db.user.UserGroupDto;
  33. import org.sonar.server.user.index.UserIndexer;
  34. import org.sonar.server.usergroups.DefaultGroupFinder;
  35. import static com.google.common.base.Preconditions.checkArgument;
  36. import static com.google.common.collect.Sets.difference;
  37. import static com.google.common.collect.Sets.union;
  38. import static java.util.Collections.singletonList;
  39. import static java.util.stream.Collectors.toSet;
  40. import static org.sonar.api.CoreProperties.DEFAULT_ISSUE_ASSIGNEE;
  41. import static org.sonar.core.util.stream.MoreCollectors.toList;
  42. import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
  43. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
  44. public class MemberUpdater {
  45. private final DbClient dbClient;
  46. private final DefaultGroupFinder defaultGroupFinder;
  47. private final UserIndexer userIndexer;
  48. public MemberUpdater(DbClient dbClient, DefaultGroupFinder defaultGroupFinder, UserIndexer userIndexer) {
  49. this.dbClient = dbClient;
  50. this.defaultGroupFinder = defaultGroupFinder;
  51. this.userIndexer = userIndexer;
  52. }
  53. public void addMember(DbSession dbSession, OrganizationDto organization, UserDto user) {
  54. addMembers(dbSession, organization, singletonList(user));
  55. }
  56. public void addMembers(DbSession dbSession, OrganizationDto organization, List<UserDto> users) {
  57. Set<String> currentMemberUuids = new HashSet<>(dbClient.organizationMemberDao().selectUserUuidsByOrganizationUuid(dbSession, organization.getUuid()));
  58. List<UserDto> usersToAdd = users.stream()
  59. .filter(UserDto::isActive)
  60. .filter(u -> !currentMemberUuids.contains(u.getUuid()))
  61. .collect(toList());
  62. if (usersToAdd.isEmpty()) {
  63. return;
  64. }
  65. usersToAdd.forEach(u -> addMemberInDb(dbSession, organization, u));
  66. userIndexer.commitAndIndex(dbSession, usersToAdd);
  67. }
  68. private void addMemberInDb(DbSession dbSession, OrganizationDto organization, UserDto user) {
  69. dbClient.organizationMemberDao().insert(dbSession, new OrganizationMemberDto()
  70. .setOrganizationUuid(organization.getUuid())
  71. .setUserUuid(user.getUuid()));
  72. dbClient.userGroupDao().insert(dbSession,
  73. new UserGroupDto().setGroupUuid(defaultGroupFinder.findDefaultGroup(dbSession).getUuid()).setUserUuid(user.getUuid()));
  74. }
  75. public void removeMember(DbSession dbSession, OrganizationDto organization, UserDto user) {
  76. removeMembers(dbSession, organization, singletonList(user));
  77. }
  78. public void removeMembers(DbSession dbSession, OrganizationDto organization, List<UserDto> users) {
  79. Set<String> currentMemberIds = new HashSet<>(dbClient.organizationMemberDao().selectUserUuidsByOrganizationUuid(dbSession, organization.getUuid()));
  80. List<UserDto> usersToRemove = users.stream()
  81. .filter(UserDto::isActive)
  82. .filter(u -> currentMemberIds.contains(u.getUuid()))
  83. .collect(toList());
  84. if (usersToRemove.isEmpty()) {
  85. return;
  86. }
  87. Set<String> userUuidsToRemove = usersToRemove.stream().map(UserDto::getUuid).collect(toSet());
  88. Set<String> adminUuids = new HashSet<>(dbClient.authorizationDao().selectUserUuidsWithGlobalPermission(dbSession, ADMINISTER.getKey()));
  89. checkArgument(!difference(adminUuids, userUuidsToRemove).isEmpty(), "The last administrator member cannot be removed");
  90. usersToRemove.forEach(u -> removeMemberInDb(dbSession, organization, u));
  91. userIndexer.commitAndIndex(dbSession, usersToRemove);
  92. }
  93. /**
  94. * Synchronize organization membership of a user from a list of ALM organization specific ids
  95. * Please note that no commit will not be executed.
  96. */
  97. public void synchronizeUserOrganizationMembership(DbSession dbSession, UserDto user, ALM alm, Set<String> organizationAlmIds) {
  98. Set<String> userOrganizationUuids = dbClient.organizationMemberDao().selectOrganizationUuidsByUser(dbSession, user.getUuid());
  99. Set<String> userOrganizationUuidsWithMembersSyncEnabled = dbClient.organizationAlmBindingDao().selectByOrganizationUuids(dbSession, userOrganizationUuids).stream()
  100. .filter(OrganizationAlmBindingDto::isMembersSyncEnable)
  101. .map(OrganizationAlmBindingDto::getOrganizationUuid)
  102. .collect(toSet());
  103. Set<String> almOrganizationUuidsWithMembersSyncEnabled = dbClient.organizationAlmBindingDao().selectByOrganizationAlmIds(dbSession, alm, organizationAlmIds).stream()
  104. .filter(OrganizationAlmBindingDto::isMembersSyncEnable)
  105. .map(OrganizationAlmBindingDto::getOrganizationUuid)
  106. .collect(toSet());
  107. Set<String> organizationUuidsToBeAdded = difference(almOrganizationUuidsWithMembersSyncEnabled, userOrganizationUuidsWithMembersSyncEnabled);
  108. Set<String> organizationUuidsToBeRemoved = difference(userOrganizationUuidsWithMembersSyncEnabled, almOrganizationUuidsWithMembersSyncEnabled);
  109. Map<String, OrganizationDto> allOrganizationsByUuid = dbClient.organizationDao().selectByUuids(dbSession, union(organizationUuidsToBeAdded, organizationUuidsToBeRemoved))
  110. .stream()
  111. .collect(uniqueIndex(OrganizationDto::getUuid));
  112. allOrganizationsByUuid.entrySet().stream()
  113. .filter(entry -> organizationUuidsToBeAdded.contains(entry.getKey()))
  114. .forEach(entry -> addMemberInDb(dbSession, entry.getValue(), user));
  115. allOrganizationsByUuid.entrySet().stream()
  116. .filter(entry -> organizationUuidsToBeRemoved.contains(entry.getKey()))
  117. .forEach(entry -> removeMemberInDb(dbSession, entry.getValue(), user));
  118. }
  119. private void removeMemberInDb(DbSession dbSession, OrganizationDto organization, UserDto user) {
  120. String userUuid = user.getUuid();
  121. String organizationUuid = organization.getUuid();
  122. // dbClient.userPermissionDao().deleteOrganizationMemberPermissions(dbSession, userUuid);
  123. //dbClient.permissionTemplateDao().deleteUserPermissionsByOrganization(dbSession, organizationUuid, userUuid);
  124. dbClient.propertiesDao().deleteByOrganizationAndUser(dbSession, organizationUuid, userUuid);
  125. dbClient.propertiesDao().deleteByOrganizationAndMatchingLogin(dbSession, organizationUuid, user.getLogin(), singletonList(DEFAULT_ISSUE_ASSIGNEE));
  126. dbClient.organizationMemberDao().delete(dbSession, organizationUuid, userUuid);
  127. }
  128. }