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;
23 import org.sonar.api.i18n.I18n;
24 import org.sonar.api.resources.Qualifiers;
25 import org.sonar.api.resources.ResourceTypes;
26 import org.sonar.api.server.ws.Request;
27 import org.sonar.api.server.ws.Response;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.permission.PermissionTemplateDto;
32 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
33 import org.sonar.server.permission.ws.PermissionsWsAction;
34 import org.sonar.server.platform.PersistentSettings;
35 import org.sonar.server.user.UserSession;
36 import org.sonarqube.ws.client.permission.SetDefaultTemplateWsRequest;
38 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
39 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
40 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
41 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.QualifierParameterContext.newQualifierParameterContext;
42 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createQualifierParameter;
43 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
44 import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
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);
77 createQualifierParameter(action, newQualifierParameterContext(userSession, i18n, resourceTypes))
78 .setDefaultValue(Qualifiers.PROJECT);
82 public void handle(Request request, Response response) throws Exception {
83 doHandle(toSetDefaultTemplateWsRequest(request));
87 private void doHandle(SetDefaultTemplateWsRequest request) {
88 checkGlobalAdminUser(userSession);
90 String qualifier = request.getQualifier();
91 PermissionTemplateDto template = getTemplate(request);
92 validateQualifier(qualifier, resourceTypes);
93 setDefaultTemplateUuid(template.getUuid(), qualifier);
96 private static SetDefaultTemplateWsRequest toSetDefaultTemplateWsRequest(Request request) {
97 return new SetDefaultTemplateWsRequest()
98 .setQualifier(request.param(PARAM_QUALIFIER))
99 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
100 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
103 private PermissionTemplateDto getTemplate(SetDefaultTemplateWsRequest request) {
104 DbSession dbSession = dbClient.openSession(false);
106 return finder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
108 dbClient.closeSession(dbSession);
112 private void setDefaultTemplateUuid(String templateUuid, String qualifier) {
113 settings.saveProperty(defaultRootQualifierTemplateProperty(qualifier), templateUuid);