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 org.sonar.api.i18n.I18n;
23 import org.sonar.api.resources.Qualifiers;
24 import org.sonar.api.resources.ResourceTypes;
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.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.permission.template.PermissionTemplateDto;
31 import org.sonar.server.permission.ws.PermissionWsSupport;
32 import org.sonar.server.permission.ws.PermissionsWsAction;
33 import org.sonar.server.platform.PersistentSettings;
34 import org.sonar.server.user.UserSession;
35 import org.sonarqube.ws.client.permission.SetDefaultTemplateWsRequest;
37 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
38 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
39 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
40 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
41 import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
42 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
43 import static org.sonar.server.ws.WsParameterBuilder.createRootQualifierParameter;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
48 public class SetDefaultTemplateAction implements PermissionsWsAction {
49 private final DbClient dbClient;
50 private final PermissionWsSupport wsSupport;
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, PermissionWsSupport wsSupport, ResourceTypes resourceTypes, PersistentSettings settings, UserSession userSession,
58 this.dbClient = dbClient;
59 this.wsSupport = wsSupport;
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 try (DbSession dbSession = dbClient.openSession(false)) {
91 PermissionTemplateDto template = getTemplate(dbSession, request);
92 validateQualifier(qualifier, resourceTypes);
93 setDefaultTemplateUuid(dbSession, template.getUuid(), qualifier);
98 private static SetDefaultTemplateWsRequest toSetDefaultTemplateWsRequest(Request request) {
99 return new SetDefaultTemplateWsRequest()
100 .setQualifier(request.param(PARAM_QUALIFIER))
101 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
102 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
105 private PermissionTemplateDto getTemplate(DbSession dbSession, SetDefaultTemplateWsRequest request) {
106 return wsSupport.findTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
109 private void setDefaultTemplateUuid(DbSession dbSession, String templateUuid, String qualifier) {
110 settings.saveProperty(dbSession, defaultRootQualifierTemplateProperty(qualifier), templateUuid);