2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.permission.ws.template;
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.PermissionTemplateDto;
29 import org.sonar.db.user.GroupDto;
30 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
31 import org.sonar.server.permission.ws.PermissionsWsAction;
32 import org.sonar.server.usergroups.ws.WsGroupRef;
33 import org.sonar.server.permission.ws.WsTemplateRef;
34 import org.sonar.server.user.UserSession;
36 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
37 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
38 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createGroupIdParameter;
39 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createGroupNameParameter;
40 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter;
41 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
42 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
44 public class RemoveGroupFromTemplateAction implements PermissionsWsAction {
45 private final DbClient dbClient;
46 private final PermissionDependenciesFinder dependenciesFinder;
47 private final UserSession userSession;
49 public RemoveGroupFromTemplateAction(DbClient dbClient, PermissionDependenciesFinder dependenciesFinder, UserSession userSession) {
50 this.dbClient = dbClient;
51 this.dependenciesFinder = dependenciesFinder;
52 this.userSession = userSession;
56 public void define(WebService.NewController context) {
57 WebService.NewAction action = context
58 .createAction("remove_group_from_template")
61 .setDescription("Remove a group from a permission template.<br /> " +
62 "The group id or group name must be provided. <br />" +
63 "It requires administration permissions to access.")
66 createTemplateParameters(action);
67 createProjectPermissionParameter(action);
68 createGroupIdParameter(action);
69 createGroupNameParameter(action);
73 public void handle(Request wsRequest, Response wsResponse) throws Exception {
74 checkGlobalAdminUser(userSession);
76 String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
77 WsGroupRef group = WsGroupRef.newWsGroupRefFromPermissionRequest(wsRequest);
79 DbSession dbSession = dbClient.openSession(false);
81 validateProjectPermission(permission);
83 PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest));
84 GroupDto groupDto = dependenciesFinder.getGroup(dbSession, group);
86 Long groupId = groupDto == null ? null : groupDto.getId();
87 dbClient.permissionTemplateDao().deleteGroupPermission(dbSession, template.getId(), groupId, permission);
89 dbClient.closeSession(dbSession);
92 wsResponse.noContent();