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 org.sonar.api.server.ws.Change;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.permission.template.PermissionTemplateDto;
29 import org.sonar.server.permission.GroupUuidOrAnyone;
30 import org.sonar.server.permission.ws.PermissionWsSupport;
31 import org.sonar.server.permission.ws.PermissionsWsAction;
32 import org.sonar.server.permission.ws.WsParameters;
33 import org.sonar.server.user.UserSession;
35 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
36 import static org.sonar.server.permission.ws.WsParameters.createGroupNameParameter;
37 import static org.sonar.server.permission.ws.WsParameters.createTemplateParameters;
38 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
39 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
41 public class RemoveGroupFromTemplateAction implements PermissionsWsAction {
42 private final DbClient dbClient;
43 private final PermissionWsSupport wsSupport;
44 private final UserSession userSession;
45 private final WsParameters wsParameters;
47 public RemoveGroupFromTemplateAction(DbClient dbClient, PermissionWsSupport wsSupport, UserSession userSession, WsParameters wsParameters) {
48 this.dbClient = dbClient;
49 this.wsSupport = wsSupport;
50 this.userSession = userSession;
51 this.wsParameters = wsParameters;
55 public void define(WebService.NewController context) {
56 WebService.NewAction action = context
57 .createAction("remove_group_from_template")
60 .setDescription("Remove a group from a permission template.<br /> " +
61 "The group name must be provided. <br />" +
62 "Requires the following permission: 'Administer System'.")
64 new Change("10.0", "Parameter 'groupId' is removed. Use 'groupName' instead."),
65 new Change("8.4", "Parameter 'groupId' is deprecated. Format changes from integer to string. Use 'groupName' instead."))
68 createTemplateParameters(action);
69 wsParameters.createProjectPermissionParameter(action);
70 createGroupNameParameter(action);
74 public void handle(Request request, Response response) throws Exception {
75 try (DbSession dbSession = dbClient.openSession(false)) {
76 String permission = request.mandatoryParam(PARAM_PERMISSION);
77 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.fromRequest(request));
78 checkGlobalAdmin(userSession);
79 GroupUuidOrAnyone group = wsSupport.findGroup(dbSession, request);
81 dbClient.permissionTemplateDao().deleteGroupPermission(dbSession, template.getUuid(), group.getUuid(), permission,
82 template.getName(), request.param(PARAM_GROUP_NAME));