3 * Copyright (C) 2009-2021 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.qualityprofile.ws;
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Arrays;
24 import java.util.List;
26 import org.sonar.api.resources.Language;
27 import org.sonar.api.resources.Languages;
28 import org.sonar.api.server.ws.Request;
29 import org.sonar.api.server.ws.Response;
30 import org.sonar.api.server.ws.WebService;
31 import org.sonar.api.server.ws.WebService.SelectionMode;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.qualityprofile.QProfileDto;
35 import org.sonar.db.qualityprofile.SearchUsersQuery;
36 import org.sonar.db.qualityprofile.UserMembershipDto;
37 import org.sonar.db.user.UserDto;
38 import org.sonar.server.issue.AvatarResolver;
39 import org.sonarqube.ws.Common;
40 import org.sonarqube.ws.Qualityprofiles.SearchUsersResponse;
42 import static com.google.common.base.Strings.emptyToNull;
43 import static java.util.Optional.ofNullable;
44 import static org.sonar.api.server.ws.WebService.Param.PAGE;
45 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
46 import static org.sonar.api.server.ws.WebService.Param.SELECTED;
47 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
48 import static org.sonar.api.server.ws.WebService.SelectionMode.ALL;
49 import static org.sonar.api.server.ws.WebService.SelectionMode.DESELECTED;
50 import static org.sonar.api.server.ws.WebService.SelectionMode.fromParam;
51 import static org.sonar.core.util.stream.MoreCollectors.toList;
52 import static org.sonar.core.util.stream.MoreCollectors.toSet;
53 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
54 import static org.sonar.db.Pagination.forPage;
55 import static org.sonar.db.qualityprofile.SearchUsersQuery.ANY;
56 import static org.sonar.db.qualityprofile.SearchUsersQuery.IN;
57 import static org.sonar.db.qualityprofile.SearchUsersQuery.OUT;
58 import static org.sonar.db.qualityprofile.SearchUsersQuery.builder;
59 import static org.sonar.server.ws.WsUtils.writeProtobuf;
60 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SEARCH_USERS;
61 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
62 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
64 public class SearchUsersAction implements QProfileWsAction {
66 private static final Map<SelectionMode, String> MEMBERSHIP = ImmutableMap.of(SelectionMode.SELECTED, IN, DESELECTED, OUT, ALL, ANY);
68 private final DbClient dbClient;
69 private final QProfileWsSupport wsSupport;
70 private final Languages languages;
71 private final AvatarResolver avatarResolver;
73 public SearchUsersAction(DbClient dbClient, QProfileWsSupport wsSupport, Languages languages, AvatarResolver avatarResolver) {
74 this.dbClient = dbClient;
75 this.wsSupport = wsSupport;
76 this.languages = languages;
77 this.avatarResolver = avatarResolver;
81 public void define(WebService.NewController context) {
82 WebService.NewAction action = context
83 .createAction(ACTION_SEARCH_USERS)
84 .setDescription("List the users that are allowed to edit a Quality Profile.<br>" +
85 "Requires one of the following permissions:" +
87 " <li>'Administer Quality Profiles'</li>" +
88 " <li>Edit right on the specified quality profile</li>" +
92 .addSearchQuery("freddy", "names", "logins")
93 .addSelectionModeParam()
95 .setResponseExample(getClass().getResource("search_users-example.json"))
98 action.createParam(PARAM_QUALITY_PROFILE)
99 .setDescription("Quality Profile name")
101 .setExampleValue("Recommended quality profile");
104 .createParam(PARAM_LANGUAGE)
105 .setDescription("Quality profile language")
107 .setPossibleValues(Arrays.stream(languages.all()).map(Language::getKey).collect(toSet()));
111 public void handle(Request request, Response response) throws Exception {
112 SearchUsersRequest wsRequest = buildRequest(request);
113 try (DbSession dbSession = dbClient.openSession(false)) {
114 QProfileDto profile = wsSupport.getProfile(dbSession, wsRequest.getQualityProfile(), wsRequest.getLanguage());
115 wsSupport.checkCanEdit(dbSession, profile);
117 SearchUsersQuery query = builder()
119 .setQuery(wsRequest.getQuery())
120 .setMembership(MEMBERSHIP.get(fromParam(wsRequest.getSelected())))
122 int total = dbClient.qProfileEditUsersDao().countByQuery(dbSession, query);
123 List<UserMembershipDto> usersMembership = dbClient.qProfileEditUsersDao().selectByQuery(dbSession, query,
124 forPage(wsRequest.getPage()).andSize(wsRequest.getPageSize()));
125 Map<String, UserDto> usersById = dbClient.userDao().selectByUuids(dbSession, usersMembership.stream().map(UserMembershipDto::getUserUuid).collect(toList()))
126 .stream().collect(uniqueIndex(UserDto::getUuid));
128 SearchUsersResponse.newBuilder()
129 .addAllUsers(usersMembership.stream()
130 .map(userMembershipDto -> toUser(usersById.get(userMembershipDto.getUserUuid()), userMembershipDto.isSelected()))
132 .setPaging(buildPaging(wsRequest, total)).build(),
137 private static SearchUsersRequest buildRequest(Request request) {
138 return SearchUsersRequest.builder()
139 .setQualityProfile(request.mandatoryParam(PARAM_QUALITY_PROFILE))
140 .setLanguage(request.mandatoryParam(PARAM_LANGUAGE))
141 .setQuery(request.param(TEXT_QUERY))
142 .setSelected(request.mandatoryParam(SELECTED))
143 .setPage(request.mandatoryParamAsInt(PAGE))
144 .setPageSize(request.mandatoryParamAsInt(PAGE_SIZE))
148 private SearchUsersResponse.User toUser(UserDto user, boolean isSelected) {
149 SearchUsersResponse.User.Builder builder = SearchUsersResponse.User.newBuilder()
150 .setLogin(user.getLogin())
151 .setSelected(isSelected);
152 ofNullable(user.getName()).ifPresent(builder::setName);
153 ofNullable(emptyToNull(user.getEmail())).ifPresent(e -> builder.setAvatar(avatarResolver.create(user)));
158 private static Common.Paging buildPaging(SearchUsersRequest wsRequest, int total) {
159 return Common.Paging.newBuilder()
160 .setPageIndex(wsRequest.getPage())
161 .setPageSize(wsRequest.getPageSize())