]> source.dussan.org Git - sonarqube.git/blob
d4456bcdb3d372877d39c20f32c06d85746fad60
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.qualityprofile.ws;
21
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Map;
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;
41
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;
63
64 public class SearchUsersAction implements QProfileWsAction {
65
66   private static final Map<SelectionMode, String> MEMBERSHIP = ImmutableMap.of(SelectionMode.SELECTED, IN, DESELECTED, OUT, ALL, ANY);
67
68   private final DbClient dbClient;
69   private final QProfileWsSupport wsSupport;
70   private final Languages languages;
71   private final AvatarResolver avatarResolver;
72
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;
78   }
79
80   @Override
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:" +
86         "<ul>" +
87         "  <li>'Administer Quality Profiles'</li>" +
88         "  <li>Edit right on the specified quality profile</li>" +
89         "</ul>")
90       .setHandler(this)
91       .setInternal(true)
92       .addSearchQuery("freddy", "names", "logins")
93       .addSelectionModeParam()
94       .addPagingParams(25)
95       .setResponseExample(getClass().getResource("search_users-example.json"))
96       .setSince("6.6");
97
98     action.createParam(PARAM_QUALITY_PROFILE)
99       .setDescription("Quality Profile name")
100       .setRequired(true)
101       .setExampleValue("Recommended quality profile");
102
103     action
104       .createParam(PARAM_LANGUAGE)
105       .setDescription("Quality profile language")
106       .setRequired(true)
107       .setPossibleValues(Arrays.stream(languages.all()).map(Language::getKey).collect(toSet()));
108   }
109
110   @Override
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);
116
117       SearchUsersQuery query = builder()
118         .setProfile(profile)
119         .setQuery(wsRequest.getQuery())
120         .setMembership(MEMBERSHIP.get(fromParam(wsRequest.getSelected())))
121         .build();
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));
127       writeProtobuf(
128         SearchUsersResponse.newBuilder()
129           .addAllUsers(usersMembership.stream()
130             .map(userMembershipDto -> toUser(usersById.get(userMembershipDto.getUserUuid()), userMembershipDto.isSelected()))
131             .collect(toList()))
132           .setPaging(buildPaging(wsRequest, total)).build(),
133         request, response);
134     }
135   }
136
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))
145       .build();
146   }
147
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)));
154     return builder
155       .build();
156   }
157
158   private static Common.Paging buildPaging(SearchUsersRequest wsRequest, int total) {
159     return Common.Paging.newBuilder()
160       .setPageIndex(wsRequest.getPage())
161       .setPageSize(wsRequest.getPageSize())
162       .setTotal(total)
163       .build();
164   }
165
166 }