]> source.dussan.org Git - sonarqube.git/blob
f1159aeade578d9bf17d4fe36e24047efec4cb59
[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.template;
21
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.WsPermissions;
38 import org.sonarqube.ws.WsPermissions.Permission;
39 import org.sonarqube.ws.WsPermissions.PermissionTemplate;
40 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse;
41 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse.TemplateIdQualifier;
42 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
43
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;
50
51 public class SearchTemplatesAction implements PermissionsWsAction {
52   private static final String PROPERTY_PREFIX = "projects_role.";
53   private static final String DESCRIPTION_SUFFIX = ".desc";
54
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;
60
61   public SearchTemplatesAction(DbClient dbClient, UserSession userSession, I18n i18n, PermissionWsSupport support, SearchTemplatesDataLoader dataLoader) {
62     this.dbClient = dbClient;
63     this.userSession = userSession;
64     this.i18n = i18n;
65     this.support = support;
66     this.dataLoader = dataLoader;
67   }
68
69   private static void buildDefaultTemplatesResponse(SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
70     TemplateIdQualifier.Builder templateUuidQualifierBuilder = TemplateIdQualifier.newBuilder();
71
72     DefaultTemplatesResolverImpl.ResolvedDefaultTemplates resolvedDefaultTemplates = data.defaultTemplates();
73     response.addDefaultTemplates(templateUuidQualifierBuilder
74       .setQualifier(Qualifiers.PROJECT)
75       .setTemplateId(resolvedDefaultTemplates.getProject()));
76
77     resolvedDefaultTemplates.getView()
78       .ifPresent(viewDefaultTemplate -> response.addDefaultTemplates(
79         templateUuidQualifierBuilder
80           .clear()
81           .setQualifier(Qualifiers.VIEW)
82           .setTemplateId(viewDefaultTemplate)));
83   }
84
85   private static void buildTemplatesResponse(WsPermissions.SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
86     Permission.Builder permissionResponse = Permission.newBuilder();
87     PermissionTemplate.Builder templateBuilder = PermissionTemplate.newBuilder();
88
89     for (PermissionTemplateDto templateDto : data.templates()) {
90       templateBuilder
91         .clear()
92         .setId(templateDto.getUuid())
93         .setName(templateDto.getName())
94         .setCreatedAt(formatDateTime(templateDto.getCreatedAt()))
95         .setUpdatedAt(formatDateTime(templateDto.getUpdatedAt()));
96       setNullable(templateDto.getKeyPattern(), templateBuilder::setProjectKeyPattern);
97       setNullable(templateDto.getDescription(), templateBuilder::setDescription);
98       for (String permission : ProjectPermissions.ALL) {
99         templateBuilder.addPermissions(
100           permissionResponse
101             .clear()
102             .setKey(permission)
103             .setUsersCount(data.userCount(templateDto.getId(), permission))
104             .setGroupsCount(data.groupCount(templateDto.getId(), permission))
105             .setWithProjectCreator(data.withProjectCreator(templateDto.getId(), permission)));
106       }
107       response.addPermissionTemplates(templateBuilder);
108     }
109   }
110
111   @Override
112   public void define(WebService.NewController context) {
113     WebService.NewAction action = context.createAction("search_templates")
114       .setDescription("List permission templates.<br />" +
115         "Requires the following permission: 'Administer System'.")
116       .setResponseExample(getClass().getResource("search_templates-example.json"))
117       .setSince("5.2")
118       .addSearchQuery("defau", "permission template names")
119       .setHandler(this);
120
121     createOrganizationParameter(action).setSince("6.2");
122   }
123
124   @Override
125   public void handle(Request wsRequest, Response wsResponse) throws Exception {
126     try (DbSession dbSession = dbClient.openSession(false)) {
127       OrganizationDto org = support.findOrganization(dbSession, wsRequest.param(PARAM_ORGANIZATION));
128       SearchTemplatesWsRequest request = new SearchTemplatesWsRequest()
129         .setOrganizationUuid(org.getUuid())
130         .setQuery(wsRequest.param(Param.TEXT_QUERY));
131       checkGlobalAdmin(userSession, request.getOrganizationUuid());
132
133       SearchTemplatesWsResponse searchTemplatesWsResponse = buildResponse(dataLoader.load(dbSession, request));
134       writeProtobuf(searchTemplatesWsResponse, wsRequest, wsResponse);
135     }
136   }
137
138   private WsPermissions.SearchTemplatesWsResponse buildResponse(SearchTemplatesData data) {
139     SearchTemplatesWsResponse.Builder response = SearchTemplatesWsResponse.newBuilder();
140
141     buildTemplatesResponse(response, data);
142     buildDefaultTemplatesResponse(response, data);
143     buildPermissionsResponse(response);
144
145     return response.build();
146   }
147
148   private void buildPermissionsResponse(SearchTemplatesWsResponse.Builder response) {
149     Permission.Builder permissionResponse = Permission.newBuilder();
150     for (String permissionKey : ProjectPermissions.ALL) {
151       response.addPermissions(
152         permissionResponse
153           .clear()
154           .setKey(permissionKey)
155           .setName(i18nName(permissionKey))
156           .setDescription(i18nDescriptionMessage(permissionKey)));
157     }
158   }
159
160   private String i18nDescriptionMessage(String permissionKey) {
161     return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
162   }
163
164   private String i18nName(String permissionKey) {
165     return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey, permissionKey);
166   }
167 }