3 * Copyright (C) 2009-2023 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 java.util.Collections;
23 import java.util.Optional;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import org.sonar.api.resources.Qualifiers;
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.entity.EntityDto;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.management.ManagedInstanceChecker;
36 import org.sonar.server.permission.PermissionTemplateService;
37 import org.sonar.server.permission.ws.PermissionWsSupport;
38 import org.sonar.server.permission.ws.PermissionsWsAction;
39 import org.sonar.server.permission.ws.ProjectWsRef;
40 import org.sonar.server.user.UserSession;
42 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
43 import static org.sonar.server.permission.ws.WsParameters.createProjectParameters;
44 import static org.sonar.server.permission.ws.WsParameters.createTemplateParameters;
45 import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
51 public class ApplyTemplateAction implements PermissionsWsAction {
52 private final DbClient dbClient;
53 private final UserSession userSession;
54 private final PermissionTemplateService permissionTemplateService;
55 private final PermissionWsSupport wsSupport;
57 private final ManagedInstanceChecker managedInstanceChecker;
59 public ApplyTemplateAction(DbClient dbClient, UserSession userSession, PermissionTemplateService permissionTemplateService,
60 PermissionWsSupport wsSupport, ManagedInstanceChecker managedInstanceChecker) {
61 this.dbClient = dbClient;
62 this.userSession = userSession;
63 this.permissionTemplateService = permissionTemplateService;
64 this.wsSupport = wsSupport;
65 this.managedInstanceChecker = managedInstanceChecker;
68 private static ApplyTemplateRequest toApplyTemplateWsRequest(Request request) {
69 return new ApplyTemplateRequest()
70 .setProjectId(request.param(PARAM_PROJECT_ID))
71 .setProjectKey(request.param(PARAM_PROJECT_KEY))
72 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
73 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
77 public void define(WebService.NewController context) {
78 WebService.NewAction action = context.createAction("apply_template")
79 .setDescription("Apply a permission template to one project.<br>" +
80 "The project id or project key must be provided.<br>" +
81 "The template id or name must be provided.<br>" +
82 "Requires the following permission: 'Administer System'.")
87 createTemplateParameters(action);
88 createProjectParameters(action);
92 public void handle(Request request, Response response) throws Exception {
93 doHandle(toApplyTemplateWsRequest(request));
97 private void doHandle(ApplyTemplateRequest request) {
98 try (DbSession dbSession = dbClient.openSession(false)) {
99 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
100 request.getTemplateId(), request.getTemplateName()));
102 ProjectWsRef.validateUuidAndKeyPair(request.getProjectId(), request.getProjectKey());
103 EntityDto entityDto = getEntityByKeyOrUuid(request.getProjectId(), request.getProjectKey(), dbSession);
104 checkGlobalAdmin(userSession);
106 if (entityDto.isProject()) {
107 managedInstanceChecker.throwIfProjectIsManaged(dbSession, entityDto.getUuid());
109 permissionTemplateService.applyAndCommit(dbSession, template, Collections.singletonList(entityDto));
113 private EntityDto getEntityByKeyOrUuid(@Nullable String uuid, @Nullable String key, DbSession dbSession) {
114 Optional<EntityDto> entityDto = uuid != null ? dbClient.entityDao().selectByUuid(dbSession, uuid) : dbClient.entityDao().selectByKey(dbSession, key);
115 if (entityDto.isPresent() && !Qualifiers.SUBVIEW.equals(entityDto.get().getQualifier())) {
116 return entityDto.get();
118 throw new NotFoundException("Entity not found");
122 private static class ApplyTemplateRequest {
123 private String projectId;
124 private String projectKey;
125 private String templateId;
126 private String templateName;
129 public String getProjectId() {
133 public ApplyTemplateRequest setProjectId(@Nullable String projectId) {
134 this.projectId = projectId;
139 public String getProjectKey() {
143 public ApplyTemplateRequest setProjectKey(@Nullable String projectKey) {
144 this.projectKey = projectKey;
149 public String getTemplateId() {
153 public ApplyTemplateRequest setTemplateId(@Nullable String templateId) {
154 this.templateId = templateId;
159 public String getTemplateName() {
163 public ApplyTemplateRequest setTemplateName(@Nullable String templateName) {
164 this.templateName = templateName;