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.
21 package org.sonar.server.permission.ws.template;
23 import java.util.Optional;
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.api.utils.System2;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.permission.PermissionTemplateDto;
31 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao;
32 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
33 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
34 import org.sonar.server.permission.ws.PermissionsWsAction;
35 import org.sonar.server.permission.ws.WsTemplateRef;
36 import org.sonar.server.user.UserSession;
37 import org.sonarqube.ws.client.permission.RemoveProjectCreatorFromTemplateWsRequest;
39 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
40 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
41 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter;
42 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
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;
47 public class RemoveProjectCreatorFromTemplateAction implements PermissionsWsAction {
48 private final DbClient dbClient;
49 private final PermissionDependenciesFinder dependenciesFinder;
50 private final UserSession userSession;
51 private final System2 system;
53 public RemoveProjectCreatorFromTemplateAction(DbClient dbClient, PermissionDependenciesFinder dependenciesFinder, UserSession userSession, System2 system) {
54 this.dbClient = dbClient;
55 this.dependenciesFinder = dependenciesFinder;
56 this.userSession = userSession;
61 public void define(WebService.NewController context) {
62 WebService.NewAction action = context.createAction("remove_project_creator_from_template")
63 .setDescription("Remove a project creator from a permission template.<br>" +
64 "Requires the 'Administer' permission.")
69 createTemplateParameters(action);
70 createProjectPermissionParameter(action);
74 public void handle(Request request, Response response) throws Exception {
75 checkGlobalAdminUser(userSession);
76 doHandle(toWsRequest(request));
80 private void doHandle(RemoveProjectCreatorFromTemplateWsRequest request) {
81 DbSession dbSession = dbClient.openSession(false);
83 PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getTemplateName()));
84 PermissionTemplateCharacteristicDao dao = dbClient.permissionTemplateCharacteristicDao();
85 Optional<PermissionTemplateCharacteristicDto> templatePermission = dao.selectByPermissionAndTemplateId(dbSession, request.getPermission(), template.getId());
86 if (templatePermission.isPresent()) {
87 updateTemplateCharacteristic(dbSession, templatePermission.get());
90 dbClient.closeSession(dbSession);
94 private void updateTemplateCharacteristic(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermission) {
95 PermissionTemplateCharacteristicDto targetTemplatePermission = templatePermission
96 .setUpdatedAt(system.now())
97 .setWithProjectCreator(false);
98 dbClient.permissionTemplateCharacteristicDao().update(dbSession, targetTemplatePermission);
102 private static RemoveProjectCreatorFromTemplateWsRequest toWsRequest(Request request) {
103 RemoveProjectCreatorFromTemplateWsRequest wsRequest = RemoveProjectCreatorFromTemplateWsRequest.builder()
104 .setPermission(request.mandatoryParam(PARAM_PERMISSION))
105 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
106 .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
108 validateProjectPermission(wsRequest.getPermission());