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.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.PermissionsWsParametersBuilder.createProjectParameter;
45 import static org.sonar.server.permission.ws.WsProjectRef.newOptionalWsProjectRef;
46 import static org.sonar.server.ws.WsUtils.writeProtobuf;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
50 public class SearchProjectPermissionsAction implements PermissionsWsAction {
51 private static final String PROPERTY_PREFIX = "projects_role.";
52 private static final String DESCRIPTION_SUFFIX = ".desc";
54 private final DbClient dbClient;
55 private final UserSession userSession;
56 private final I18n i18n;
57 private final SearchProjectPermissionsDataLoader dataLoader;
59 public SearchProjectPermissionsAction(DbClient dbClient, UserSession userSession, I18n i18n, SearchProjectPermissionsDataLoader dataLoader) {
60 this.dbClient = dbClient;
61 this.userSession = userSession;
63 this.dataLoader = dataLoader;
67 public void define(WebService.NewController context) {
68 WebService.NewAction action = context.createAction("search_project_permissions")
69 .setDescription("List project permissions. A project can be a technical project, a view or a developer.<br />" +
70 "Requires 'Administer System' permission or 'Administer' rights on the specified project.")
71 .setResponseExample(getClass().getResource("search_project_permissions-example.json"))
74 .addSearchQuery("sonarq", "project names", "project keys")
77 createProjectParameter(action);
81 public void handle(Request wsRequest, Response wsResponse) throws Exception {
82 SearchProjectPermissionsWsResponse searchProjectPermissionsWsResponse = doHandle(toSearchProjectPermissionsWsRequest(wsRequest));
83 writeProtobuf(searchProjectPermissionsWsResponse, wsRequest, wsResponse);
86 private SearchProjectPermissionsWsResponse doHandle(SearchProjectPermissionsWsRequest request) {
87 checkRequestAndPermissions(request);
88 DbSession dbSession = dbClient.openSession(false);
90 SearchProjectPermissionsData data = dataLoader.load(request);
91 return buildResponse(data);
93 dbClient.closeSession(dbSession);
97 private static SearchProjectPermissionsWsRequest toSearchProjectPermissionsWsRequest(Request request) {
98 return new SearchProjectPermissionsWsRequest()
99 .setProjectId(request.param(PARAM_PROJECT_ID))
100 .setProjectKey(request.param(PARAM_PROJECT_KEY))
101 .setPage(request.mandatoryParamAsInt(Param.PAGE))
102 .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
103 .setQuery(request.param(Param.TEXT_QUERY));
106 private void checkRequestAndPermissions(SearchProjectPermissionsWsRequest request) {
107 Optional<WsProjectRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
108 boolean hasProject = project.isPresent();
109 boolean hasProjectUuid = hasProject && project.get().uuid() != null;
110 boolean hasProjectKey = hasProject && project.get().key() != null;
112 if (hasProjectUuid) {
113 checkProjectAdminUserByComponentUuid(userSession, project.get().uuid());
114 } else if (hasProjectKey) {
115 checkProjectAdminUserByComponentKey(userSession, project.get().key());
117 checkGlobalAdminUser(userSession);
121 private SearchProjectPermissionsWsResponse buildResponse(SearchProjectPermissionsData data) {
122 SearchProjectPermissionsWsResponse.Builder response = SearchProjectPermissionsWsResponse.newBuilder();
123 Permission.Builder permissionResponse = Permission.newBuilder();
125 Project.Builder rootComponentBuilder = Project.newBuilder();
126 for (ComponentDto rootComponent : data.rootComponents()) {
129 .setId(rootComponent.uuid())
130 .setKey(rootComponent.key())
131 .setQualifier(rootComponent.qualifier())
132 .setName(rootComponent.name());
133 for (String permission : data.permissions(rootComponent.getId())) {
134 rootComponentBuilder.addPermissions(
138 .setUsersCount(data.userCount(rootComponent.getId(), permission))
139 .setGroupsCount(data.groupCount(rootComponent.getId(), permission)));
141 response.addProjects(rootComponentBuilder);
144 for (String permissionKey : ProjectPermissions.ALL) {
145 response.addPermissions(
148 .setKey(permissionKey)
149 .setName(i18nName(permissionKey))
150 .setDescription(i18nDescriptionMessage(permissionKey)));
153 Paging paging = data.paging();
155 Common.Paging.newBuilder()
156 .setPageIndex(paging.pageIndex())
157 .setPageSize(paging.pageSize())
158 .setTotal(paging.total()));
160 return response.build();
163 private String i18nDescriptionMessage(String permissionKey) {
164 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
167 private String i18nName(String permissionKey) {
168 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey, permissionKey);