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.UserDto;
30 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
31 import org.sonar.server.permission.ws.PermissionsWsAction;
32 import org.sonar.server.permission.ws.WsTemplateRef;
33 import org.sonar.server.user.UserSession;
35 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
36 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PERMISSION;
37 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_USER_LOGIN;
38 import static org.sonar.server.permission.ws.PermissionsWsParameters.createProjectPermissionParameter;
39 import static org.sonar.server.permission.ws.PermissionsWsParameters.createTemplateParameters;
40 import static org.sonar.server.permission.ws.PermissionsWsParameters.createUserLoginParameter;
41 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
43 public class RemoveUserFromTemplateAction implements PermissionsWsAction {
44 private final DbClient dbClient;
45 private final PermissionDependenciesFinder dependenciesFinder;
46 private final UserSession userSession;
48 public RemoveUserFromTemplateAction(DbClient dbClient, PermissionDependenciesFinder dependenciesFinder, UserSession userSession) {
49 this.dbClient = dbClient;
50 this.dependenciesFinder = dependenciesFinder;
51 this.userSession = userSession;
55 public void define(WebService.NewController context) {
56 WebService.NewAction action = context
57 .createAction("remove_user_from_template")
60 .setDescription("Remove a user from a permission template.<br /> " +
61 "It requires administration permissions to access.")
64 createTemplateParameters(action);
65 createProjectPermissionParameter(action);
66 createUserLoginParameter(action);
70 public void handle(Request wsRequest, Response wsResponse) throws Exception {
71 checkGlobalAdminUser(userSession);
73 String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
74 String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN);
76 DbSession dbSession = dbClient.openSession(false);
78 validateProjectPermission(permission);
79 PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest));
80 UserDto user = dependenciesFinder.getUser(dbSession, userLogin);
82 dbClient.permissionTemplateDao().deleteUserPermission(dbSession, template.getId(), user.getId(), permission);
85 dbClient.closeSession(dbSession);
88 wsResponse.noContent();