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.utils.Paging;
29 import org.sonar.core.permission.ProjectPermissions;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.server.user.UserSession;
34 import org.sonarqube.ws.Common;
35 import org.sonarqube.ws.WsPermissions.Permission;
36 import org.sonarqube.ws.WsPermissions.WsSearchProjectPermissionsResponse;
37 import org.sonarqube.ws.WsPermissions.WsSearchProjectPermissionsResponse.Project;
39 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
40 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentKey;
41 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentUuid;
42 import static org.sonar.server.permission.ws.WsPermissionParameters.createProjectParameter;
43 import static org.sonar.server.ws.WsUtils.writeProtobuf;
45 public class SearchProjectPermissionsAction implements PermissionsWsAction {
46 private static final String PROPERTY_PREFIX = "projects_role.";
47 private static final String DESCRIPTION_SUFFIX = ".desc";
49 private final DbClient dbClient;
50 private final UserSession userSession;
51 private final I18n i18n;
52 private final SearchProjectPermissionsDataLoader dataLoader;
54 public SearchProjectPermissionsAction(DbClient dbClient, UserSession userSession, I18n i18n, SearchProjectPermissionsDataLoader dataLoader) {
55 this.dbClient = dbClient;
56 this.userSession = userSession;
58 this.dataLoader = dataLoader;
62 public void define(WebService.NewController context) {
63 WebService.NewAction action = context.createAction("search_project_permissions")
64 .setDescription("List project permissions. A project can be a technical project, a view or a developer.<br />" +
65 "Requires 'Administer System' permission or 'Administer' rights on the specified project.")
66 .setResponseExample(getClass().getResource("search_project_permissions-example.json"))
69 .addSearchQuery("sonarq", "project names", "project keys")
72 createProjectParameter(action);
76 public void handle(Request wsRequest, Response wsResponse) throws Exception {
77 checkRequestAndPermissions(wsRequest);
79 DbSession dbSession = dbClient.openSession(false);
81 SearchProjectPermissionsData data = dataLoader.load(wsRequest);
82 WsSearchProjectPermissionsResponse response = buildResponse(data);
83 writeProtobuf(response, wsRequest, wsResponse);
85 dbClient.closeSession(dbSession);
89 private void checkRequestAndPermissions(Request wsRequest) {
90 Optional<WsProjectRef> project = WsProjectRef.optionalFromRequest(wsRequest);
91 boolean hasProject = project.isPresent();
92 boolean hasProjectUuid = hasProject && project.get().uuid() != null;
93 boolean hasProjectKey = hasProject && project.get().key() != null;
96 checkProjectAdminUserByComponentUuid(userSession, project.get().uuid());
97 } else if (hasProjectKey) {
98 checkProjectAdminUserByComponentKey(userSession, project.get().key());
100 checkGlobalAdminUser(userSession);
104 private WsSearchProjectPermissionsResponse buildResponse(SearchProjectPermissionsData data) {
105 WsSearchProjectPermissionsResponse.Builder response = WsSearchProjectPermissionsResponse.newBuilder();
106 Permission.Builder permissionResponse = Permission.newBuilder();
108 Project.Builder rootComponentBuilder = Project.newBuilder();
109 for (ComponentDto rootComponent : data.rootComponents()) {
112 .setId(rootComponent.uuid())
113 .setKey(rootComponent.key())
114 .setName(rootComponent.name());
115 for (String permission : data.permissions(rootComponent.getId())) {
116 rootComponentBuilder.addPermissions(
120 .setUsersCount(data.userCount(rootComponent.getId(), permission))
121 .setGroupsCount(data.groupCount(rootComponent.getId(), permission)));
123 response.addProjects(rootComponentBuilder);
126 for (String permissionKey : ProjectPermissions.ALL) {
127 response.addPermissions(
130 .setKey(permissionKey)
131 .setName(i18nName(permissionKey))
132 .setDescription(i18nDescriptionMessage(permissionKey))
136 Paging paging = data.paging();
138 Common.Paging.newBuilder()
139 .setPageIndex(paging.pageIndex())
140 .setPageSize(paging.pageSize())
141 .setTotal(paging.total())
144 return response.build();
147 private String i18nDescriptionMessage(String permissionKey) {
148 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
151 private String i18nName(String permissionKey) {
152 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey, permissionKey);