]> source.dussan.org Git - sonarqube.git/blob
f694f5ec6c4dd65ab77c1013584fab199fffc2e2
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.permission.ws.template;
21
22 import java.util.Optional;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.utils.System2;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
30 import org.sonar.db.permission.template.PermissionTemplateDto;
31 import org.sonar.server.permission.ws.PermissionWsSupport;
32 import org.sonar.server.permission.ws.PermissionsWsAction;
33 import org.sonar.server.user.UserSession;
34 import org.sonarqube.ws.client.permission.AddProjectCreatorToTemplateWsRequest;
35
36 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
37 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
38 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter;
39 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
40 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
44
45 public class AddProjectCreatorToTemplateAction implements PermissionsWsAction {
46   private final DbClient dbClient;
47   private final PermissionWsSupport wsSupport;
48   private final UserSession userSession;
49   private final System2 system;
50
51   public AddProjectCreatorToTemplateAction(DbClient dbClient, PermissionWsSupport wsSupport, UserSession userSession, System2 system) {
52     this.dbClient = dbClient;
53     this.wsSupport = wsSupport;
54     this.userSession = userSession;
55     this.system = system;
56   }
57
58   private static AddProjectCreatorToTemplateWsRequest toWsRequest(Request request) {
59     AddProjectCreatorToTemplateWsRequest wsRequest = AddProjectCreatorToTemplateWsRequest.builder()
60       .setPermission(request.mandatoryParam(PARAM_PERMISSION))
61       .setTemplateId(request.param(PARAM_TEMPLATE_ID))
62       .setOrganization(request.param(PARAM_ORGANIZATION))
63       .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
64       .build();
65     validateProjectPermission(wsRequest.getPermission());
66     return wsRequest;
67   }
68
69   @Override
70   public void define(WebService.NewController context) {
71     WebService.NewAction action = context.createAction("add_project_creator_to_template")
72       .setDescription("Add a project creator to a permission template.<br>" +
73         "Requires the following permission: 'Administer System'.")
74       .setSince("6.0")
75       .setPost(true)
76       .setHandler(this);
77
78     createTemplateParameters(action);
79     createProjectPermissionParameter(action);
80   }
81
82   @Override
83   public void handle(Request request, Response response) throws Exception {
84     doHandle(toWsRequest(request));
85     response.noContent();
86   }
87
88   private void doHandle(AddProjectCreatorToTemplateWsRequest request) {
89     try (DbSession dbSession = dbClient.openSession(false)) {
90       PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(
91         request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
92       checkGlobalAdmin(userSession, template.getOrganizationUuid());
93
94       Optional<PermissionTemplateCharacteristicDto> templatePermission = dbClient.permissionTemplateCharacteristicDao()
95         .selectByPermissionAndTemplateId(dbSession, request.getPermission(), template.getId());
96       if (templatePermission.isPresent()) {
97         updateTemplatePermission(dbSession, templatePermission.get());
98       } else {
99         addTemplatePermission(dbSession, request, template);
100       }
101     }
102   }
103
104   private void addTemplatePermission(DbSession dbSession, AddProjectCreatorToTemplateWsRequest request, PermissionTemplateDto template) {
105     long now = system.now();
106     dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto()
107       .setPermission(request.getPermission())
108       .setTemplateId(template.getId())
109       .setWithProjectCreator(true)
110       .setCreatedAt(now)
111       .setUpdatedAt(now));
112     dbSession.commit();
113   }
114
115   private void updateTemplatePermission(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermission) {
116     PermissionTemplateCharacteristicDto targetTemplatePermission = templatePermission
117       .setUpdatedAt(system.now())
118       .setWithProjectCreator(true);
119     dbClient.permissionTemplateCharacteristicDao().update(dbSession, targetTemplatePermission);
120     dbSession.commit();
121   }
122 }