3 * Copyright (C) 2009-2017 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.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.template.PermissionTemplateDto;
28 import org.sonar.server.permission.UserId;
29 import org.sonar.server.permission.ws.PermissionWsSupport;
30 import org.sonar.server.permission.ws.PermissionsWsAction;
31 import org.sonar.server.user.UserSession;
32 import org.sonarqube.ws.client.permission.RemoveUserFromTemplateWsRequest;
34 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
35 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
36 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter;
37 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
38 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createUserLoginParameter;
39 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
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 PermissionWsSupport wsSupport;
48 private final UserSession userSession;
50 public RemoveUserFromTemplateAction(DbClient dbClient, PermissionWsSupport wsSupport, UserSession userSession) {
51 this.dbClient = dbClient;
52 this.wsSupport = wsSupport;
53 this.userSession = userSession;
56 private static RemoveUserFromTemplateWsRequest toRemoveUserFromTemplateWsRequest(Request request) {
57 return new RemoveUserFromTemplateWsRequest()
58 .setPermission(request.mandatoryParam(PARAM_PERMISSION))
59 .setLogin(request.mandatoryParam(PARAM_USER_LOGIN))
60 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
61 .setOrganization(request.param(PARAM_ORGANIZATION))
62 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
66 public void define(WebService.NewController context) {
67 WebService.NewAction action = context
68 .createAction("remove_user_from_template")
71 .setDescription("Remove a user from a permission template.<br /> " +
72 "Requires the following permission: 'Administer System'.")
75 createTemplateParameters(action);
76 createProjectPermissionParameter(action);
77 createUserLoginParameter(action);
81 public void handle(Request request, Response response) throws Exception {
82 doHandle(toRemoveUserFromTemplateWsRequest(request));
86 private void doHandle(RemoveUserFromTemplateWsRequest request) {
87 String permission = request.getPermission();
88 String userLogin = request.getLogin();
90 try (DbSession dbSession = dbClient.openSession(false)) {
91 validateProjectPermission(permission);
92 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(
93 request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
94 checkGlobalAdmin(userSession, template.getOrganizationUuid());
96 UserId user = wsSupport.findUser(dbSession, userLogin);
98 dbClient.permissionTemplateDao().deleteUserPermission(dbSession, template.getId(), user.getId(), permission);