]> source.dussan.org Git - sonarqube.git/blob
ef9dabdcb7e081c19fb46fec9a189fc005834311
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.permission.ws;
21
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;
38
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;
43
44 public class SearchProjectPermissionsDataLoader {
45   private final DbClient dbClient;
46   private final PermissionWsSupport wsSupport;
47   private final String[] rootQualifiers;
48
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()]);
53   }
54
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);
60
61     data.rootComponents(rootComponents)
62       .paging(paging(request, countRootComponents))
63       .userCountByProjectIdAndPermission(userCountByRootComponentIdAndPermission(dbSession, rootComponentIds))
64       .groupCountByProjectIdAndPermission(groupCountByRootComponentIdAndPermission(dbSession, rootComponentIds));
65
66     return data.build();
67   }
68
69   private static Paging paging(SearchProjectPermissionsWsRequest request, int total) {
70     return forPageIndex(request.getPage())
71       .withPageSize(request.getPageSize())
72       .andTotal(total);
73   }
74
75   private int countRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request) {
76     return dbClient.componentDao().countByQuery(dbSession, toDbQuery(request));
77   }
78
79   private List<ComponentDto> searchRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request, Paging paging) {
80     Optional<ProjectWsRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
81
82     if (project.isPresent()) {
83       return singletonList(wsSupport.getRootComponentOrModule(dbSession, project.get()));
84     }
85
86     return dbClient.componentDao().selectByQuery(dbSession, toDbQuery(request), paging.offset(), paging.pageSize());
87   }
88
89   private ComponentQuery toDbQuery(SearchProjectPermissionsWsRequest wsRequest) {
90     return ComponentQuery.builder()
91       .setQualifiers(qualifiers(wsRequest.getQualifier()))
92       .setNameOrKeyQuery(wsRequest.getQuery())
93       .build();
94   }
95
96   private String[] qualifiers(@Nullable String requestQualifier) {
97     return requestQualifier == null
98       ? rootQualifiers
99       : (new String[] {requestQualifier});
100   }
101
102   private Table<Long, String, Integer> userCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
103     final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create();
104
105     dbClient.userPermissionDao().countUsersByProjectPermission(dbSession, rootComponentIds).forEach(
106       row -> userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount()));
107
108     return userCountByRootComponentIdAndPermission;
109   }
110
111   private Table<Long, String, Integer> groupCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
112     final Table<Long, String, Integer> userCountByRootComponentIdAndPermission = TreeBasedTable.create();
113
114     dbClient.groupPermissionDao().groupsCountByComponentIdAndPermission(dbSession, rootComponentIds, context -> {
115       CountPerProjectPermission row = (CountPerProjectPermission) context.getResultObject();
116       userCountByRootComponentIdAndPermission.put(row.getComponentId(), row.getPermission(), row.getCount());
117     });
118
119     return userCountByRootComponentIdAndPermission;
120   }
121 }