2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.permission.ws.template;
24 import org.sonar.api.i18n.I18n;
25 import org.sonar.api.resources.Qualifiers;
26 import org.sonar.api.resources.ResourceTypes;
27 import org.sonar.api.server.ws.Request;
28 import org.sonar.api.server.ws.Response;
29 import org.sonar.api.server.ws.WebService;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.permission.PermissionTemplateDto;
33 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
34 import org.sonar.server.permission.ws.PermissionsWsAction;
35 import org.sonar.server.platform.PersistentSettings;
36 import org.sonar.server.user.UserSession;
37 import org.sonarqube.ws.client.permission.SetDefaultTemplateWsRequest;
39 import static com.google.common.collect.FluentIterable.from;
40 import static com.google.common.collect.Ordering.natural;
41 import static java.lang.String.format;
42 import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
43 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
44 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
45 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
46 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
47 import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
52 public class SetDefaultTemplateAction implements PermissionsWsAction {
53 private final DbClient dbClient;
54 private final PermissionDependenciesFinder finder;
55 private final ResourceTypes resourceTypes;
56 private final PersistentSettings settings;
57 private final UserSession userSession;
58 private final I18n i18n;
60 public SetDefaultTemplateAction(DbClient dbClient, PermissionDependenciesFinder finder, ResourceTypes resourceTypes, PersistentSettings settings, UserSession userSession,
62 this.dbClient = dbClient;
64 this.resourceTypes = resourceTypes;
65 this.settings = settings;
66 this.userSession = userSession;
71 public void define(WebService.NewController context) {
72 WebService.NewAction action = context.createAction("set_default_template")
73 .setDescription("Set a permission template as default.<br />" +
74 "It requires administration permissions to access.")
79 createTemplateParameters(action);
81 action.createParam(PARAM_QUALIFIER)
82 .setDescription("Project qualifier. Possible values are:" + buildRootQualifiersDescription())
83 .setDefaultValue(Qualifiers.PROJECT)
84 .setPossibleValues(getRootQualifiers());
88 public void handle(Request request, Response response) throws Exception {
89 doHandle(toSetDefaultTemplateWsRequest(request));
93 private void doHandle(SetDefaultTemplateWsRequest request) {
94 checkGlobalAdminUser(userSession);
96 String qualifier = request.getQualifier();
97 PermissionTemplateDto template = getTemplate(request);
98 validateQualifier(qualifier, getRootQualifiers());
99 setDefaultTemplateUuid(template.getUuid(), qualifier);
102 private static SetDefaultTemplateWsRequest toSetDefaultTemplateWsRequest(Request request) {
103 return new SetDefaultTemplateWsRequest()
104 .setQualifier(request.param(PARAM_QUALIFIER))
105 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
106 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
109 private Set<String> getRootQualifiers() {
110 return from(resourceTypes.getRoots())
111 .transform(RESOURCE_TYPE_TO_QUALIFIER)
112 .toSortedSet(natural());
115 private String buildRootQualifiersDescription() {
116 StringBuilder description = new StringBuilder();
117 description.append("<ul>");
118 String qualifierPattern = "<li>%s - %s</li>";
119 for (String qualifier : getRootQualifiers()) {
120 description.append(format(qualifierPattern, qualifier, i18n(qualifier)));
122 description.append("</ul>");
124 return description.toString();
127 private String i18n(String qualifier) {
128 String qualifiersPropertyPrefix = "qualifiers.";
129 return i18n.message(userSession.locale(), qualifiersPropertyPrefix + qualifier, "");
132 private PermissionTemplateDto getTemplate(SetDefaultTemplateWsRequest request) {
133 DbSession dbSession = dbClient.openSession(false);
135 return finder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
137 dbClient.closeSession(dbSession);
141 private void setDefaultTemplateUuid(String templateUuid, String qualifier) {
142 settings.saveProperty(defaultRootQualifierTemplateProperty(qualifier), templateUuid);