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 com.google.common.base.Predicate;
23 import java.util.List;
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.PermissionQuery;
30 import org.sonar.db.permission.PermissionTemplateDto;
31 import org.sonar.db.permission.UserWithPermissionDto;
32 import org.sonar.db.user.UserDto;
33 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
34 import org.sonar.server.permission.ws.PermissionsWsAction;
35 import org.sonar.server.user.UserSession;
36 import org.sonarqube.ws.client.permission.AddUserToTemplateWsRequest;
38 import static com.google.common.collect.FluentIterable.from;
39 import static org.sonar.db.user.GroupMembershipQuery.IN;
40 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
41 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
42 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter;
43 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
44 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createUserLoginParameter;
45 import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
51 public class AddUserToTemplateAction implements PermissionsWsAction {
52 private final DbClient dbClient;
53 private final PermissionDependenciesFinder dependenciesFinder;
54 private final UserSession userSession;
56 public AddUserToTemplateAction(DbClient dbClient, PermissionDependenciesFinder dependenciesFinder, UserSession userSession) {
57 this.dbClient = dbClient;
58 this.dependenciesFinder = dependenciesFinder;
59 this.userSession = userSession;
63 public void define(WebService.NewController context) {
64 WebService.NewAction action = context
65 .createAction("add_user_to_template")
68 .setDescription("Add a user to a permission template.<br /> " +
69 "It requires administration permissions to access.")
72 createTemplateParameters(action);
73 createProjectPermissionParameter(action);
74 createUserLoginParameter(action);
78 public void handle(Request request, Response response) throws Exception {
79 checkGlobalAdminUser(userSession);
80 doHandle(toAddUserToTemplateWsRequest(request));
84 private void doHandle(AddUserToTemplateWsRequest request) {
85 String permission = request.getPermission();
86 final String userLogin = request.getLogin();
88 DbSession dbSession = dbClient.openSession(false);
90 validateProjectPermission(permission);
91 PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
92 UserDto user = dependenciesFinder.getUser(dbSession, userLogin);
94 if (!isUserAlreadyAdded(dbSession, template.getId(), userLogin, permission)) {
95 dbClient.permissionTemplateDao().insertUserPermission(dbSession, template.getId(), user.getId(), permission);
98 dbClient.closeSession(dbSession);
102 private static AddUserToTemplateWsRequest toAddUserToTemplateWsRequest(Request request) {
103 return new AddUserToTemplateWsRequest()
104 .setLogin(request.mandatoryParam(PARAM_USER_LOGIN))
105 .setPermission(request.mandatoryParam(PARAM_PERMISSION))
106 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
107 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
110 private boolean isUserAlreadyAdded(DbSession dbSession, long templateId, String userLogin, String permission) {
111 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
112 List<UserWithPermissionDto> usersWithPermission = dbClient.permissionTemplateDao().selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE);
113 return from(usersWithPermission).anyMatch(new HasUserPredicate(userLogin));
116 private static class HasUserPredicate implements Predicate<UserWithPermissionDto> {
117 private final String userLogin;
119 public HasUserPredicate(String userLogin) {
120 this.userLogin = userLogin;
124 public boolean apply(UserWithPermissionDto userWithPermission) {
125 return userLogin.equals(userWithPermission.getLogin());