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.user.ExternalIdentity;
36 import org.sonar.server.user.UpdateUser;
37 import org.sonar.server.user.UserSession;
38 import org.sonar.server.user.UserUpdater;
40 import static com.google.common.base.Preconditions.checkArgument;
41 import static com.google.common.base.Strings.isNullOrEmpty;
42 import static java.lang.String.format;
43 import static org.sonar.auth.ldap.LdapRealm.DEFAULT_LDAP_IDENTITY_PROVIDER_ID;
44 import static org.sonar.auth.ldap.LdapRealm.LDAP_SECURITY_REALM;
45 import static org.sonar.server.user.ExternalIdentity.SQ_AUTHORITY;
46 import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_UPDATE_IDENTITY_PROVIDER;
47 import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_LOGIN;
48 import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_NEW_EXTERNAL_IDENTITY;
49 import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_NEW_EXTERNAL_PROVIDER;
51 public class UpdateIdentityProviderAction implements UsersWsAction {
53 private final DbClient dbClient;
54 private final IdentityProviderRepository identityProviderRepository;
56 private final UserUpdater userUpdater;
57 private final UserSession userSession;
59 public UpdateIdentityProviderAction(DbClient dbClient, IdentityProviderRepository identityProviderRepository, UserUpdater userUpdater, UserSession userSession) {
60 this.dbClient = dbClient;
61 this.identityProviderRepository = identityProviderRepository;
62 this.userUpdater = userUpdater;
63 this.userSession = userSession;
67 public void define(NewController controller) {
68 WebService.NewAction action = controller.createAction(ACTION_UPDATE_IDENTITY_PROVIDER)
69 .setDescription("Update identity provider information. <br/>"
70 + "It's only possible to migrate to an installed identity provider. "
71 + "Be careful that as soon as this information has been updated for a user, "
72 + "the user will only be able to authenticate on the new identity provider. "
73 + "It is not possible to migrate external user to local one.<br/>"
74 + "Requires Administer System permission.")
80 action.createParam(PARAM_LOGIN)
81 .setDescription("User login")
84 action.createParam(PARAM_NEW_EXTERNAL_PROVIDER)
86 .setDescription("New external provider. Only authentication system installed are available. " +
87 "Use 'LDAP' identity provider for single server LDAP setup." +
88 "User 'LDAP_{serverKey}' identity provider for multiple LDAP server setup.");
91 new Change("9.8", String.format("Use of 'sonarqube' for the value of '%s' is deprecated.", PARAM_NEW_EXTERNAL_PROVIDER)));
93 action.createParam(PARAM_NEW_EXTERNAL_IDENTITY)
94 .setDescription("New external identity, usually the login used in the authentication system. "
95 + "If not provided previous identity will be used.");
99 public void handle(Request request, Response response) throws Exception {
100 userSession.checkLoggedIn().checkIsSystemAdministrator();
101 UpdateIdentityProviderRequest wsRequest = toWsRequest(request);
103 response.noContent();
106 private void doHandle(UpdateIdentityProviderRequest request) {
107 checkEnabledIdentityProviders(request.newExternalProvider);
108 try (DbSession dbSession = dbClient.openSession(false)) {
109 UserDto user = getUser(dbSession, request.login);
110 ExternalIdentity externalIdentity = getExternalIdentity(request, user);
111 userUpdater.updateAndCommit(dbSession, user, new UpdateUser().setExternalIdentity(externalIdentity), u -> {
116 private void checkEnabledIdentityProviders(String newExternalProvider) {
117 List<String> allowedIdentityProviders = getAvailableIdentityProviders();
119 boolean isAllowedProvider = allowedIdentityProviders.contains(newExternalProvider) || isLdapIdentityProvider(newExternalProvider);
120 checkArgument(isAllowedProvider, "Value of parameter 'newExternalProvider' (%s) must be one of: [%s] or [%s]", newExternalProvider,
121 String.join(", ", allowedIdentityProviders), String.join(", ", "LDAP", "LDAP_{serverKey}"));
124 private List<String> getAvailableIdentityProviders() {
125 return identityProviderRepository.getAllEnabledAndSorted()
127 .map(IdentityProvider::getKey)
131 private static boolean isLdapIdentityProvider(String identityProviderKey) {
132 return identityProviderKey.startsWith(LDAP_SECURITY_REALM + "_");
135 private UserDto getUser(DbSession dbSession, String login) {
136 UserDto user = dbClient.userDao().selectByLogin(dbSession, login);
137 if (user == null || !user.isActive()) {
138 throw new NotFoundException(format("User '%s' doesn't exist", login));
143 private static ExternalIdentity getExternalIdentity(UpdateIdentityProviderRequest request, UserDto user) {
144 return new ExternalIdentity(
145 request.newExternalProvider,
146 request.newExternalIdentity != null ? request.newExternalIdentity : user.getExternalLogin(),
150 private static UpdateIdentityProviderRequest toWsRequest(Request request) {
151 return UpdateIdentityProviderRequest.builder()
152 .setLogin(request.mandatoryParam(PARAM_LOGIN))
153 .setNewExternalProvider(replaceDeprecatedSonarqubeIdentityProviderByLdapForSonar17508(request.mandatoryParam(PARAM_NEW_EXTERNAL_PROVIDER)))
154 .setNewExternalIdentity(request.param(PARAM_NEW_EXTERNAL_IDENTITY))
158 private static String replaceDeprecatedSonarqubeIdentityProviderByLdapForSonar17508(String newExternalProvider) {
159 return newExternalProvider.equals(SQ_AUTHORITY) || newExternalProvider.equals(LDAP_SECURITY_REALM) ? DEFAULT_LDAP_IDENTITY_PROVIDER_ID : newExternalProvider;
162 static class UpdateIdentityProviderRequest {
163 private final String login;
164 private final String newExternalProvider;
165 private final String newExternalIdentity;
167 public UpdateIdentityProviderRequest(Builder builder) {
168 this.login = builder.login;
169 this.newExternalProvider = builder.newExternalProvider;
170 this.newExternalIdentity = builder.newExternalIdentity;
173 public static UpdateIdentityProviderRequest.Builder builder() {
174 return new UpdateIdentityProviderRequest.Builder();
177 static class Builder {
178 private String login;
179 private String newExternalProvider;
180 private String newExternalIdentity;
183 // enforce factory method use
186 public Builder setLogin(String login) {
191 public Builder setNewExternalProvider(String newExternalProvider) {
192 this.newExternalProvider = newExternalProvider;
196 public Builder setNewExternalIdentity(@Nullable String newExternalIdentity) {
197 this.newExternalIdentity = newExternalIdentity;
201 public UpdateIdentityProviderRequest build() {
202 checkArgument(!isNullOrEmpty(login), "Login is mandatory and must not be empty");
203 checkArgument(!isNullOrEmpty(newExternalProvider), "New External Provider is mandatory and must not be empty");
204 return new UpdateIdentityProviderRequest(this);