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 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.PermissionQuery;
30 import org.sonar.db.permission.PermissionTemplateDto;
31 import org.sonar.db.user.GroupDto;
32 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
33 import org.sonar.server.permission.ws.PermissionsWsAction;
34 import org.sonar.server.user.UserSession;
35 import org.sonar.server.usergroups.ws.WsGroupRef;
36 import org.sonarqube.ws.client.permission.AddGroupToTemplateWsRequest;
38 import static org.sonar.api.security.DefaultGroups.ANYONE;
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.validateNotAnyoneAndAdminPermission;
42 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
43 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_GROUP_ID;
44 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_GROUP_NAME;
45 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PERMISSION;
46 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
47 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_UUID;
48 import static org.sonar.server.permission.ws.PermissionsWsParameters.createGroupIdParameter;
49 import static org.sonar.server.permission.ws.PermissionsWsParameters.createGroupNameParameter;
50 import static org.sonar.server.permission.ws.PermissionsWsParameters.createProjectPermissionParameter;
51 import static org.sonar.server.permission.ws.PermissionsWsParameters.createTemplateParameters;
52 import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
53 import static org.sonar.server.usergroups.ws.WsGroupRef.newWsGroupRef;
55 public class AddGroupToTemplateAction implements PermissionsWsAction {
56 private final DbClient dbClient;
57 private final PermissionDependenciesFinder dependenciesFinder;
58 private final UserSession userSession;
60 public AddGroupToTemplateAction(DbClient dbClient, PermissionDependenciesFinder dependenciesFinder, UserSession userSession) {
61 this.dbClient = dbClient;
62 this.dependenciesFinder = dependenciesFinder;
63 this.userSession = userSession;
67 public void define(WebService.NewController context) {
68 WebService.NewAction action = context
69 .createAction("add_group_to_template")
72 .setDescription("Add a group to a permission template.<br /> " +
73 "The group id or group name must be provided. <br />" +
74 "It requires administration permissions to access.")
77 createTemplateParameters(action);
78 createProjectPermissionParameter(action);
79 createGroupIdParameter(action);
80 createGroupNameParameter(action);
84 public void handle(Request wsRequest, Response wsResponse) {
85 checkGlobalAdminUser(userSession);
86 doHandle(toAddGroupToTemplateWsRequest(wsRequest));
87 wsResponse.noContent();
90 private void doHandle(AddGroupToTemplateWsRequest wsRequest) {
91 String permission = wsRequest.getPermission();
92 Long requestGroupId = wsRequest.getGroupId() == null ? null : Long.valueOf(wsRequest.getGroupId());
93 WsGroupRef group = newWsGroupRef(requestGroupId, wsRequest.getGroupName());
95 DbSession dbSession = dbClient.openSession(false);
97 validateProjectPermission(permission);
98 validateNotAnyoneAndAdminPermission(permission, group.name());
100 PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, newTemplateRef(wsRequest.getTemplateId(), wsRequest.getTemplateName()));
101 GroupDto groupDto = dependenciesFinder.getGroup(dbSession, group);
103 if (!groupAlreadyAdded(dbSession, template.getId(), groupDto, permission)) {
104 Long groupId = groupDto == null ? null : groupDto.getId();
105 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getId(), groupId, permission);
108 dbClient.closeSession(dbSession);
112 private boolean groupAlreadyAdded(DbSession dbSession, long templateId, @Nullable GroupDto group, String permission) {
113 String groupName = group == null ? ANYONE : group.getName();
114 PermissionQuery permissionQuery = PermissionQuery.builder().membership(IN).permission(permission).build();
115 return dbClient.permissionTemplateDao().hasGroup(dbSession, permissionQuery, templateId, groupName);
118 private static AddGroupToTemplateWsRequest toAddGroupToTemplateWsRequest(Request request) {
119 return new AddGroupToTemplateWsRequest()
120 .setPermission(request.mandatoryParam(PARAM_PERMISSION))
121 .setGroupId(request.param(PARAM_GROUP_ID))
122 .setGroupName(request.param(PARAM_GROUP_NAME))
123 .setTemplateId(request.param(PARAM_TEMPLATE_UUID))
124 .setTemplateName(request.param(PARAM_TEMPLATE_NAME));