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 com.google.common.base.Optional;
23 import org.sonar.api.i18n.I18n;
24 import org.sonar.api.resources.ResourceTypes;
25 import org.sonar.api.server.ws.Request;
26 import org.sonar.api.server.ws.Response;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.api.server.ws.WebService.Param;
29 import org.sonar.api.utils.Paging;
30 import org.sonar.core.permission.ProjectPermissions;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.server.user.UserSession;
35 import org.sonarqube.ws.Common;
36 import org.sonarqube.ws.WsPermissions.Permission;
37 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse;
38 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse.Project;
39 import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
41 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
42 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentKey;
43 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentUuid;
44 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
45 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameter;
46 import static org.sonar.server.permission.ws.WsProjectRef.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;
64 public SearchProjectPermissionsAction(DbClient dbClient, UserSession userSession, I18n i18n, ResourceTypes resourceTypes, SearchProjectPermissionsDataLoader dataLoader) {
65 this.dbClient = dbClient;
66 this.userSession = userSession;
68 this.resourceTypes = resourceTypes;
69 this.dataLoader = dataLoader;
73 public void define(WebService.NewController context) {
74 WebService.NewAction action = context.createAction("search_project_permissions")
75 .setDescription("List project permissions. A project can be a technical project, a view or a developer.<br />" +
76 "Requires 'Administer System' permission or 'Administer' rights on the specified project.")
77 .setResponseExample(getClass().getResource("search_project_permissions-example.json"))
80 .addSearchQuery("sonarq", "project names", "project keys")
83 createProjectParameter(action);
84 createRootQualifierParameter(action, newQualifierParameterContext(userSession, i18n, resourceTypes))
89 public void handle(Request wsRequest, Response wsResponse) throws Exception {
90 SearchProjectPermissionsWsResponse searchProjectPermissionsWsResponse = doHandle(toSearchProjectPermissionsWsRequest(wsRequest));
91 writeProtobuf(searchProjectPermissionsWsResponse, wsRequest, wsResponse);
94 private SearchProjectPermissionsWsResponse doHandle(SearchProjectPermissionsWsRequest request) {
95 checkRequestAndPermissions(request);
96 DbSession dbSession = dbClient.openSession(false);
98 validateQualifier(request.getQualifier(), resourceTypes);
99 SearchProjectPermissionsData data = dataLoader.load(request);
100 return buildResponse(data);
102 dbClient.closeSession(dbSession);
106 private static SearchProjectPermissionsWsRequest toSearchProjectPermissionsWsRequest(Request request) {
107 return new SearchProjectPermissionsWsRequest()
108 .setProjectId(request.param(PARAM_PROJECT_ID))
109 .setProjectKey(request.param(PARAM_PROJECT_KEY))
110 .setQualifier(request.param(PARAM_QUALIFIER))
111 .setPage(request.mandatoryParamAsInt(Param.PAGE))
112 .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
113 .setQuery(request.param(Param.TEXT_QUERY));
116 private void checkRequestAndPermissions(SearchProjectPermissionsWsRequest request) {
117 Optional<WsProjectRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
118 boolean hasProject = project.isPresent();
119 boolean hasProjectUuid = hasProject && project.get().uuid() != null;
120 boolean hasProjectKey = hasProject && project.get().key() != null;
122 if (hasProjectUuid) {
123 checkProjectAdminUserByComponentUuid(userSession, project.get().uuid());
124 } else if (hasProjectKey) {
125 checkProjectAdminUserByComponentKey(userSession, project.get().key());
127 checkGlobalAdminUser(userSession);
131 private SearchProjectPermissionsWsResponse buildResponse(SearchProjectPermissionsData data) {
132 SearchProjectPermissionsWsResponse.Builder response = SearchProjectPermissionsWsResponse.newBuilder();
133 Permission.Builder permissionResponse = Permission.newBuilder();
135 Project.Builder rootComponentBuilder = Project.newBuilder();
136 for (ComponentDto rootComponent : data.rootComponents()) {
139 .setId(rootComponent.uuid())
140 .setKey(rootComponent.key())
141 .setQualifier(rootComponent.qualifier())
142 .setName(rootComponent.name());
143 for (String permission : data.permissions(rootComponent.getId())) {
144 rootComponentBuilder.addPermissions(
148 .setUsersCount(data.userCount(rootComponent.getId(), permission))
149 .setGroupsCount(data.groupCount(rootComponent.getId(), permission)));
151 response.addProjects(rootComponentBuilder);
154 for (String permissionKey : ProjectPermissions.ALL) {
155 response.addPermissions(
158 .setKey(permissionKey)
159 .setName(i18nName(permissionKey))
160 .setDescription(i18nDescriptionMessage(permissionKey)));
163 Paging paging = data.paging();
165 Common.Paging.newBuilder()
166 .setPageIndex(paging.pageIndex())
167 .setPageSize(paging.pageSize())
168 .setTotal(paging.total()));
170 return response.build();
173 private String i18nDescriptionMessage(String permissionKey) {
174 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
177 private String i18nName(String permissionKey) {
178 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey, permissionKey);