/* * SonarQube * Copyright (C) 2009-2020 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package org.sonar.server.usergroups.ws; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService.NewController; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.Paging; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.user.GroupDto; import org.sonar.server.es.SearchOptions; import org.sonar.server.user.UserSession; import org.sonar.server.usergroups.DefaultGroupFinder; import static java.util.Optional.ofNullable; import static org.apache.commons.lang.StringUtils.defaultIfBlank; import static org.sonar.api.utils.Paging.forPageIndex; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.UserGroups.Group; import static org.sonarqube.ws.UserGroups.SearchWsResponse; public class SearchAction implements UserGroupsWsAction { private static final String FIELD_NAME = "name"; private static final String FIELD_DESCRIPTION = "description"; private static final String FIELD_MEMBERS_COUNT = "membersCount"; private static final List ALL_FIELDS = Arrays.asList(FIELD_NAME, FIELD_DESCRIPTION, FIELD_MEMBERS_COUNT); private final DbClient dbClient; private final UserSession userSession; private final DefaultGroupFinder defaultGroupFinder; public SearchAction(DbClient dbClient, UserSession userSession, DefaultGroupFinder defaultGroupFinder) { this.dbClient = dbClient; this.userSession = userSession; this.defaultGroupFinder = defaultGroupFinder; } @Override public void define(NewController context) { context.createAction("search") .setDescription("Search for user groups.
" + "Requires the following permission: 'Administer System'.") .setHandler(this) .setResponseExample(getClass().getResource("search-example.json")) .setSince("5.2") .addFieldsParam(ALL_FIELDS) .addPagingParams(100, MAX_PAGE_SIZE) .addSearchQuery("sonar-users", "names") .setChangelog( new Change("8.4", "Field 'id' in the response is deprecated. Format changes from integer to string."), new Change("6.4", "Paging response fields moved to a Paging object"), new Change("6.4", "'default' response field has been added")); } @Override public void handle(Request request, Response response) throws Exception { int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); SearchOptions options = new SearchOptions() .setPage(page, pageSize); String query = defaultIfBlank(request.param(Param.TEXT_QUERY), ""); Set fields = neededFields(request); try (DbSession dbSession = dbClient.openSession(false)) { userSession.checkLoggedIn().checkPermission(ADMINISTER); GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession); int limit = dbClient.groupDao().countByQuery(dbSession, query); Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(limit); List groups = dbClient.groupDao().selectByQuery(dbSession, query, options.getOffset(), pageSize); List groupUuids = groups.stream().map(GroupDto::getUuid).collect(MoreCollectors.toList(groups.size())); Map userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupUuids); writeProtobuf(buildResponse(groups, userCountByGroup, fields, paging, defaultGroup), request, response); } } private static Set neededFields(Request request) { Set fields = new HashSet<>(); List fieldsFromRequest = request.paramAsStrings(Param.FIELDS); if (fieldsFromRequest == null || fieldsFromRequest.isEmpty()) { fields.addAll(ALL_FIELDS); } else { fields.addAll(fieldsFromRequest); } return fields; } private static SearchWsResponse buildResponse(List groups, Map userCountByGroup, Set fields, Paging paging, GroupDto defaultGroup) { SearchWsResponse.Builder responseBuilder = SearchWsResponse.newBuilder(); groups.forEach(group -> responseBuilder .addGroups(toWsGroup(group, userCountByGroup.get(group.getName()), fields, defaultGroup.getUuid().equals(group.getUuid())))); responseBuilder.getPagingBuilder() .setPageIndex(paging.pageIndex()) .setPageSize(paging.pageSize()) .setTotal(paging.total()) .build(); return responseBuilder.build(); } private static Group toWsGroup(GroupDto group, Integer memberCount, Set fields, boolean isDefault) { Group.Builder groupBuilder = Group.newBuilder() .setId(group.getUuid()) .setDefault(isDefault); if (fields.contains(FIELD_NAME)) { groupBuilder.setName(group.getName()); } if (fields.contains(FIELD_DESCRIPTION)) { ofNullable(group.getDescription()).ifPresent(groupBuilder::setDescription); } if (fields.contains(FIELD_MEMBERS_COUNT)) { groupBuilder.setMembersCount(memberCount); } return groupBuilder.build(); } }