3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.permission.ws.template;
22 import java.util.Locale;
23 import org.sonar.api.i18n.I18n;
24 import org.sonar.api.server.ws.Request;
25 import org.sonar.api.server.ws.Response;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.api.server.ws.WebService.Param;
28 import org.sonar.core.permission.ProjectPermissions;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.organization.OrganizationDto;
32 import org.sonar.db.permission.template.PermissionTemplateDto;
33 import org.sonar.server.permission.ws.PermissionWsSupport;
34 import org.sonar.server.permission.ws.PermissionsWsAction;
35 import org.sonar.server.user.UserSession;
36 import org.sonarqube.ws.WsPermissions;
37 import org.sonarqube.ws.WsPermissions.Permission;
38 import org.sonarqube.ws.WsPermissions.PermissionTemplate;
39 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse;
40 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse.TemplateIdQualifier;
41 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
43 import static org.sonar.api.utils.DateUtils.formatDateTime;
44 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
45 import static org.sonar.server.ws.WsUtils.writeProtobuf;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
48 public class SearchTemplatesAction implements PermissionsWsAction {
49 private static final String PROPERTY_PREFIX = "projects_role.";
50 private static final String DESCRIPTION_SUFFIX = ".desc";
52 private final DbClient dbClient;
53 private final UserSession userSession;
54 private final I18n i18n;
55 private final PermissionWsSupport support;
56 private final SearchTemplatesDataLoader dataLoader;
58 public SearchTemplatesAction(DbClient dbClient, UserSession userSession, I18n i18n, PermissionWsSupport support, SearchTemplatesDataLoader dataLoader) {
59 this.dbClient = dbClient;
60 this.userSession = userSession;
62 this.support = support;
63 this.dataLoader = dataLoader;
67 public void define(WebService.NewController context) {
68 WebService.NewAction action = context.createAction("search_templates")
69 .setDescription("List permission templates.<br />" +
70 "It requires to be authenticated.")
71 .setResponseExample(getClass().getResource("search_templates-example.json"))
73 .addSearchQuery("defau", "permission template names")
76 createOrganizationParameter(action);
80 public void handle(Request wsRequest, Response wsResponse) throws Exception {
81 userSession.checkLoggedIn();
83 try (DbSession dbSession = dbClient.openSession(false)) {
84 OrganizationDto org = support.findOrganization(dbSession, wsRequest.param(PARAM_ORGANIZATION_KEY));
85 SearchTemplatesWsRequest request = new SearchTemplatesWsRequest()
86 .setOrganizationUuid(org.getUuid())
87 .setQuery(wsRequest.param(Param.TEXT_QUERY));
89 SearchTemplatesWsResponse searchTemplatesWsResponse = buildResponse(dataLoader.load(dbSession, request));
90 writeProtobuf(searchTemplatesWsResponse, wsRequest, wsResponse);
94 private WsPermissions.SearchTemplatesWsResponse buildResponse(SearchTemplatesData data) {
95 SearchTemplatesWsResponse.Builder response = SearchTemplatesWsResponse.newBuilder();
97 buildTemplatesResponse(response, data);
98 buildDefaultTemplatesResponse(response, data);
99 buildPermissionsResponse(response);
101 return response.build();
104 private static void buildDefaultTemplatesResponse(SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
105 TemplateIdQualifier.Builder templateUuidQualifierBuilder = TemplateIdQualifier.newBuilder();
106 for (DefaultPermissionTemplateFinder.TemplateUuidQualifier templateUuidQualifier : data.defaultTemplates()) {
107 response.addDefaultTemplates(templateUuidQualifierBuilder
109 .setQualifier(templateUuidQualifier.getQualifier())
110 .setTemplateId(templateUuidQualifier.getTemplateUuid()));
114 private static void buildTemplatesResponse(WsPermissions.SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
115 Permission.Builder permissionResponse = Permission.newBuilder();
116 PermissionTemplate.Builder templateBuilder = PermissionTemplate.newBuilder();
118 for (PermissionTemplateDto templateDto : data.templates()) {
121 .setId(templateDto.getUuid())
122 .setName(templateDto.getName())
123 .setCreatedAt(formatDateTime(templateDto.getCreatedAt()))
124 .setUpdatedAt(formatDateTime(templateDto.getUpdatedAt()));
125 if (templateDto.getKeyPattern() != null) {
126 templateBuilder.setProjectKeyPattern(templateDto.getKeyPattern());
128 if (templateDto.getDescription() != null) {
129 templateBuilder.setDescription(templateDto.getDescription());
131 for (String permission : ProjectPermissions.ALL) {
132 templateBuilder.addPermissions(
136 .setUsersCount(data.userCount(templateDto.getId(), permission))
137 .setGroupsCount(data.groupCount(templateDto.getId(), permission))
138 .setWithProjectCreator(data.withProjectCreator(templateDto.getId(), permission)));
140 response.addPermissionTemplates(templateBuilder);
144 private void buildPermissionsResponse(SearchTemplatesWsResponse.Builder response) {
145 Permission.Builder permissionResponse = Permission.newBuilder();
146 for (String permissionKey : ProjectPermissions.ALL) {
147 response.addPermissions(
150 .setKey(permissionKey)
151 .setName(i18nName(permissionKey))
152 .setDescription(i18nDescriptionMessage(permissionKey)));
156 private String i18nDescriptionMessage(String permissionKey) {
157 return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
160 private String i18nName(String permissionKey) {
161 return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey, permissionKey);