2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.permission.ws;
23 import com.google.common.base.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.user.UserSession;
36 import org.sonarqube.ws.Common;
37 import org.sonarqube.ws.WsPermissions.Permission;
38 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse;
39 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse.Project;
40 import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
42 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
43 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentKey;
44 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentUuid;
45 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
46 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.QualifierParameterContext.newQualifierParameterContext;
47 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameter;
48 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createQualifierParameter;
49 import static org.sonar.server.permission.ws.WsProjectRef.newOptionalWsProjectRef;
50 import static org.sonar.server.ws.WsUtils.writeProtobuf;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
55 public class SearchProjectPermissionsAction implements PermissionsWsAction {
56 private static final String PROPERTY_PREFIX = "projects_role.";
57 private static final String DESCRIPTION_SUFFIX = ".desc";
59 private final DbClient dbClient;
60 private final UserSession userSession;
61 private final I18n i18n;
62 private final ResourceTypes resourceTypes;
63 private final SearchProjectPermissionsDataLoader dataLoader;
65 public SearchProjectPermissionsAction(DbClient dbClient, UserSession userSession, I18n i18n, ResourceTypes resourceTypes, SearchProjectPermissionsDataLoader dataLoader) {
66 this.dbClient = dbClient;
67 this.userSession = userSession;
69 this.resourceTypes = resourceTypes;
70 this.dataLoader = dataLoader;
74 public void define(WebService.NewController context) {
75 WebService.NewAction action = context.createAction("search_project_permissions")
76 .setDescription("List project permissions. A project can be a technical project, a view or a developer.<br />" +
77 "Requires 'Administer System' permission or 'Administer' rights on the specified project.")
78 .setResponseExample(getClass().getResource("search_project_permissions-example.json"))
81 .addSearchQuery("sonarq", "project names", "project keys")
84 createProjectParameter(action);
85 createQualifierParameter(action, newQualifierParameterContext(userSession, i18n, resourceTypes))
90 public void handle(Request wsRequest, Response wsResponse) throws Exception {
91 SearchProjectPermissionsWsResponse searchProjectPermissionsWsResponse = doHandle(toSearchProjectPermissionsWsRequest(wsRequest));
92 writeProtobuf(searchProjectPermissionsWsResponse, wsRequest, wsResponse);
95 private SearchProjectPermissionsWsResponse doHandle(SearchProjectPermissionsWsRequest request) {
96 checkRequestAndPermissions(request);
97 DbSession dbSession = dbClient.openSession(false);
99 validateQualifier(request.getQualifier(), resourceTypes);
100 SearchProjectPermissionsData data = dataLoader.load(request);
101 return buildResponse(data);
103 dbClient.closeSession(dbSession);
107 private static SearchProjectPermissionsWsRequest toSearchProjectPermissionsWsRequest(Request request) {
108 return new SearchProjectPermissionsWsRequest()
109 .setProjectId(request.param(PARAM_PROJECT_ID))
110 .setProjectKey(request.param(PARAM_PROJECT_KEY))
111 .setQualifier(request.param(PARAM_QUALIFIER))
112 .setPage(request.mandatoryParamAsInt(Param.PAGE))
113 .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
114 .setQuery(request.param(Param.TEXT_QUERY));
117 private void checkRequestAndPermissions(SearchProjectPermissionsWsRequest request) {
118 Optional<WsProjectRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
119 boolean hasProject = project.isPresent();
120 boolean hasProjectUuid = hasProject && project.get().uuid() != null;
121 boolean hasProjectKey = hasProject && project.get().key() != null;
123 if (hasProjectUuid) {
124 checkProjectAdminUserByComponentUuid(userSession, project.get().uuid());
125 } else if (hasProjectKey) {
126 checkProjectAdminUserByComponentKey(userSession, project.get().key());
128 checkGlobalAdminUser(userSession);
132 private SearchProjectPermissionsWsResponse buildResponse(SearchProjectPermissionsData data) {
133 SearchProjectPermissionsWsResponse.Builder response = SearchProjectPermissionsWsResponse.newBuilder();
134 Permission.Builder permissionResponse = Permission.newBuilder();
136 Project.Builder rootComponentBuilder = Project.newBuilder();
137 for (ComponentDto rootComponent : data.rootComponents()) {
140 .setId(rootComponent.uuid())
141 .setKey(rootComponent.key())
142 .setQualifier(rootComponent.qualifier())
143 .setName(rootComponent.name());
144 for (String permission : data.permissions(rootComponent.getId())) {
145 rootComponentBuilder.addPermissions(
149 .setUsersCount(data.userCount(rootComponent.getId(), permission))
150 .setGroupsCount(data.groupCount(rootComponent.getId(), permission)));
152 response.addProjects(rootComponentBuilder);
155 for (String permissionKey : ProjectPermissions.ALL) {
156 response.addPermissions(
159 .setKey(permissionKey)
160 .setName(i18nName(permissionKey))
161 .setDescription(i18nDescriptionMessage(permissionKey)));
164 Paging paging = data.paging();
166 Common.Paging.newBuilder()
167 .setPageIndex(paging.pageIndex())
168 .setPageSize(paging.pageSize())
169 .setTotal(paging.total()));
171 return response.build();
174 private String i18nDescriptionMessage(String permissionKey) {
175 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
178 private String i18nName(String permissionKey) {
179 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey, permissionKey);