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.user.ws;
22 import java.util.List;
23 import javax.annotation.Nullable;
24 import org.sonar.api.server.authentication.IdentityProvider;
25 import org.sonar.api.server.ws.Change;
26 import org.sonar.api.server.ws.Request;
27 import org.sonar.api.server.ws.Response;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.api.server.ws.WebService.NewController;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.user.UserDto;
33 import org.sonar.server.authentication.IdentityProviderRepository;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.management.ManagedInstanceChecker;
36 import org.sonar.server.user.ExternalIdentity;
37 import org.sonar.server.user.UpdateUser;
38 import org.sonar.server.user.UserSession;
39 import org.sonar.server.user.UserUpdater;
41 import static com.google.common.base.Preconditions.checkArgument;
42 import static com.google.common.base.Strings.isNullOrEmpty;
43 import static java.lang.String.format;
44 import static org.sonar.auth.ldap.LdapRealm.DEFAULT_LDAP_IDENTITY_PROVIDER_ID;
45 import static org.sonar.auth.ldap.LdapRealm.LDAP_SECURITY_REALM;
46 import static org.sonar.server.user.ExternalIdentity.SQ_AUTHORITY;
47 import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_UPDATE_IDENTITY_PROVIDER;
48 import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_LOGIN;
49 import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_NEW_EXTERNAL_IDENTITY;
50 import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_NEW_EXTERNAL_PROVIDER;
52 public class UpdateIdentityProviderAction implements UsersWsAction {
54 private final DbClient dbClient;
55 private final IdentityProviderRepository identityProviderRepository;
56 private final UserUpdater userUpdater;
57 private final UserSession userSession;
58 private final ManagedInstanceChecker managedInstanceChecker;
60 public UpdateIdentityProviderAction(DbClient dbClient, IdentityProviderRepository identityProviderRepository, UserUpdater userUpdater, UserSession userSession, ManagedInstanceChecker managedInstanceChecker) {
61 this.dbClient = dbClient;
62 this.identityProviderRepository = identityProviderRepository;
63 this.userUpdater = userUpdater;
64 this.userSession = userSession;
65 this.managedInstanceChecker = managedInstanceChecker;
69 public void define(NewController controller) {
70 WebService.NewAction action = controller.createAction(ACTION_UPDATE_IDENTITY_PROVIDER)
71 .setDescription("Update identity provider information. <br/>"
72 + "It's only possible to migrate to an installed identity provider. "
73 + "Be careful that as soon as this information has been updated for a user, "
74 + "the user will only be able to authenticate on the new identity provider. "
75 + "It is not possible to migrate external user to local one.<br/>"
76 + "Requires Administer System permission.")
82 action.createParam(PARAM_LOGIN)
83 .setDescription("User login")
86 action.createParam(PARAM_NEW_EXTERNAL_PROVIDER)
88 .setDescription("New external provider. Only authentication system installed are available. " +
89 "Use 'LDAP' identity provider for single server LDAP setup." +
90 "User 'LDAP_{serverKey}' identity provider for multiple LDAP server setup.");
93 new Change("9.8", String.format("Use of 'sonarqube' for the value of '%s' is deprecated.", PARAM_NEW_EXTERNAL_PROVIDER)));
95 action.createParam(PARAM_NEW_EXTERNAL_IDENTITY)
96 .setDescription("New external identity, usually the login used in the authentication system. "
97 + "If not provided previous identity will be used.");
101 public void handle(Request request, Response response) throws Exception {
102 userSession.checkLoggedIn().checkIsSystemAdministrator();
103 managedInstanceChecker.throwIfInstanceIsManaged();
104 UpdateIdentityProviderRequest wsRequest = toWsRequest(request);
106 response.noContent();
109 private void doHandle(UpdateIdentityProviderRequest request) {
110 checkEnabledIdentityProviders(request.newExternalProvider);
111 try (DbSession dbSession = dbClient.openSession(false)) {
112 UserDto user = getUser(dbSession, request.login);
113 ExternalIdentity externalIdentity = getExternalIdentity(request, user);
114 userUpdater.updateAndCommit(dbSession, user, new UpdateUser().setExternalIdentity(externalIdentity), u -> {
119 private void checkEnabledIdentityProviders(String newExternalProvider) {
120 List<String> allowedIdentityProviders = getAvailableIdentityProviders();
122 boolean isAllowedProvider = allowedIdentityProviders.contains(newExternalProvider) || isLdapIdentityProvider(newExternalProvider);
123 checkArgument(isAllowedProvider, "Value of parameter 'newExternalProvider' (%s) must be one of: [%s] or [%s]", newExternalProvider,
124 String.join(", ", allowedIdentityProviders), String.join(", ", "LDAP", "LDAP_{serverKey}"));
127 private List<String> getAvailableIdentityProviders() {
128 return identityProviderRepository.getAllEnabledAndSorted()
130 .map(IdentityProvider::getKey)
134 private static boolean isLdapIdentityProvider(String identityProviderKey) {
135 return identityProviderKey.startsWith(LDAP_SECURITY_REALM + "_");
138 private UserDto getUser(DbSession dbSession, String login) {
139 UserDto user = dbClient.userDao().selectByLogin(dbSession, login);
140 if (user == null || !user.isActive()) {
141 throw new NotFoundException(format("User '%s' doesn't exist", login));
146 private static ExternalIdentity getExternalIdentity(UpdateIdentityProviderRequest request, UserDto user) {
147 return new ExternalIdentity(
148 request.newExternalProvider,
149 request.newExternalIdentity != null ? request.newExternalIdentity : user.getExternalLogin(),
153 private static UpdateIdentityProviderRequest toWsRequest(Request request) {
154 return UpdateIdentityProviderRequest.builder()
155 .setLogin(request.mandatoryParam(PARAM_LOGIN))
156 .setNewExternalProvider(replaceDeprecatedSonarqubeIdentityProviderByLdapForSonar17508(request.mandatoryParam(PARAM_NEW_EXTERNAL_PROVIDER)))
157 .setNewExternalIdentity(request.param(PARAM_NEW_EXTERNAL_IDENTITY))
161 private static String replaceDeprecatedSonarqubeIdentityProviderByLdapForSonar17508(String newExternalProvider) {
162 return newExternalProvider.equals(SQ_AUTHORITY) || newExternalProvider.equals(LDAP_SECURITY_REALM) ? DEFAULT_LDAP_IDENTITY_PROVIDER_ID : newExternalProvider;
165 static class UpdateIdentityProviderRequest {
166 private final String login;
167 private final String newExternalProvider;
168 private final String newExternalIdentity;
170 public UpdateIdentityProviderRequest(Builder builder) {
171 this.login = builder.login;
172 this.newExternalProvider = builder.newExternalProvider;
173 this.newExternalIdentity = builder.newExternalIdentity;
176 public static UpdateIdentityProviderRequest.Builder builder() {
177 return new UpdateIdentityProviderRequest.Builder();
180 static class Builder {
181 private String login;
182 private String newExternalProvider;
183 private String newExternalIdentity;
186 // enforce factory method use
189 public Builder setLogin(String login) {
194 public Builder setNewExternalProvider(String newExternalProvider) {
195 this.newExternalProvider = newExternalProvider;
199 public Builder setNewExternalIdentity(@Nullable String newExternalIdentity) {
200 this.newExternalIdentity = newExternalIdentity;
204 public UpdateIdentityProviderRequest build() {
205 checkArgument(!isNullOrEmpty(login), "Login is mandatory and must not be empty");
206 checkArgument(!isNullOrEmpty(newExternalProvider), "New External Provider is mandatory and must not be empty");
207 return new UpdateIdentityProviderRequest(this);