3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info 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.resources.Qualifiers;
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.core.permission.ProjectPermissions;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.organization.OrganizationDto;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
34 import org.sonar.server.permission.ws.PermissionWsSupport;
35 import org.sonar.server.permission.ws.PermissionsWsAction;
36 import org.sonar.server.user.UserSession;
37 import org.sonarqube.ws.Permissions;
38 import org.sonarqube.ws.Permissions.Permission;
39 import org.sonarqube.ws.Permissions.PermissionTemplate;
40 import org.sonarqube.ws.Permissions.SearchTemplatesWsResponse;
41 import org.sonarqube.ws.Permissions.SearchTemplatesWsResponse.TemplateIdQualifier;
42 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
44 import static org.sonar.api.utils.DateUtils.formatDateTime;
45 import static org.sonar.core.util.Protobuf.setNullable;
46 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
47 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
48 import static org.sonar.server.ws.WsUtils.writeProtobuf;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
51 public class SearchTemplatesAction implements PermissionsWsAction {
52 private static final String PROPERTY_PREFIX = "projects_role.";
53 private static final String DESCRIPTION_SUFFIX = ".desc";
55 private final DbClient dbClient;
56 private final UserSession userSession;
57 private final I18n i18n;
58 private final PermissionWsSupport support;
59 private final SearchTemplatesDataLoader dataLoader;
61 public SearchTemplatesAction(DbClient dbClient, UserSession userSession, I18n i18n, PermissionWsSupport support, SearchTemplatesDataLoader dataLoader) {
62 this.dbClient = dbClient;
63 this.userSession = userSession;
65 this.support = support;
66 this.dataLoader = dataLoader;
70 public void define(WebService.NewController context) {
71 WebService.NewAction action = context.createAction("search_templates")
72 .setDescription("List permission templates.<br />" +
73 "Requires the following permission: 'Administer System'.")
74 .setResponseExample(getClass().getResource("search_templates-example.json"))
76 .addSearchQuery("defau", "permission template names")
79 createOrganizationParameter(action).setSince("6.2");
83 public void handle(Request wsRequest, Response wsResponse) throws Exception {
84 try (DbSession dbSession = dbClient.openSession(false)) {
85 OrganizationDto org = support.findOrganization(dbSession, wsRequest.param(PARAM_ORGANIZATION));
86 SearchTemplatesWsRequest request = new SearchTemplatesWsRequest()
87 .setOrganizationUuid(org.getUuid())
88 .setQuery(wsRequest.param(Param.TEXT_QUERY));
89 checkGlobalAdmin(userSession, request.getOrganizationUuid());
91 SearchTemplatesWsResponse searchTemplatesWsResponse = buildResponse(dataLoader.load(dbSession, request));
92 writeProtobuf(searchTemplatesWsResponse, wsRequest, wsResponse);
96 private static void buildDefaultTemplatesResponse(SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
97 TemplateIdQualifier.Builder templateUuidQualifierBuilder = TemplateIdQualifier.newBuilder();
99 DefaultTemplatesResolverImpl.ResolvedDefaultTemplates resolvedDefaultTemplates = data.defaultTemplates();
100 response.addDefaultTemplates(templateUuidQualifierBuilder
101 .setQualifier(Qualifiers.PROJECT)
102 .setTemplateId(resolvedDefaultTemplates.getProject()));
104 resolvedDefaultTemplates.getView()
105 .ifPresent(viewDefaultTemplate -> response.addDefaultTemplates(
106 templateUuidQualifierBuilder
108 .setQualifier(Qualifiers.VIEW)
109 .setTemplateId(viewDefaultTemplate)));
112 private static void buildTemplatesResponse(Permissions.SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
113 Permission.Builder permissionResponse = Permission.newBuilder();
114 PermissionTemplate.Builder templateBuilder = PermissionTemplate.newBuilder();
116 for (PermissionTemplateDto templateDto : data.templates()) {
119 .setId(templateDto.getUuid())
120 .setName(templateDto.getName())
121 .setCreatedAt(formatDateTime(templateDto.getCreatedAt()))
122 .setUpdatedAt(formatDateTime(templateDto.getUpdatedAt()));
123 setNullable(templateDto.getKeyPattern(), templateBuilder::setProjectKeyPattern);
124 setNullable(templateDto.getDescription(), templateBuilder::setDescription);
125 for (String permission : ProjectPermissions.ALL) {
126 templateBuilder.addPermissions(
130 .setUsersCount(data.userCount(templateDto.getId(), permission))
131 .setGroupsCount(data.groupCount(templateDto.getId(), permission))
132 .setWithProjectCreator(data.withProjectCreator(templateDto.getId(), permission)));
134 response.addPermissionTemplates(templateBuilder);
138 private Permissions.SearchTemplatesWsResponse buildResponse(SearchTemplatesData data) {
139 SearchTemplatesWsResponse.Builder response = SearchTemplatesWsResponse.newBuilder();
141 buildTemplatesResponse(response, data);
142 buildDefaultTemplatesResponse(response, data);
143 buildPermissionsResponse(response);
145 return response.build();
148 private void buildPermissionsResponse(SearchTemplatesWsResponse.Builder response) {
149 Permission.Builder permissionResponse = Permission.newBuilder();
150 for (String permissionKey : ProjectPermissions.ALL) {
151 response.addPermissions(
154 .setKey(permissionKey)
155 .setName(i18nName(permissionKey))
156 .setDescription(i18nDescriptionMessage(permissionKey)));
160 private String i18nDescriptionMessage(String permissionKey) {
161 return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
164 private String i18nName(String permissionKey) {
165 return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey, permissionKey);