3 * Copyright (C) 2009-2021 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 javax.annotation.CheckForNull;
23 import javax.annotation.Nullable;
24 import org.sonar.api.server.ws.Request;
25 import org.sonar.api.server.ws.Response;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.permission.template.PermissionTemplateDto;
30 import org.sonar.server.permission.RequestValidator;
31 import org.sonar.db.user.UserIdDto;
32 import org.sonar.server.permission.ws.PermissionWsSupport;
33 import org.sonar.server.permission.ws.PermissionsWsAction;
34 import org.sonar.server.permission.ws.WsParameters;
35 import org.sonar.server.user.UserSession;
37 import static java.util.Objects.requireNonNull;
38 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
39 import static org.sonar.server.permission.ws.WsParameters.createTemplateParameters;
40 import static org.sonar.server.permission.ws.WsParameters.createUserLoginParameter;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
46 public class RemoveUserFromTemplateAction implements PermissionsWsAction {
47 private final DbClient dbClient;
48 private final PermissionWsSupport wsSupport;
49 private final UserSession userSession;
50 private final WsParameters wsParameters;
51 private final RequestValidator requestValidator;
53 public RemoveUserFromTemplateAction(DbClient dbClient, PermissionWsSupport wsSupport, UserSession userSession, WsParameters wsParameters, RequestValidator requestValidator) {
54 this.dbClient = dbClient;
55 this.wsSupport = wsSupport;
56 this.userSession = userSession;
57 this.wsParameters = wsParameters;
58 this.requestValidator = requestValidator;
61 private static RemoveUserFromTemplateRequest toRemoveUserFromTemplateWsRequest(Request request) {
62 return new RemoveUserFromTemplateRequest()
63 .setPermission(request.mandatoryParam(PARAM_PERMISSION))
64 .setLogin(request.mandatoryParam(PARAM_USER_LOGIN))
65 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
66 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
70 public void define(WebService.NewController context) {
71 WebService.NewAction action = context
72 .createAction("remove_user_from_template")
75 .setDescription("Remove a user from a permission template.<br /> " +
76 "Requires the following permission: 'Administer System'.")
79 createTemplateParameters(action);
80 wsParameters.createProjectPermissionParameter(action);
81 createUserLoginParameter(action);
85 public void handle(Request request, Response response) throws Exception {
86 doHandle(toRemoveUserFromTemplateWsRequest(request));
90 private void doHandle(RemoveUserFromTemplateRequest request) {
91 String permission = request.getPermission();
92 String userLogin = request.getLogin();
94 try (DbSession dbSession = dbClient.openSession(false)) {
95 requestValidator.validateProjectPermission(permission);
96 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getTemplateName()));
97 checkGlobalAdmin(userSession);
99 UserIdDto user = wsSupport.findUser(dbSession, userLogin);
101 dbClient.permissionTemplateDao().deleteUserPermission(dbSession, template.getUuid(), user.getUuid(), permission, template.getName(), user.getLogin());
106 private static class RemoveUserFromTemplateRequest {
107 private String login;
108 private String permission;
109 private String templateId;
110 private String templateName;
112 public String getLogin() {
116 public RemoveUserFromTemplateRequest setLogin(String login) {
117 this.login = requireNonNull(login);
121 public String getPermission() {
125 public RemoveUserFromTemplateRequest setPermission(String permission) {
126 this.permission = requireNonNull(permission);
131 public String getTemplateId() {
135 public RemoveUserFromTemplateRequest setTemplateId(@Nullable String templateId) {
136 this.templateId = templateId;
141 public String getTemplateName() {
145 public RemoveUserFromTemplateRequest setTemplateName(@Nullable String templateName) {
146 this.templateName = templateName;