3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.monitoring;
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;
32 import static java.util.Collections.emptyList;
33 import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE;
35 public class CommonSystemInformation {
36 private final Configuration config;
37 private final IdentityProviderRepository identityProviderRepository;
38 private final ManagedInstanceService managedInstanceService;
39 private final SecurityRealmFactory securityRealmFactory;
41 public CommonSystemInformation(Configuration config, IdentityProviderRepository identityProviderRepository,
42 ManagedInstanceService managedInstanceService, SecurityRealmFactory securityRealmFactory) {
44 this.identityProviderRepository = identityProviderRepository;
45 this.managedInstanceService = managedInstanceService;
46 this.securityRealmFactory = securityRealmFactory;
49 public boolean getForceAuthentication() {
50 return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE);
53 public List<String> getEnabledIdentityProviders() {
54 return identityProviderRepository.getAllEnabledAndSorted()
56 .filter(IdentityProvider::isEnabled)
57 .map(IdentityProvider::getName)
61 public List<String> getAllowsToSignUpEnabledIdentityProviders() {
62 if (managedInstanceService.isInstanceExternallyManaged()) {
65 return identityProviderRepository.getAllEnabledAndSorted()
67 .filter(IdentityProvider::isEnabled)
68 .filter(IdentityProvider::allowsUsersToSignUp)
69 .map(IdentityProvider::getName)
73 public String getManagedProvider() {
74 if (managedInstanceService.isInstanceExternallyManaged()) {
75 return identityProviderRepository.getAllEnabledAndSorted()
77 .filter(provider -> provider.getKey().equalsIgnoreCase("saml"))
78 .filter(IdentityProvider::isEnabled)
80 .map(IdentityProvider::getName)
87 public String getExternalUserAuthentication() {
88 SecurityRealm realm = securityRealmFactory.getRealm();
89 return realm == null ? "" : realm.getName();