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 java.util.List;
23 import javax.annotation.CheckForNull;
24 import javax.annotation.Nullable;
25 import org.sonar.api.server.ws.Request;
26 import org.sonar.api.server.ws.Response;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.permission.PermissionQuery;
31 import org.sonar.db.permission.template.PermissionTemplateDto;
32 import org.sonar.db.user.UserIdDto;
33 import org.sonar.server.permission.ws.PermissionWsSupport;
34 import org.sonar.server.permission.ws.PermissionsWsAction;
35 import org.sonar.server.permission.ws.WsParameters;
36 import org.sonar.server.user.UserSession;
38 import static java.util.Objects.requireNonNull;
39 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
40 import static org.sonar.server.permission.ws.WsParameters.createTemplateParameters;
41 import static org.sonar.server.permission.ws.WsParameters.createUserLoginParameter;
42 import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
48 public class AddUserToTemplateAction implements PermissionsWsAction {
49 private final DbClient dbClient;
50 private final PermissionWsSupport wsSupport;
51 private final UserSession userSession;
52 private final WsParameters wsParameters;
54 public AddUserToTemplateAction(DbClient dbClient, PermissionWsSupport wsSupport, UserSession userSession, WsParameters wsParameters) {
55 this.dbClient = dbClient;
56 this.wsSupport = wsSupport;
57 this.userSession = userSession;
58 this.wsParameters = wsParameters;
61 private static AddUserToTemplateRequest toAddUserToTemplateWsRequest(Request request) {
62 return new AddUserToTemplateRequest()
63 .setLogin(request.mandatoryParam(PARAM_USER_LOGIN))
64 .setPermission(request.mandatoryParam(PARAM_PERMISSION))
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("add_user_to_template")
75 .setDescription("Add a user to 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(toAddUserToTemplateWsRequest(request));
90 private void doHandle(AddUserToTemplateRequest request) {
91 String permission = request.getPermission();
92 String userLogin = request.getLogin();
94 try (DbSession dbSession = dbClient.openSession(false)) {
95 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
96 request.getTemplateId(), request.getTemplateName()));
97 checkGlobalAdmin(userSession);
98 UserIdDto user = wsSupport.findUser(dbSession, userLogin);
100 if (!isUserAlreadyAdded(dbSession, template.getUuid(), userLogin, permission)) {
101 dbClient.permissionTemplateDao().insertUserPermission(dbSession, template.getUuid(), user.getUuid(), permission,
102 template.getName(), user.getLogin());
108 private boolean isUserAlreadyAdded(DbSession dbSession, String templateUuid, String userLogin, String permission) {
109 PermissionQuery permissionQuery = PermissionQuery.builder().setPermission(permission).build();
110 List<String> usersWithPermission = dbClient.permissionTemplateDao().selectUserLoginsByQueryAndTemplate(dbSession, permissionQuery, templateUuid);
111 return usersWithPermission.stream().anyMatch(s -> s.equals(userLogin));
114 private static class AddUserToTemplateRequest {
115 private String login;
116 private String permission;
117 private String templateId;
118 private String templateName;
120 public String getLogin() {
124 public AddUserToTemplateRequest setLogin(String login) {
125 this.login = requireNonNull(login);
129 public String getPermission() {
133 public AddUserToTemplateRequest setPermission(String permission) {
134 this.permission = requireNonNull(permission);
139 public String getTemplateId() {
143 public AddUserToTemplateRequest setTemplateId(@Nullable String templateId) {
144 this.templateId = templateId;
149 public String getTemplateName() {
153 public AddUserToTemplateRequest setTemplateName(@Nullable String templateName) {
154 this.templateName = templateName;