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.v2.api.user.controller;
22 import java.util.Optional;
23 import javax.annotation.Nullable;
24 import org.sonar.server.common.PaginationInformation;
25 import org.sonar.server.common.SearchResults;
26 import org.sonar.server.common.user.service.UserCreateRequest;
27 import org.sonar.server.common.user.service.UserInformation;
28 import org.sonar.server.common.user.service.UserService;
29 import org.sonar.server.common.user.service.UsersSearchRequest;
30 import org.sonar.server.exceptions.ForbiddenException;
31 import org.sonar.server.user.UpdateUser;
32 import org.sonar.server.user.UserSession;
33 import org.sonar.server.v2.api.model.RestPage;
34 import org.sonar.server.v2.api.user.converter.UsersSearchRestResponseGenerator;
35 import org.sonar.server.v2.api.user.model.RestUser;
36 import org.sonar.server.v2.api.user.request.UserCreateRestRequest;
37 import org.sonar.server.v2.api.user.request.UserUpdateRestRequest;
38 import org.sonar.server.v2.api.user.request.UsersSearchRestRequest;
39 import org.sonar.server.v2.api.user.response.UsersSearchRestResponse;
41 import static org.sonar.server.common.PaginationInformation.forPageIndex;
42 import static org.sonar.server.exceptions.BadRequestException.checkRequest;
44 public class DefaultUserController implements UserController {
45 private final UsersSearchRestResponseGenerator usersSearchResponseGenerator;
46 private final UserService userService;
47 private final UserSession userSession;
49 public DefaultUserController(
50 UserSession userSession,
51 UserService userService,
52 UsersSearchRestResponseGenerator usersSearchResponseGenerator) {
53 this.userSession = userSession;
54 this.usersSearchResponseGenerator = usersSearchResponseGenerator;
55 this.userService = userService;
59 public UsersSearchRestResponse search(UsersSearchRestRequest usersSearchRestRequest, RestPage page) {
60 throwIfAdminOnlyParametersAreUsed(usersSearchRestRequest);
62 SearchResults<UserInformation> userSearchResults = userService.findUsers(toUserSearchRequest(usersSearchRestRequest, page));
63 PaginationInformation paging = forPageIndex(page.pageIndex()).withPageSize(page.pageSize()).andTotal(userSearchResults.total());
65 return usersSearchResponseGenerator.toUsersForResponse(userSearchResults.searchResults(), paging);
68 private void throwIfAdminOnlyParametersAreUsed(UsersSearchRestRequest usersSearchRestRequest) {
69 if (!userSession.isSystemAdministrator()) {
70 throwIfValuePresent("externalIdentity", usersSearchRestRequest.externalIdentity());
71 throwIfValuePresent("sonarLintLastConnectionDateFrom", usersSearchRestRequest.sonarLintLastConnectionDateFrom());
72 throwIfValuePresent("sonarLintLastConnectionDateTo", usersSearchRestRequest.sonarLintLastConnectionDateTo());
73 throwIfValuePresent("sonarQubeLastConnectionDateFrom", usersSearchRestRequest.sonarQubeLastConnectionDateFrom());
74 throwIfValuePresent("sonarQubeLastConnectionDateTo", usersSearchRestRequest.sonarQubeLastConnectionDateTo());
78 private static void throwIfValuePresent(String parameter, @Nullable Object value) {
79 Optional.ofNullable(value).ifPresent(v -> throwForbiddenFor(parameter));
82 private static void throwForbiddenFor(String parameterName) {
83 throw new ForbiddenException("Parameter " + parameterName + " requires Administer System permission.");
86 private static UsersSearchRequest toUserSearchRequest(UsersSearchRestRequest usersSearchRestRequest, RestPage page) {
87 return UsersSearchRequest.builder()
88 .setDeactivated(Optional.ofNullable(usersSearchRestRequest.active()).map(active -> !active).orElse(false))
89 .setManaged(usersSearchRestRequest.managed())
90 .setQuery(usersSearchRestRequest.q())
91 .setExternalLogin(usersSearchRestRequest.externalIdentity())
92 .setLastConnectionDateFrom(usersSearchRestRequest.sonarQubeLastConnectionDateFrom())
93 .setLastConnectionDateTo(usersSearchRestRequest.sonarQubeLastConnectionDateTo())
94 .setSonarLintLastConnectionDateFrom(usersSearchRestRequest.sonarLintLastConnectionDateFrom())
95 .setSonarLintLastConnectionDateTo(usersSearchRestRequest.sonarLintLastConnectionDateTo())
96 .setPage(page.pageIndex())
97 .setPageSize(page.pageSize())
102 public void deactivate(String login, Boolean anonymize) {
103 userSession.checkLoggedIn().checkIsSystemAdministrator();
104 checkRequest(!login.equals(userSession.getLogin()), "Self-deactivation is not possible");
105 userService.deactivate(login, anonymize);
109 public RestUser fetchUser(String login) {
110 return usersSearchResponseGenerator.toRestUser(userService.fetchUser(login));
114 public RestUser updateUser(String login, UserUpdateRestRequest updateRequest) {
115 userSession.checkLoggedIn().checkIsSystemAdministrator();
116 UpdateUser update = toUpdateUser(updateRequest);
117 UserInformation updatedUser = userService.updateUser(login, update);
118 return usersSearchResponseGenerator.toRestUser(updatedUser);
121 private static UpdateUser toUpdateUser(UserUpdateRestRequest updateRequest) {
122 UpdateUser update = new UpdateUser();
123 updateRequest.getName().applyIfDefined(update::setName);
124 updateRequest.getEmail().applyIfDefined(update::setEmail);
125 updateRequest.getScmAccounts().applyIfDefined(update::setScmAccounts);
130 public RestUser create(UserCreateRestRequest userCreateRestRequest) {
131 userSession.checkLoggedIn().checkIsSystemAdministrator();
132 UserCreateRequest userCreateRequest = toUserCreateRequest(userCreateRestRequest);
133 return usersSearchResponseGenerator.toRestUser(userService.createUser(userCreateRequest));
136 private static UserCreateRequest toUserCreateRequest(UserCreateRestRequest userCreateRestRequest) {
137 return UserCreateRequest.builder()
138 .setEmail(userCreateRestRequest.email())
139 .setLocal(userCreateRestRequest.local())
140 .setLogin(userCreateRestRequest.login())
141 .setName(userCreateRestRequest.name())
142 .setPassword(userCreateRestRequest.password())
143 .setScmAccounts(userCreateRestRequest.scmAccounts())