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.permission.template.PermissionTemplateDto;
30 import org.sonar.server.permission.ws.PermissionsWsAction;
31 import org.sonar.server.user.UserSession;
32 import org.sonarqube.ws.WsPermissions;
33 import org.sonarqube.ws.WsPermissions.Permission;
34 import org.sonarqube.ws.WsPermissions.PermissionTemplate;
35 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse;
36 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse.TemplateIdQualifier;
37 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
39 import static org.sonar.api.utils.DateUtils.formatDateTime;
40 import static org.sonar.server.ws.WsUtils.writeProtobuf;
42 public class SearchTemplatesAction implements PermissionsWsAction {
43 private static final String PROPERTY_PREFIX = "projects_role.";
44 private static final String DESCRIPTION_SUFFIX = ".desc";
46 private final UserSession userSession;
47 private final I18n i18n;
48 private final SearchTemplatesDataLoader dataLoader;
50 public SearchTemplatesAction(UserSession userSession, I18n i18n, SearchTemplatesDataLoader dataLoader) {
51 this.userSession = userSession;
53 this.dataLoader = dataLoader;
57 public void define(WebService.NewController context) {
58 context.createAction("search_templates")
59 .setDescription("List permission templates.<br />" +
60 "It requires to be authenticated.")
61 .setResponseExample(getClass().getResource("search_templates-example.json"))
63 .addSearchQuery("defau", "permission template names")
68 public void handle(Request wsRequest, Response wsResponse) throws Exception {
69 userSession.checkLoggedIn();
71 SearchTemplatesWsResponse searchTemplatesWsResponse = doHandle(toSearchTemplatesWsRequest(wsRequest));
72 writeProtobuf(searchTemplatesWsResponse, wsRequest, wsResponse);
75 private SearchTemplatesWsResponse doHandle(SearchTemplatesWsRequest wsRequest) {
76 SearchTemplatesData data = dataLoader.load(wsRequest);
77 return buildResponse(data);
80 private static SearchTemplatesWsRequest toSearchTemplatesWsRequest(Request request) {
81 return new SearchTemplatesWsRequest().setQuery(request.param(Param.TEXT_QUERY));
84 private WsPermissions.SearchTemplatesWsResponse buildResponse(SearchTemplatesData data) {
85 SearchTemplatesWsResponse.Builder response = SearchTemplatesWsResponse.newBuilder();
87 buildTemplatesResponse(response, data);
88 buildDefaultTemplatesResponse(response, data);
89 buildPermissionsResponse(response);
91 return response.build();
94 private static void buildDefaultTemplatesResponse(SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
95 TemplateIdQualifier.Builder templateUuidQualifierBuilder = TemplateIdQualifier.newBuilder();
96 for (DefaultPermissionTemplateFinder.TemplateUuidQualifier templateUuidQualifier : data.defaultTemplates()) {
97 response.addDefaultTemplates(templateUuidQualifierBuilder
99 .setQualifier(templateUuidQualifier.getQualifier())
100 .setTemplateId(templateUuidQualifier.getTemplateUuid()));
104 private static void buildTemplatesResponse(WsPermissions.SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
105 Permission.Builder permissionResponse = Permission.newBuilder();
106 PermissionTemplate.Builder templateBuilder = PermissionTemplate.newBuilder();
108 for (PermissionTemplateDto templateDto : data.templates()) {
111 .setId(templateDto.getUuid())
112 .setName(templateDto.getName())
113 .setCreatedAt(formatDateTime(templateDto.getCreatedAt()))
114 .setUpdatedAt(formatDateTime(templateDto.getUpdatedAt()));
115 if (templateDto.getKeyPattern() != null) {
116 templateBuilder.setProjectKeyPattern(templateDto.getKeyPattern());
118 if (templateDto.getDescription() != null) {
119 templateBuilder.setDescription(templateDto.getDescription());
121 for (String permission : ProjectPermissions.ALL) {
122 templateBuilder.addPermissions(
126 .setUsersCount(data.userCount(templateDto.getId(), permission))
127 .setGroupsCount(data.groupCount(templateDto.getId(), permission))
128 .setWithProjectCreator(data.withProjectCreator(templateDto.getId(), permission)));
130 response.addPermissionTemplates(templateBuilder);
134 private void buildPermissionsResponse(SearchTemplatesWsResponse.Builder response) {
135 Permission.Builder permissionResponse = Permission.newBuilder();
136 for (String permissionKey : ProjectPermissions.ALL) {
137 response.addPermissions(
140 .setKey(permissionKey)
141 .setName(i18nName(permissionKey))
142 .setDescription(i18nDescriptionMessage(permissionKey)));
146 private String i18nDescriptionMessage(String permissionKey) {
147 return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
150 private String i18nName(String permissionKey) {
151 return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey, permissionKey);