3 * Copyright (C) 2009-2017 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.permission.ws;
22 import com.google.common.base.Optional;
23 import com.google.common.collect.Collections2;
24 import com.google.common.collect.Lists;
25 import com.google.common.collect.Table;
26 import com.google.common.collect.TreeBasedTable;
27 import java.util.List;
28 import javax.annotation.Nullable;
29 import org.sonar.api.resources.ResourceType;
30 import org.sonar.api.resources.ResourceTypes;
31 import org.sonar.api.utils.Paging;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.component.ComponentDto;
35 import org.sonar.db.component.ComponentQuery;
36 import org.sonar.db.permission.CountPerProjectPermission;
37 import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
39 import static java.util.Collections.singletonList;
40 import static org.sonar.api.utils.Paging.forPageIndex;
41 import static org.sonar.server.permission.ws.ProjectWsRef.newOptionalWsProjectRef;
42 import static org.sonar.server.permission.ws.SearchProjectPermissionsData.newBuilder;
44 public class SearchProjectPermissionsDataLoader {
45 private final DbClient dbClient;
46 private final PermissionWsSupport wsSupport;
47 private final String[] rootQualifiers;
49 public SearchProjectPermissionsDataLoader(DbClient dbClient, PermissionWsSupport wsSupport, ResourceTypes resourceTypes) {
50 this.dbClient = dbClient;
51 this.wsSupport = wsSupport;
52 this.rootQualifiers = Collections2.transform(resourceTypes.getRoots(), ResourceType::getQualifier).toArray(new String[resourceTypes.getRoots().size()]);
55 SearchProjectPermissionsData load(DbSession dbSession, SearchProjectPermissionsWsRequest request) {
56 SearchProjectPermissionsData.Builder data = newBuilder();
57 int countRootComponents = countRootComponents(dbSession, request);
58 List<ComponentDto> rootComponents = searchRootComponents(dbSession, request, paging(request, countRootComponents));
59 List<Long> rootComponentIds = Lists.transform(rootComponents, ComponentDto::getId);
61 data.rootComponents(rootComponents)
62 .paging(paging(request, countRootComponents))
63 .userCountByProjectIdAndPermission(userCountByRootComponentIdAndPermission(dbSession, rootComponentIds))
64 .groupCountByProjectIdAndPermission(groupCountByRootComponentIdAndPermission(dbSession, rootComponentIds));
69 private static Paging paging(SearchProjectPermissionsWsRequest request, int total) {
70 return forPageIndex(request.getPage())
71 .withPageSize(request.getPageSize())
75 private int countRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request) {
76 return dbClient.componentDao().countByQuery(dbSession, toDbQuery(request));
79 private List<ComponentDto> searchRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request, Paging paging) {
80 Optional<ProjectWsRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
82 if (project.isPresent()) {
83 return singletonList(wsSupport.getRootComponentOrModule(dbSession, project.get()));
86 return dbClient.componentDao().selectByQuery(dbSession, toDbQuery(request), paging.offset(), paging.pageSize());
89 private ComponentQuery toDbQuery(SearchProjectPermissionsWsRequest wsRequest) {
90 return ComponentQuery.builder()
91 .setQualifiers(qualifiers(wsRequest.getQualifier()))
92 .setNameOrKeyQuery(wsRequest.getQuery())
96 private String[] qualifiers(@Nullable String requestQualifier) {
97 return requestQualifier == null
99 : (new String[] {requestQualifier});
102 private Table<Long, String, Integer> userCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
103 final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create();
105 dbClient.userPermissionDao().countUsersByProjectPermission(dbSession, rootComponentIds).forEach(
106 row -> userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount()));
108 return userCountByRootComponentIdAndPermission;
111 private Table<Long, String, Integer> groupCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
112 final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create();
114 dbClient.groupPermissionDao().groupsCountByComponentIdAndPermission(dbSession, rootComponentIds, context -> {
115 CountPerProjectPermission row = (CountPerProjectPermission) context.getResultObject();
116 userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount());
119 return userCountByRootComponentIdAndPermission;