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.

PermissionUpdater.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.common.permission;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import java.util.Set;
  27. import java.util.function.Function;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.entity.EntityDto;
  30. import org.sonar.server.es.Indexers;
  31. import static java.util.stream.Collectors.groupingBy;
  32. import static java.util.stream.Collectors.toMap;
  33. import static org.sonar.api.utils.Preconditions.checkState;
  34. import static org.sonar.server.es.Indexers.EntityEvent.PERMISSION_CHANGE;
  35. public class PermissionUpdater<T extends PermissionChange> {
  36. private final Indexers indexers;
  37. private final Map<Class<?>, GranteeTypeSpecificPermissionUpdater<T>> specificPermissionClassToHandler;
  38. public PermissionUpdater(Indexers indexers, Set<GranteeTypeSpecificPermissionUpdater<T>> permissionChangers) {
  39. this.indexers = indexers;
  40. specificPermissionClassToHandler = permissionChangers.stream()
  41. .collect(toMap(GranteeTypeSpecificPermissionUpdater::getHandledClass, Function.identity()));
  42. }
  43. public void apply(DbSession dbSession, Collection<T> changes) {
  44. checkState(changes.stream().map(PermissionChange::getProjectUuid).distinct().count() <= 1,
  45. "Only one project per changes is supported");
  46. List<String> projectOrViewUuids = new ArrayList<>();
  47. Map<Optional<String>, List<T>> granteeUuidToPermissionChanges = changes.stream().collect(groupingBy(change -> Optional.ofNullable(change.getUuidOfGrantee())));
  48. granteeUuidToPermissionChanges.values().forEach(permissionChanges -> applyForSingleGrantee(dbSession, projectOrViewUuids, permissionChanges));
  49. indexers.commitAndIndexOnEntityEvent(dbSession, projectOrViewUuids, PERMISSION_CHANGE);
  50. }
  51. private void applyForSingleGrantee(DbSession dbSession, List<String> projectOrViewUuids, List<T> permissionChanges) {
  52. T anyPermissionChange = permissionChanges.iterator().next();
  53. EntityDto entity = anyPermissionChange.getEntity();
  54. String entityUuid = Optional.ofNullable(entity).map(EntityDto::getUuid).orElse(null);
  55. GranteeTypeSpecificPermissionUpdater<T> granteeTypeSpecificPermissionUpdater = getSpecificProjectUpdater(anyPermissionChange);
  56. Set<String> existingPermissions = granteeTypeSpecificPermissionUpdater.loadExistingEntityPermissions(dbSession, anyPermissionChange.getUuidOfGrantee(), entityUuid);
  57. for (T permissionChange : permissionChanges) {
  58. if (granteeTypeSpecificPermissionUpdater.apply(dbSession, existingPermissions, permissionChange) && permissionChange.getProjectUuid() != null) {
  59. projectOrViewUuids.add(permissionChange.getProjectUuid());
  60. }
  61. }
  62. }
  63. private GranteeTypeSpecificPermissionUpdater<T> getSpecificProjectUpdater(T anyPermissionChange) {
  64. return specificPermissionClassToHandler.get(anyPermissionChange.getClass());
  65. }
  66. }