]> source.dussan.org Git - sonarqube.git/blob
1782ae1ef8183c0640059d680c3bd3e2c7d064d3
[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.Permissions.Permission;
40 import org.sonarqube.ws.Permissions.SearchProjectPermissionsWsResponse;
41 import org.sonarqube.ws.Permissions.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.createRootQualifierParameter;
48 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
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       .setDeprecatedSince("6.5")
87       .addPagingParams(25)
88       .setHandler(this);
89
90     action.createParam(Param.TEXT_QUERY)
91       .setDescription("Limit search to: <ul>" +
92         "<li>project names that contain the supplied string</li>" +
93         "<li>project keys that are exactly the same as the supplied string</li>" +
94         "</ul>")
95       .setExampleValue("apac");
96     createProjectParameters(action);
97     createRootQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes))
98       .setSince("5.3");
99   }
100
101   @Override
102   public void handle(Request wsRequest, Response wsResponse) throws Exception {
103     SearchProjectPermissionsWsResponse searchProjectPermissionsWsResponse = doHandle(toSearchProjectPermissionsWsRequest(wsRequest));
104     writeProtobuf(searchProjectPermissionsWsResponse, wsRequest, wsResponse);
105   }
106
107   private SearchProjectPermissionsWsResponse doHandle(SearchProjectPermissionsWsRequest request) {
108     try (DbSession dbSession = dbClient.openSession(false)) {
109       checkAuthorized(dbSession, request);
110       validateQualifier(request.getQualifier(), resourceTypes);
111       SearchProjectPermissionsData data = dataLoader.load(dbSession, request);
112       return buildResponse(data);
113     }
114   }
115
116   private static SearchProjectPermissionsWsRequest toSearchProjectPermissionsWsRequest(Request request) {
117     return new SearchProjectPermissionsWsRequest()
118       .setProjectId(request.param(PARAM_PROJECT_ID))
119       .setProjectKey(request.param(PARAM_PROJECT_KEY))
120       .setQualifier(request.param(PARAM_QUALIFIER))
121       .setPage(request.mandatoryParamAsInt(Param.PAGE))
122       .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
123       .setQuery(request.param(Param.TEXT_QUERY));
124   }
125
126   private void checkAuthorized(DbSession dbSession, SearchProjectPermissionsWsRequest request) {
127     com.google.common.base.Optional<ProjectWsRef> projectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
128     if (projectRef.isPresent()) {
129       ComponentDto project = wsSupport.getRootComponentOrModule(dbSession, projectRef.get());
130       PermissionPrivilegeChecker.checkProjectAdmin(userSession, project.getOrganizationUuid(), Optional.of(new ProjectId(project)));
131     } else {
132       userSession.checkLoggedIn().checkIsSystemAdministrator();
133     }
134   }
135
136   private SearchProjectPermissionsWsResponse buildResponse(SearchProjectPermissionsData data) {
137     SearchProjectPermissionsWsResponse.Builder response = SearchProjectPermissionsWsResponse.newBuilder();
138     Permission.Builder permissionResponse = Permission.newBuilder();
139
140     Project.Builder rootComponentBuilder = Project.newBuilder();
141     for (ComponentDto rootComponent : data.rootComponents()) {
142       rootComponentBuilder
143         .clear()
144         .setId(rootComponent.uuid())
145         .setKey(rootComponent.getDbKey())
146         .setQualifier(rootComponent.qualifier())
147         .setName(rootComponent.name());
148       for (String permission : data.permissions(rootComponent.getId())) {
149         rootComponentBuilder.addPermissions(
150           permissionResponse
151             .clear()
152             .setKey(permission)
153             .setUsersCount(data.userCount(rootComponent.getId(), permission))
154             .setGroupsCount(data.groupCount(rootComponent.getId(), permission)));
155       }
156       response.addProjects(rootComponentBuilder);
157     }
158
159     for (String permissionKey : ProjectPermissions.ALL) {
160       response.addPermissions(
161         permissionResponse
162           .clear()
163           .setKey(permissionKey)
164           .setName(i18nName(permissionKey))
165           .setDescription(i18nDescriptionMessage(permissionKey)));
166     }
167
168     Paging paging = data.paging();
169     response.setPaging(
170       Common.Paging.newBuilder()
171         .setPageIndex(paging.pageIndex())
172         .setPageSize(paging.pageSize())
173         .setTotal(paging.total()));
174
175     return response.build();
176   }
177
178   private String i18nDescriptionMessage(String permissionKey) {
179     return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
180   }
181
182   private String i18nName(String permissionKey) {
183     return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey, permissionKey);
184   }
185 }