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.permission.ws.WsTemplateRef;
36 import org.sonar.server.platform.PersistentSettings;
37 import org.sonar.server.user.UserSession;
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.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
43 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
44 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_QUALIFIER;
45 import static org.sonar.server.permission.ws.PermissionsWsParameters.createTemplateParameters;
46 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
47 import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
49 public class SetDefaultTemplateAction implements PermissionsWsAction {
50 private final DbClient dbClient;
51 private final PermissionDependenciesFinder finder;
52 private final ResourceTypes resourceTypes;
53 private final PersistentSettings settings;
54 private final UserSession userSession;
55 private final I18n i18n;
57 public SetDefaultTemplateAction(DbClient dbClient, PermissionDependenciesFinder finder, ResourceTypes resourceTypes, PersistentSettings settings, UserSession userSession,
59 this.dbClient = dbClient;
61 this.resourceTypes = resourceTypes;
62 this.settings = settings;
63 this.userSession = userSession;
68 public void define(WebService.NewController context) {
69 WebService.NewAction action = context.createAction("set_default_template")
70 .setDescription("Set a permission template as default.<br />" +
71 "It requires administration permissions to access.")
76 createTemplateParameters(action);
78 action.createParam(PARAM_QUALIFIER)
79 .setDescription("Project qualifier. Possible values are:" + buildRootQualifiersDescription())
80 .setDefaultValue(Qualifiers.PROJECT)
81 .setPossibleValues(getRootQualifiers());
85 public void handle(Request wsRequest, Response wsResponse) throws Exception {
86 checkGlobalAdminUser(userSession);
88 String qualifier = wsRequest.mandatoryParam(PARAM_QUALIFIER);
90 PermissionTemplateDto template = getTemplate(wsRequest);
91 validateQualifier(qualifier, getRootQualifiers());
92 setDefaultTemplateUuid(template.getUuid(), qualifier);
93 wsResponse.noContent();
96 private Set<String> getRootQualifiers() {
97 return from(resourceTypes.getRoots())
98 .transform(RESOURCE_TYPE_TO_QUALIFIER)
99 .toSortedSet(natural());
102 private String buildRootQualifiersDescription() {
103 StringBuilder description = new StringBuilder();
104 description.append("<ul>");
105 String qualifierPattern = "<li>%s - %s</li>";
106 for (String qualifier : getRootQualifiers()) {
107 description.append(format(qualifierPattern, qualifier, i18n(qualifier)));
109 description.append("</ul>");
111 return description.toString();
114 private String i18n(String qualifier) {
115 String qualifiersPropertyPrefix = "qualifiers.";
116 return i18n.message(userSession.locale(), qualifiersPropertyPrefix + qualifier, "");
119 private PermissionTemplateDto getTemplate(Request wsRequest) {
120 DbSession dbSession = dbClient.openSession(false);
122 return finder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest));
124 dbClient.closeSession(dbSession);
128 private void setDefaultTemplateUuid(String templateUuid, String qualifier) {
129 settings.saveProperty(defaultRootQualifierTemplateProperty(qualifier), templateUuid);