3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.Request;
23 import org.sonar.api.server.ws.Response;
24 import org.sonar.api.server.ws.WebService;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.permission.PermissionTemplateDto;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
30 import org.sonar.server.permission.ws.PermissionsWsAction;
31 import org.sonar.server.permission.ws.WsTemplateRef;
32 import org.sonar.server.user.UserSession;
33 import org.sonarqube.ws.client.permission.RemoveUserFromTemplateWsRequest;
35 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
36 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
37 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter;
38 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
39 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createUserLoginParameter;
40 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
45 public class RemoveUserFromTemplateAction implements PermissionsWsAction {
46 private final DbClient dbClient;
47 private final PermissionDependenciesFinder dependenciesFinder;
48 private final UserSession userSession;
50 public RemoveUserFromTemplateAction(DbClient dbClient, PermissionDependenciesFinder dependenciesFinder, UserSession userSession) {
51 this.dbClient = dbClient;
52 this.dependenciesFinder = dependenciesFinder;
53 this.userSession = userSession;
57 public void define(WebService.NewController context) {
58 WebService.NewAction action = context
59 .createAction("remove_user_from_template")
62 .setDescription("Remove a user from a permission template.<br /> " +
63 "It requires administration permissions to access.")
66 createTemplateParameters(action);
67 createProjectPermissionParameter(action);
68 createUserLoginParameter(action);
72 public void handle(Request request, Response response) throws Exception {
73 doHandle(toRemoveUserFromTemplateWsRequest(request));
77 private void doHandle(RemoveUserFromTemplateWsRequest request) {
78 checkGlobalAdminUser(userSession);
80 String permission = request.getPermission();
81 String userLogin = request.getLogin();
83 DbSession dbSession = dbClient.openSession(false);
85 validateProjectPermission(permission);
86 PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getTemplateName()));
87 UserDto user = dependenciesFinder.getUser(dbSession, userLogin);
89 dbClient.permissionTemplateDao().deleteUserPermission(dbSession, template.getId(), user.getId(), permission);
92 dbClient.closeSession(dbSession);
96 private static RemoveUserFromTemplateWsRequest toRemoveUserFromTemplateWsRequest(Request request) {
97 return new RemoveUserFromTemplateWsRequest()
98 .setPermission(request.mandatoryParam(PARAM_PERMISSION))
99 .setLogin(request.mandatoryParam(PARAM_USER_LOGIN))
100 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
101 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));