]> source.dussan.org Git - sonarqube.git/blob
71bd9b6727ecff587c05b5d57cd330b9206bc93d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.permission.ws;
21
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.PermissionPrivilegeChecker;
36 import org.sonar.server.permission.ProjectId;
37 import org.sonar.server.user.UserSession;
38 import org.sonarqube.ws.Common;
39 import org.sonarqube.ws.WsPermissions.Permission;
40 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse;
41 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse.Project;
42 import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
43
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;
53
54 public class SearchProjectPermissionsAction implements PermissionsWsAction {
55   private static final String PROPERTY_PREFIX = "projects_role.";
56   private static final String DESCRIPTION_SUFFIX = ".desc";
57
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;
64
65   public SearchProjectPermissionsAction(DbClient dbClient, UserSession userSession, I18n i18n, ResourceTypes resourceTypes,
66     SearchProjectPermissionsDataLoader dataLoader, PermissionWsSupport wsSupport) {
67     this.dbClient = dbClient;
68     this.userSession = userSession;
69     this.i18n = i18n;
70     this.resourceTypes = resourceTypes;
71     this.dataLoader = dataLoader;
72     this.wsSupport = wsSupport;
73   }
74
75   @Override
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:" +
80         "<ul>" +
81         "<li>'Administer System'</li>" +
82         "<li>'Administer' rights on the specified project</li>" +
83         "</ul>")
84       .setResponseExample(getClass().getResource("search_project_permissions-example.json"))
85       .setSince("5.2")
86       .addPagingParams(25)
87       .setHandler(this);
88
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>" +
93         "</ul>")
94       .setExampleValue("apac");
95     createProjectParameters(action);
96     createRootQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes))
97       .setSince("5.3");
98   }
99
100   @Override
101   public void handle(Request wsRequest, Response wsResponse) throws Exception {
102     SearchProjectPermissionsWsResponse searchProjectPermissionsWsResponse = doHandle(toSearchProjectPermissionsWsRequest(wsRequest));
103     writeProtobuf(searchProjectPermissionsWsResponse, wsRequest, wsResponse);
104   }
105
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);
112     }
113   }
114
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));
123   }
124
125   private void checkAuthorized(DbSession dbSession, SearchProjectPermissionsWsRequest request) {
126     com.google.common.base.Optional<ProjectWsRef> projectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
127     if (projectRef.isPresent()) {
128       ComponentDto project = wsSupport.getRootComponentOrModule(dbSession, projectRef.get());
129       PermissionPrivilegeChecker.checkProjectAdmin(userSession, project.getOrganizationUuid(), Optional.of(new ProjectId(project)));
130     } else {
131       userSession.checkLoggedIn().checkIsRoot();
132     }
133   }
134
135   private SearchProjectPermissionsWsResponse buildResponse(SearchProjectPermissionsData data) {
136     SearchProjectPermissionsWsResponse.Builder response = SearchProjectPermissionsWsResponse.newBuilder();
137     Permission.Builder permissionResponse = Permission.newBuilder();
138
139     Project.Builder rootComponentBuilder = Project.newBuilder();
140     for (ComponentDto rootComponent : data.rootComponents()) {
141       rootComponentBuilder
142         .clear()
143         .setId(rootComponent.uuid())
144         .setKey(rootComponent.key())
145         .setQualifier(rootComponent.qualifier())
146         .setName(rootComponent.name());
147       for (String permission : data.permissions(rootComponent.getId())) {
148         rootComponentBuilder.addPermissions(
149           permissionResponse
150             .clear()
151             .setKey(permission)
152             .setUsersCount(data.userCount(rootComponent.getId(), permission))
153             .setGroupsCount(data.groupCount(rootComponent.getId(), permission)));
154       }
155       response.addProjects(rootComponentBuilder);
156     }
157
158     for (String permissionKey : ProjectPermissions.ALL) {
159       response.addPermissions(
160         permissionResponse
161           .clear()
162           .setKey(permissionKey)
163           .setName(i18nName(permissionKey))
164           .setDescription(i18nDescriptionMessage(permissionKey)));
165     }
166
167     Paging paging = data.paging();
168     response.setPaging(
169       Common.Paging.newBuilder()
170         .setPageIndex(paging.pageIndex())
171         .setPageSize(paging.pageSize())
172         .setTotal(paging.total()));
173
174     return response.build();
175   }
176
177   private String i18nDescriptionMessage(String permissionKey) {
178     return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
179   }
180
181   private String i18nName(String permissionKey) {
182     return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey, permissionKey);
183   }
184 }