3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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 java.util.Locale;
23 import java.util.Optional;
24 import org.sonar.api.i18n.I18n;
25 import org.sonar.api.resources.ResourceTypes;
26 import org.sonar.api.server.ws.Request;
27 import org.sonar.api.server.ws.Response;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.api.server.ws.WebService.Param;
30 import org.sonar.api.utils.Paging;
31 import org.sonar.core.permission.ProjectPermissions;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.component.ComponentDto;
35 import org.sonar.server.permission.ProjectId;
36 import org.sonar.server.user.UserSession;
37 import org.sonarqube.ws.Common;
38 import org.sonarqube.ws.WsPermissions.Permission;
39 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse;
40 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse.Project;
41 import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
43 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
44 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
45 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameters;
46 import static org.sonar.server.permission.ws.ProjectWsRef.newOptionalWsProjectRef;
47 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
48 import static org.sonar.server.ws.WsParameterBuilder.createRootQualifierParameter;
49 import static org.sonar.server.ws.WsUtils.writeProtobuf;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
54 public class SearchProjectPermissionsAction implements PermissionsWsAction {
55 private static final String PROPERTY_PREFIX = "projects_role.";
56 private static final String DESCRIPTION_SUFFIX = ".desc";
58 private final DbClient dbClient;
59 private final UserSession userSession;
60 private final I18n i18n;
61 private final ResourceTypes resourceTypes;
62 private final SearchProjectPermissionsDataLoader dataLoader;
63 private final PermissionWsSupport wsSupport;
65 public SearchProjectPermissionsAction(DbClient dbClient, UserSession userSession, I18n i18n, ResourceTypes resourceTypes,
66 SearchProjectPermissionsDataLoader dataLoader, PermissionWsSupport wsSupport) {
67 this.dbClient = dbClient;
68 this.userSession = userSession;
70 this.resourceTypes = resourceTypes;
71 this.dataLoader = dataLoader;
72 this.wsSupport = wsSupport;
76 public void define(WebService.NewController context) {
77 WebService.NewAction action = context.createAction("search_project_permissions")
78 .setDescription("List project permissions. A project can be a technical project, a view or a developer.<br />" +
79 "Requires one of the following permissions:" +
81 "<li>'Administer System'</li>" +
82 "<li>'Administer' rights on the specified project</li>" +
84 .setResponseExample(getClass().getResource("search_project_permissions-example.json"))
89 action.createParam(Param.TEXT_QUERY)
90 .setDescription("Limit search to: <ul>" +
91 "<li>project names that contain the supplied string</li>" +
92 "<li>project keys that are exactly the same as the supplied string</li>" +
94 .setExampleValue("apac");
95 createProjectParameters(action);
96 createRootQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes))
101 public void handle(Request wsRequest, Response wsResponse) throws Exception {
102 SearchProjectPermissionsWsResponse searchProjectPermissionsWsResponse = doHandle(toSearchProjectPermissionsWsRequest(wsRequest));
103 writeProtobuf(searchProjectPermissionsWsResponse, wsRequest, wsResponse);
106 private SearchProjectPermissionsWsResponse doHandle(SearchProjectPermissionsWsRequest request) {
107 try (DbSession dbSession = dbClient.openSession(false)) {
108 checkAuthorized(dbSession, request);
109 validateQualifier(request.getQualifier(), resourceTypes);
110 SearchProjectPermissionsData data = dataLoader.load(dbSession, request);
111 return buildResponse(data);
115 private static SearchProjectPermissionsWsRequest toSearchProjectPermissionsWsRequest(Request request) {
116 return new SearchProjectPermissionsWsRequest()
117 .setProjectId(request.param(PARAM_PROJECT_ID))
118 .setProjectKey(request.param(PARAM_PROJECT_KEY))
119 .setQualifier(request.param(PARAM_QUALIFIER))
120 .setPage(request.mandatoryParamAsInt(Param.PAGE))
121 .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
122 .setQuery(request.param(Param.TEXT_QUERY));
125 private void checkAuthorized(DbSession dbSession, SearchProjectPermissionsWsRequest request) {
126 com.google.common.base.Optional<ProjectWsRef> projectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
127 Optional<ProjectId> projectId;
128 if (projectRef.isPresent()) {
129 projectId = Optional.of(wsSupport.findProject(dbSession, projectRef.get()));
131 projectId = Optional.empty();
133 checkProjectAdmin(userSession, projectId);
136 private SearchProjectPermissionsWsResponse buildResponse(SearchProjectPermissionsData data) {
137 SearchProjectPermissionsWsResponse.Builder response = SearchProjectPermissionsWsResponse.newBuilder();
138 Permission.Builder permissionResponse = Permission.newBuilder();
140 Project.Builder rootComponentBuilder = Project.newBuilder();
141 for (ComponentDto rootComponent : data.rootComponents()) {
144 .setId(rootComponent.uuid())
145 .setKey(rootComponent.key())
146 .setQualifier(rootComponent.qualifier())
147 .setName(rootComponent.name());
148 for (String permission : data.permissions(rootComponent.getId())) {
149 rootComponentBuilder.addPermissions(
153 .setUsersCount(data.userCount(rootComponent.getId(), permission))
154 .setGroupsCount(data.groupCount(rootComponent.getId(), permission)));
156 response.addProjects(rootComponentBuilder);
159 for (String permissionKey : ProjectPermissions.ALL) {
160 response.addPermissions(
163 .setKey(permissionKey)
164 .setName(i18nName(permissionKey))
165 .setDescription(i18nDescriptionMessage(permissionKey)));
168 Paging paging = data.paging();
170 Common.Paging.newBuilder()
171 .setPageIndex(paging.pageIndex())
172 .setPageSize(paging.pageSize())
173 .setTotal(paging.total()));
175 return response.build();
178 private String i18nDescriptionMessage(String permissionKey) {
179 return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
182 private String i18nName(String permissionKey) {
183 return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey, permissionKey);