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.component.ComponentDto;
33 import org.sonar.db.entity.EntityDto;
34 import org.sonar.db.permission.template.PermissionTemplateDto;
35 import org.sonar.server.exceptions.NotFoundException;
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.ProjectWsRef.newWsProjectRef;
44 import static org.sonar.server.permission.ws.WsParameters.createProjectParameters;
45 import static org.sonar.server.permission.ws.WsParameters.createTemplateParameters;
46 import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
52 public class ApplyTemplateAction implements PermissionsWsAction {
53 private final DbClient dbClient;
54 private final UserSession userSession;
55 private final PermissionTemplateService permissionTemplateService;
56 private final PermissionWsSupport wsSupport;
58 public ApplyTemplateAction(DbClient dbClient, UserSession userSession, PermissionTemplateService permissionTemplateService,
59 PermissionWsSupport wsSupport) {
60 this.dbClient = dbClient;
61 this.userSession = userSession;
62 this.permissionTemplateService = permissionTemplateService;
63 this.wsSupport = wsSupport;
66 private static ApplyTemplateRequest toApplyTemplateWsRequest(Request request) {
67 return new ApplyTemplateRequest()
68 .setProjectId(request.param(PARAM_PROJECT_ID))
69 .setProjectKey(request.param(PARAM_PROJECT_KEY))
70 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
71 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
75 public void define(WebService.NewController context) {
76 WebService.NewAction action = context.createAction("apply_template")
77 .setDescription("Apply a permission template to one project.<br>" +
78 "The project id or project key must be provided.<br>" +
79 "The template id or name must be provided.<br>" +
80 "Requires the following permission: 'Administer System'.")
85 createTemplateParameters(action);
86 createProjectParameters(action);
90 public void handle(Request request, Response response) throws Exception {
91 doHandle(toApplyTemplateWsRequest(request));
95 private void doHandle(ApplyTemplateRequest request) {
96 try (DbSession dbSession = dbClient.openSession(false)) {
97 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
98 request.getTemplateId(), request.getTemplateName()));
100 ProjectWsRef.validateUuidAndKeyPair(request.getProjectId(), request.getProjectKey());
101 EntityDto entityDto = getEntityByKeyOrUuid(request.getProjectId(), request.getProjectKey(), dbSession);
102 checkGlobalAdmin(userSession);
104 permissionTemplateService.applyAndCommit(dbSession, template, Collections.singletonList(entityDto));
108 private EntityDto getEntityByKeyOrUuid(@Nullable String uuid, @Nullable String key, DbSession dbSession) {
109 Optional<EntityDto> entityDto = uuid != null ? dbClient.entityDao().selectByUuid(dbSession, uuid) : dbClient.entityDao().selectByKey(dbSession, key);
110 if (entityDto.isPresent() && !Qualifiers.SUBVIEW.equals(entityDto.get().getQualifier())) {
111 return entityDto.get();
113 throw new NotFoundException("Entity not found");
117 private static class ApplyTemplateRequest {
118 private String projectId;
119 private String projectKey;
120 private String templateId;
121 private String templateName;
124 public String getProjectId() {
128 public ApplyTemplateRequest setProjectId(@Nullable String projectId) {
129 this.projectId = projectId;
134 public String getProjectKey() {
138 public ApplyTemplateRequest setProjectKey(@Nullable String projectKey) {
139 this.projectKey = projectKey;
144 public String getTemplateId() {
148 public ApplyTemplateRequest setTemplateId(@Nullable String templateId) {
149 this.templateId = templateId;
154 public String getTemplateName() {
158 public ApplyTemplateRequest setTemplateName(@Nullable String templateName) {
159 this.templateName = templateName;