]> source.dussan.org Git - sonarqube.git/blob
9bb07da8b31a8abdaa84f4f783b86fd696030239
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.management;
21
22 import com.google.common.collect.MoreCollectors;
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.Set;
26 import javax.annotation.Priority;
27 import org.sonar.api.server.ServerSide;
28 import org.sonar.db.DbSession;
29
30 import static java.util.function.Function.identity;
31 import static java.util.stream.Collectors.toMap;
32 import static java.util.stream.Collectors.toSet;
33 import static org.sonar.api.utils.Preconditions.checkState;
34
35 @ServerSide
36 @Priority(ManagedInstanceService.DELEGATING_INSTANCE_PRIORITY)
37 public class DelegatingManagedInstanceService implements ManagedInstanceService {
38
39   private final Set<ManagedInstanceService> delegates;
40
41   public DelegatingManagedInstanceService(Set<ManagedInstanceService> delegates) {
42     this.delegates = delegates;
43   }
44
45   public final boolean isInstanceExternallyManaged() {
46     return delegates.stream().anyMatch(ManagedInstanceService::isInstanceExternallyManaged);
47   }
48
49   @Override
50   public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) {
51     return findManagedInstanceService()
52       .map(managedInstanceService -> managedInstanceService.getUserUuidToManaged(dbSession, userUuids))
53       .orElse(returnNonManagedForAllGroups(userUuids));
54   }
55
56   @Override
57   public Map<String, Boolean> getGroupUuidToManaged(DbSession dbSession, Set<String> groupUuids) {
58     return findManagedInstanceService()
59       .map(managedInstanceService -> managedInstanceService.getGroupUuidToManaged(dbSession, groupUuids))
60       .orElse(returnNonManagedForAllGroups(groupUuids));
61   }
62
63   private Optional<ManagedInstanceService> findManagedInstanceService() {
64     Set<ManagedInstanceService> managedInstanceServices = delegates.stream()
65       .filter(ManagedInstanceService::isInstanceExternallyManaged)
66       .collect(toSet());
67
68     checkState(managedInstanceServices.size() < 2,
69       "The instance can't be managed by more than one identity provider and %s were found.", managedInstanceServices.size());
70     return managedInstanceServices.stream().collect(MoreCollectors.toOptional());
71   }
72
73   private static Map<String, Boolean> returnNonManagedForAllGroups(Set<String> resourcesUuid) {
74     return resourcesUuid.stream().collect(toMap(identity(), any -> false));
75   }
76 }