2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.permission.ws.template;
23 import com.google.common.base.Predicate;
24 import java.util.List;
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.PermissionTemplateDto;
32 import org.sonar.db.permission.UserWithPermissionDto;
33 import org.sonar.db.user.UserDto;
34 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
35 import org.sonar.server.permission.ws.PermissionsWsAction;
36 import org.sonar.server.permission.ws.WsTemplateRef;
37 import org.sonar.server.user.UserSession;
39 import static com.google.common.collect.FluentIterable.from;
40 import static org.sonar.db.user.GroupMembershipQuery.IN;
41 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
42 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PERMISSION;
43 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_USER_LOGIN;
44 import static org.sonar.server.permission.ws.PermissionsWsParameters.createProjectPermissionParameter;
45 import static org.sonar.server.permission.ws.PermissionsWsParameters.createTemplateParameters;
46 import static org.sonar.server.permission.ws.PermissionsWsParameters.createUserLoginParameter;
47 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
49 public class AddUserToTemplateAction implements PermissionsWsAction {
50 private final DbClient dbClient;
51 private final PermissionDependenciesFinder dependenciesFinder;
52 private final UserSession userSession;
54 public AddUserToTemplateAction(DbClient dbClient, PermissionDependenciesFinder dependenciesFinder, UserSession userSession) {
55 this.dbClient = dbClient;
56 this.dependenciesFinder = dependenciesFinder;
57 this.userSession = userSession;
61 public void define(WebService.NewController context) {
62 WebService.NewAction action = context
63 .createAction("add_user_to_template")
66 .setDescription("Add a user to a permission template.<br /> " +
67 "It requires administration permissions to access.")
70 createTemplateParameters(action);
71 createProjectPermissionParameter(action);
72 createUserLoginParameter(action);
76 public void handle(Request wsRequest, Response wsResponse) throws Exception {
77 checkGlobalAdminUser(userSession);
79 String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
80 final String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN);
82 DbSession dbSession = dbClient.openSession(false);
84 validateProjectPermission(permission);
85 PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest));
86 UserDto user = dependenciesFinder.getUser(dbSession, userLogin);
88 if (!isUserAlreadyAdded(dbSession, template.getId(), userLogin, permission)) {
89 dbClient.permissionTemplateDao().insertUserPermission(dbSession, template.getId(), user.getId(), permission);
92 dbClient.closeSession(dbSession);
95 wsResponse.noContent();
98 private boolean isUserAlreadyAdded(DbSession dbSession, long templateId, String userLogin, String permission) {
99 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
100 List<UserWithPermissionDto> usersWithPermission = dbClient.permissionTemplateDao().selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE);
101 return from(usersWithPermission).anyMatch(new HasUserPredicate(userLogin));
104 private static class HasUserPredicate implements Predicate<UserWithPermissionDto> {
105 private final String userLogin;
107 public HasUserPredicate(String userLogin) {
108 this.userLogin = userLogin;
112 public boolean apply(UserWithPermissionDto userWithPermission) {
113 return userLogin.equals(userWithPermission.getLogin());