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.api.utils.Paging;
25 import org.sonar.server.common.SearchResults;
26 import org.sonar.server.common.user.service.UserSearchResult;
27 import org.sonar.server.common.user.service.UserService;
28 import org.sonar.server.common.user.service.UsersSearchRequest;
29 import org.sonar.server.exceptions.ForbiddenException;
30 import org.sonar.server.user.UserSession;
31 import org.sonar.server.v2.api.model.RestPage;
32 import org.sonar.server.v2.api.user.converter.UsersSearchRestResponseGenerator;
33 import org.sonar.server.v2.api.user.model.RestUser;
34 import org.sonar.server.v2.api.user.request.UsersSearchRestRequest;
35 import org.sonar.server.v2.api.user.response.UsersSearchRestResponse;
37 import static org.sonar.api.utils.Paging.forPageIndex;
38 import static org.sonar.server.exceptions.BadRequestException.checkRequest;
40 public class DefaultUserController implements UserController {
41 private final UsersSearchRestResponseGenerator usersSearchResponseGenerator;
42 private final UserService userService;
43 private final UserSession userSession;
45 public DefaultUserController(
46 UserSession userSession,
47 UserService userService,
48 UsersSearchRestResponseGenerator usersSearchResponseGenerator) {
49 this.userSession = userSession;
50 this.usersSearchResponseGenerator = usersSearchResponseGenerator;
51 this.userService = userService;
55 public UsersSearchRestResponse search(UsersSearchRestRequest usersSearchRestRequest, RestPage page) {
56 throwIfAdminOnlyParametersAreUsed(usersSearchRestRequest);
58 SearchResults<UserSearchResult> userSearchResults = userService.findUsers(toUserSearchRequest(usersSearchRestRequest, page));
59 Paging paging = forPageIndex(page.pageIndex()).withPageSize(page.pageSize()).andTotal(userSearchResults.total());
61 return usersSearchResponseGenerator.toUsersForResponse(userSearchResults.searchResults(), paging);
64 private void throwIfAdminOnlyParametersAreUsed(UsersSearchRestRequest usersSearchRestRequest) {
65 if (!userSession.isSystemAdministrator()) {
66 throwIfValuePresent("sonarLintLastConnectionDateFrom", usersSearchRestRequest.sonarLintLastConnectionDateFrom());
67 throwIfValuePresent("sonarLintLastConnectionDateTo", usersSearchRestRequest.sonarLintLastConnectionDateTo());
68 throwIfValuePresent("sonarQubeLastConnectionDateFrom", usersSearchRestRequest.sonarQubeLastConnectionDateFrom());
69 throwIfValuePresent("sonarQubeLastConnectionDateTo", usersSearchRestRequest.sonarQubeLastConnectionDateTo());
73 private static void throwIfValuePresent(String parameter, @Nullable Object value) {
74 Optional.ofNullable(value).ifPresent(v -> throwForbiddenFor(parameter));
77 private static void throwForbiddenFor(String parameterName) {
78 throw new ForbiddenException("parameter " + parameterName + " requires Administer System permission.");
81 private static UsersSearchRequest toUserSearchRequest(UsersSearchRestRequest usersSearchRestRequest, RestPage page) {
82 return UsersSearchRequest.builder()
83 .setDeactivated(Optional.ofNullable(usersSearchRestRequest.active()).map(active -> !active).orElse(false))
84 .setManaged(usersSearchRestRequest.managed())
85 .setQuery(usersSearchRestRequest.q())
86 .setLastConnectionDateFrom(usersSearchRestRequest.sonarQubeLastConnectionDateFrom())
87 .setLastConnectionDateTo(usersSearchRestRequest.sonarQubeLastConnectionDateTo())
88 .setSonarLintLastConnectionDateFrom(usersSearchRestRequest.sonarLintLastConnectionDateFrom())
89 .setSonarLintLastConnectionDateTo(usersSearchRestRequest.sonarLintLastConnectionDateTo())
90 .setPage(page.pageIndex())
91 .setPageSize(page.pageSize())
96 public void deactivate(String login, Boolean anonymize) {
97 userSession.checkLoggedIn().checkIsSystemAdministrator();
98 checkRequest(!login.equals(userSession.getLogin()), "Self-deactivation is not possible");
99 userService.deactivate(login, anonymize);
103 public RestUser fetchUser(String login) {
104 return usersSearchResponseGenerator.toRestUser(userService.fetchUser(login));