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 org.sonar.api.i18n.I18n;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.server.ws.WebService.Param;
27 import org.sonar.core.permission.ProjectPermissions;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.permission.PermissionTemplateDto;
31 import org.sonar.server.permission.ws.PermissionsWsAction;
32 import org.sonar.server.user.UserSession;
33 import org.sonarqube.ws.WsPermissions;
34 import org.sonarqube.ws.WsPermissions.Permission;
35 import org.sonarqube.ws.WsPermissions.PermissionTemplate;
36 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse;
37 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse.TemplateIdQualifier;
38 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
40 import static org.sonar.api.utils.DateUtils.formatDateTime;
41 import static org.sonar.server.ws.WsUtils.writeProtobuf;
43 public class SearchTemplatesAction implements PermissionsWsAction {
44 private static final String PROPERTY_PREFIX = "projects_role.";
45 private static final String DESCRIPTION_SUFFIX = ".desc";
47 private final DbClient dbClient;
48 private final UserSession userSession;
49 private final I18n i18n;
50 private final SearchTemplatesDataLoader dataLoader;
52 public SearchTemplatesAction(DbClient dbClient, UserSession userSession, I18n i18n, SearchTemplatesDataLoader dataLoader) {
53 this.dbClient = dbClient;
54 this.userSession = userSession;
56 this.dataLoader = dataLoader;
60 public void define(WebService.NewController context) {
61 context.createAction("search_templates")
62 .setDescription("List permission templates.<br />" +
63 "It requires to be authenticated.")
64 .setResponseExample(getClass().getResource("search_templates-example.json"))
66 .addSearchQuery("defau", "permission template names")
71 public void handle(Request wsRequest, Response wsResponse) throws Exception {
72 userSession.checkLoggedIn();
74 SearchTemplatesWsResponse searchTemplatesWsResponse = doHandle(toSearchTemplatesWsRequest(wsRequest));
75 writeProtobuf(searchTemplatesWsResponse, wsRequest, wsResponse);
78 private SearchTemplatesWsResponse doHandle(SearchTemplatesWsRequest wsRequest) {
79 DbSession dbSession = dbClient.openSession(false);
81 SearchTemplatesData data = dataLoader.load(wsRequest);
82 return buildResponse(data);
84 dbClient.closeSession(dbSession);
88 private static SearchTemplatesWsRequest toSearchTemplatesWsRequest(Request request) {
89 return new SearchTemplatesWsRequest().setQuery(request.param(Param.TEXT_QUERY));
92 private WsPermissions.SearchTemplatesWsResponse buildResponse(SearchTemplatesData data) {
93 SearchTemplatesWsResponse.Builder response = SearchTemplatesWsResponse.newBuilder();
95 buildTemplatesResponse(response, data);
96 buildDefaultTemplatesResponse(response, data);
97 buildPermissionsResponse(response);
99 return response.build();
102 private static void buildDefaultTemplatesResponse(SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
103 TemplateIdQualifier.Builder templateUuidQualifierBuilder = TemplateIdQualifier.newBuilder();
104 for (DefaultPermissionTemplateFinder.TemplateUuidQualifier templateUuidQualifier : data.defaultTemplates()) {
105 response.addDefaultTemplates(templateUuidQualifierBuilder
107 .setQualifier(templateUuidQualifier.getQualifier())
108 .setTemplateId(templateUuidQualifier.getTemplateUuid()));
112 private static void buildTemplatesResponse(WsPermissions.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 if (templateDto.getKeyPattern() != null) {
124 templateBuilder.setProjectKeyPattern(templateDto.getKeyPattern());
126 if (templateDto.getDescription() != null) {
127 templateBuilder.setDescription(templateDto.getDescription());
129 for (String permission : ProjectPermissions.ALL) {
130 templateBuilder.addPermissions(
134 .setUsersCount(data.userCount(templateDto.getId(), permission))
135 .setGroupsCount(data.groupCount(templateDto.getId(), permission))
136 .setWithProjectCreator(data.withProjectCreator(templateDto.getId(), permission)));
138 response.addPermissionTemplates(templateBuilder);
142 private void buildPermissionsResponse(SearchTemplatesWsResponse.Builder response) {
143 Permission.Builder permissionResponse = Permission.newBuilder();
144 for (String permissionKey : ProjectPermissions.ALL) {
145 response.addPermissions(
148 .setKey(permissionKey)
149 .setName(i18nName(permissionKey))
150 .setDescription(i18nDescriptionMessage(permissionKey)));
154 private String i18nDescriptionMessage(String permissionKey) {
155 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
158 private String i18nName(String permissionKey) {
159 return i18n.message(userSession.locale(), PROPERTY_PREFIX + permissionKey, permissionKey);