]> source.dussan.org Git - sonarqube.git/blob
f37d4bdaeb73172b5e8375d8a75bf9640e8117c6
[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.platform.monitoring;
21
22 import java.util.List;
23 import javax.annotation.CheckForNull;
24 import org.sonar.api.CoreProperties;
25 import org.sonar.api.config.Configuration;
26 import org.sonar.api.security.SecurityRealm;
27 import org.sonar.api.server.authentication.IdentityProvider;
28 import org.sonar.server.authentication.IdentityProviderRepository;
29 import org.sonar.server.management.ManagedInstanceService;
30 import org.sonar.server.user.SecurityRealmFactory;
31
32 import static java.util.Collections.emptyList;
33 import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE;
34
35 public class CommonSystemInformation {
36   private final Configuration config;
37   private final IdentityProviderRepository identityProviderRepository;
38   private final ManagedInstanceService managedInstanceService;
39   private final SecurityRealmFactory securityRealmFactory;
40
41   public CommonSystemInformation(Configuration config, IdentityProviderRepository identityProviderRepository,
42     ManagedInstanceService managedInstanceService, SecurityRealmFactory securityRealmFactory) {
43     this.config = config;
44     this.identityProviderRepository = identityProviderRepository;
45     this.managedInstanceService = managedInstanceService;
46     this.securityRealmFactory = securityRealmFactory;
47   }
48
49   public boolean getForceAuthentication() {
50     return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE);
51   }
52
53   public List<String> getEnabledIdentityProviders() {
54     return identityProviderRepository.getAllEnabledAndSorted()
55       .stream()
56       .filter(IdentityProvider::isEnabled)
57       .map(IdentityProvider::getName)
58       .toList();
59   }
60
61   public List<String> getAllowsToSignUpEnabledIdentityProviders() {
62     if (managedInstanceService.isInstanceExternallyManaged()) {
63       return emptyList();
64     }
65     return identityProviderRepository.getAllEnabledAndSorted()
66       .stream()
67       .filter(IdentityProvider::isEnabled)
68       .filter(IdentityProvider::allowsUsersToSignUp)
69       .map(IdentityProvider::getName)
70       .toList();
71   }
72
73   public String getManagedProvider() {
74     if (managedInstanceService.isInstanceExternallyManaged()) {
75       return identityProviderRepository.getAllEnabledAndSorted()
76         .stream()
77         .filter(provider -> provider.getKey().equalsIgnoreCase("saml"))
78         .filter(IdentityProvider::isEnabled)
79         .findFirst()
80         .map(IdentityProvider::getName)
81         .orElse("");
82     }
83     return "";
84   }
85
86   @CheckForNull
87   public String getExternalUserAuthentication() {
88     SecurityRealm realm = securityRealmFactory.getRealm();
89     return realm == null ? "" : realm.getName();
90   }
91 }