Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* SonarQube
* Copyright (C) 2009-2017 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.permission.ws;
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import com.google.common.io.Resources;
import java.util.List;
import java.util.Optional;
import org.sonar.api.security.DefaultGroups;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
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.organization.OrganizationDto;
import org.sonar.db.permission.GroupPermissionDto;
import org.sonar.db.permission.PermissionQuery;
import org.sonar.db.user.GroupDto;
import org.sonar.server.permission.ProjectId;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsPermissions.Group;
import org.sonarqube.ws.WsPermissions.WsGroupsResponse;
import static java.util.Collections.emptyList;
import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameters;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
public class GroupsAction implements PermissionsWsAction {
private final DbClient dbClient;
private final UserSession userSession;
private final PermissionWsSupport support;
public GroupsAction(DbClient dbClient, UserSession userSession, PermissionWsSupport support) {
this.dbClient = dbClient;
this.userSession = userSession;
this.support = support;
}
@Override
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction("groups")
.setSince("5.2")
.setInternal(true)
.setDescription("Lists the groups with their permissions. " +
"This service defaults to global permissions, but can be limited to project permissions by providing project id or project key. " +
"This service defaults to all groups, but can be limited to groups with a specific permission by providing the desired permission. " +
"Requires one of the following permissions:" +
"
" +
"
'Administer System'
" +
"
'Administer' rights on the specified project
" +
"
")
.addPagingParams(DEFAULT_PAGE_SIZE, RESULTS_MAX_SIZE)
.addSearchQuery("sonar", "names").setDescription("Limit search to group names that contain the supplied string. Must have at least %d characters. " +
"When this parameter is not set, only groups having at least one permission are returned.", SEARCH_QUERY_MIN_LENGTH)
.setResponseExample(Resources.getResource(getClass(), "groups-example.json"))
.setHandler(this);
createOrganizationParameter(action).setSince("6.2");
createPermissionParameter(action).setRequired(false);
createProjectParameters(action);
}
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION));
Optional projectId = support.findProjectId(dbSession, request);
checkProjectAdmin(userSession, org.getUuid(), projectId);
PermissionQuery query = buildPermissionQuery(request, org, projectId);
// TODO validatePermission(groupsRequest.getPermission(), wsProjectRef);
List groups = findGroups(dbSession, org, query);
int total = dbClient.groupPermissionDao().countGroupsByQuery(dbSession, query);
List groupsWithPermission = findGroupPermissions(dbSession, org, groups, projectId);
Paging paging = Paging.forPageIndex(request.mandatoryParamAsInt(Param.PAGE)).withPageSize(query.getPageSize()).andTotal(total);
WsGroupsResponse groupsResponse = buildResponse(groups, groupsWithPermission, paging);
writeProtobuf(groupsResponse, request, response);
}
}
private static PermissionQuery buildPermissionQuery(Request request, OrganizationDto org, Optional project) {
String textQuery = request.param(Param.TEXT_QUERY);
PermissionQuery.Builder permissionQuery = PermissionQuery.builder()
.setOrganizationUuid(org.getUuid())
.setPermission(request.param(PARAM_PERMISSION))
.setPageIndex(request.mandatoryParamAsInt(Param.PAGE))
.setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
.setSearchQuery(textQuery);
if (project.isPresent()) {
permissionQuery.setComponentUuid(project.get().getUuid());
}
if (textQuery == null) {
permissionQuery.withAtLeastOnePermission();
}
return permissionQuery.build();
}
private static WsGroupsResponse buildResponse(List groups, List groupPermissions, Paging paging) {
Multimap permissionsByGroupId = TreeMultimap.create();
groupPermissions.forEach(groupPermission -> permissionsByGroupId.put(groupPermission.getGroupId(), groupPermission.getRole()));
WsGroupsResponse.Builder response = WsGroupsResponse.newBuilder();
groups.forEach(group -> {
Group.Builder wsGroup = response.addGroupsBuilder()
.setName(group.getName());
if (group.getId() != 0) {
wsGroup.setId(String.valueOf(group.getId()));
}
setNullable(group.getDescription(), wsGroup::setDescription);
wsGroup.addAllPermissions(permissionsByGroupId.get(group.getId()));
});
response.getPagingBuilder()
.setPageIndex(paging.pageIndex())
.setPageSize(paging.pageSize())
.setTotal(paging.total());
return response.build();
}
private List findGroups(DbSession dbSession, OrganizationDto org, PermissionQuery dbQuery) {
List orderedNames = dbClient.groupPermissionDao().selectGroupNamesByQuery(dbSession, dbQuery);
List groups = dbClient.groupDao().selectByNames(dbSession, org.getUuid(), orderedNames);
if (orderedNames.contains(DefaultGroups.ANYONE)) {
groups.add(0, new GroupDto().setId(0).setName(DefaultGroups.ANYONE).setOrganizationUuid(org.getUuid()));
}
return Ordering.explicit(orderedNames).onResultOf(GroupDto::getName).immutableSortedCopy(groups);
}
private List findGroupPermissions(DbSession dbSession, OrganizationDto org, List groups, Optional project) {
if (groups.isEmpty()) {
return emptyList();
}
List ids = groups.stream().map(GroupDto::getId).collect(MoreCollectors.toList(groups.size()));
return dbClient.groupPermissionDao().selectByGroupIds(dbSession, org.getUuid(), ids, project.isPresent() ? project.get().getId() : null);
}
}