3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info 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.organization.DefaultTemplates;
31 import org.sonar.db.organization.OrganizationDao;
32 import org.sonar.db.permission.template.PermissionTemplateDto;
33 import org.sonar.server.permission.ws.PermissionWsSupport;
34 import org.sonar.server.permission.ws.PermissionsWsAction;
35 import org.sonar.server.user.UserSession;
36 import org.sonarqube.ws.client.permission.SetDefaultTemplateWsRequest;
38 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
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.createDefaultTemplateQualifierParameter;
44 import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
50 public class SetDefaultTemplateAction implements PermissionsWsAction {
51 private final DbClient dbClient;
52 private final PermissionWsSupport wsSupport;
53 private final ResourceTypes resourceTypes;
54 private final UserSession userSession;
55 private final I18n i18n;
57 public SetDefaultTemplateAction(DbClient dbClient, PermissionWsSupport wsSupport, ResourceTypes resourceTypes,
58 UserSession userSession, I18n i18n) {
59 this.dbClient = dbClient;
60 this.wsSupport = wsSupport;
61 this.resourceTypes = resourceTypes;
62 this.userSession = userSession;
66 private static SetDefaultTemplateWsRequest toSetDefaultTemplateWsRequest(Request request) {
67 return new SetDefaultTemplateWsRequest()
68 .setQualifier(request.param(PARAM_QUALIFIER))
69 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
70 .setOrganization(request.param(PARAM_ORGANIZATION))
71 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
75 public void define(WebService.NewController context) {
76 WebService.NewAction action = context.createAction("set_default_template")
77 .setDescription("Set a permission template as default.<br />" +
78 "Requires the following permission: 'Administer System'.")
83 createTemplateParameters(action);
84 createDefaultTemplateQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes))
85 .setDefaultValue(Qualifiers.PROJECT);
89 public void handle(Request request, Response response) throws Exception {
90 doHandle(toSetDefaultTemplateWsRequest(request));
94 private void doHandle(SetDefaultTemplateWsRequest request) {
95 try (DbSession dbSession = dbClient.openSession(false)) {
96 String qualifier = request.getQualifier();
97 PermissionTemplateDto template = findTemplate(dbSession, request);
98 checkGlobalAdmin(userSession, template.getOrganizationUuid());
99 validateQualifier(qualifier, resourceTypes);
100 setDefaultTemplateUuid(dbSession, template, qualifier);
105 private PermissionTemplateDto findTemplate(DbSession dbSession, SetDefaultTemplateWsRequest request) {
106 return wsSupport.findTemplate(dbSession, newTemplateRef(request.getTemplateId(),
107 request.getOrganization(), request.getTemplateName()));
110 private void setDefaultTemplateUuid(DbSession dbSession, PermissionTemplateDto permissionTemplateDto, String qualifier) {
111 String organizationUuid = permissionTemplateDto.getOrganizationUuid();
112 OrganizationDao organizationDao = dbClient.organizationDao();
114 DefaultTemplates defaultTemplates = checkFoundWithOptional(
115 organizationDao.getDefaultTemplates(dbSession, organizationUuid),
116 "No Default templates for organization with uuid '%s'", organizationUuid);
117 if (Qualifiers.PROJECT.equals(qualifier)) {
118 defaultTemplates.setProjectUuid(permissionTemplateDto.getUuid());
119 } else if (Qualifiers.VIEW.equals(qualifier)) {
120 defaultTemplates.setViewUuid(permissionTemplateDto.getUuid());
122 organizationDao.setDefaultTemplates(dbSession, organizationUuid, defaultTemplates);