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 static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
23 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
24 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
25 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
26 import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
27 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
28 import static org.sonar.server.ws.WsParameterBuilder.createRootQualifierParameter;
29 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
30 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
31 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
33 import org.sonar.api.i18n.I18n;
34 import org.sonar.api.resources.Qualifiers;
35 import org.sonar.api.resources.ResourceTypes;
36 import org.sonar.api.server.ws.Request;
37 import org.sonar.api.server.ws.Response;
38 import org.sonar.api.server.ws.WebService;
39 import org.sonar.db.DbClient;
40 import org.sonar.db.DbSession;
41 import org.sonar.db.permission.template.PermissionTemplateDto;
42 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
43 import org.sonar.server.permission.ws.PermissionsWsAction;
44 import org.sonar.server.platform.PersistentSettings;
45 import org.sonar.server.user.UserSession;
46 import org.sonarqube.ws.client.permission.SetDefaultTemplateWsRequest;
48 public class SetDefaultTemplateAction implements PermissionsWsAction {
49 private final DbClient dbClient;
50 private final PermissionDependenciesFinder finder;
51 private final ResourceTypes resourceTypes;
52 private final PersistentSettings settings;
53 private final UserSession userSession;
54 private final I18n i18n;
56 public SetDefaultTemplateAction(DbClient dbClient, PermissionDependenciesFinder finder, ResourceTypes resourceTypes, PersistentSettings settings, UserSession userSession,
58 this.dbClient = dbClient;
60 this.resourceTypes = resourceTypes;
61 this.settings = settings;
62 this.userSession = userSession;
67 public void define(WebService.NewController context) {
68 WebService.NewAction action = context.createAction("set_default_template")
69 .setDescription("Set a permission template as default.<br />" +
70 "It requires administration permissions to access.")
75 createTemplateParameters(action);
76 createRootQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes))
77 .setDefaultValue(Qualifiers.PROJECT);
81 public void handle(Request request, Response response) throws Exception {
82 doHandle(toSetDefaultTemplateWsRequest(request));
86 private void doHandle(SetDefaultTemplateWsRequest request) {
87 checkGlobalAdminUser(userSession);
89 String qualifier = request.getQualifier();
90 PermissionTemplateDto template = getTemplate(request);
91 validateQualifier(qualifier, resourceTypes);
92 setDefaultTemplateUuid(template.getUuid(), qualifier);
95 private static SetDefaultTemplateWsRequest toSetDefaultTemplateWsRequest(Request request) {
96 return new SetDefaultTemplateWsRequest()
97 .setQualifier(request.param(PARAM_QUALIFIER))
98 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
99 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
102 private PermissionTemplateDto getTemplate(SetDefaultTemplateWsRequest request) {
103 DbSession dbSession = dbClient.openSession(false);
105 return finder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
107 dbClient.closeSession(dbSession);
111 private void setDefaultTemplateUuid(String templateUuid, String qualifier) {
112 settings.saveProperty(defaultRootQualifierTemplateProperty(qualifier), templateUuid);