]> source.dussan.org Git - sonarqube.git/blob
5fd81105995fff41456f31839d8cbb8bb46bd463
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.qualityprofile.ws;
21
22 import org.sonar.api.resources.Languages;
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.server.ws.WebService.NewAction;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.organization.OrganizationDto;
32 import org.sonar.db.qualityprofile.QProfileDto;
33 import org.sonar.server.component.ComponentFinder;
34 import org.sonar.server.user.UserSession;
35
36 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_09;
37 import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
38 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
39 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_REMOVE_PROJECT;
40 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT;
41 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_UUID;
42
43 public class RemoveProjectAction implements QProfileWsAction {
44
45   private final DbClient dbClient;
46   private final UserSession userSession;
47   private final Languages languages;
48   private final ComponentFinder componentFinder;
49   private final QProfileWsSupport wsSupport;
50
51   public RemoveProjectAction(DbClient dbClient, UserSession userSession, Languages languages, ComponentFinder componentFinder, QProfileWsSupport wsSupport) {
52     this.dbClient = dbClient;
53     this.userSession = userSession;
54     this.languages = languages;
55     this.componentFinder = componentFinder;
56     this.wsSupport = wsSupport;
57   }
58
59   @Override
60   public void define(WebService.NewController controller) {
61     NewAction action = controller.createAction(ACTION_REMOVE_PROJECT)
62       .setSince("5.2")
63       .setDescription("Remove a project's association with a quality profile.<br> " +
64         "Requires one of the following permissions:" +
65         "<ul>" +
66         "  <li>'Administer Quality Profiles'</li>" +
67         "  <li>Edit right on the specified quality profile</li>" +
68         "  <li>Administer right on the specified project</li>" +
69         "</ul>")
70       .setPost(true)
71       .setHandler(this);
72     QProfileReference.defineParams(action, languages);
73     QProfileWsSupport.createOrganizationParam(action).setSince("6.4");
74
75     action.createParam(PARAM_PROJECT)
76       .setDescription("Project key")
77       .setDeprecatedKey("projectKey", "6.5")
78       .setExampleValue(KEY_PROJECT_EXAMPLE_001);
79     
80     action.createParam(PARAM_PROJECT_UUID)
81       .setDescription("Project ID. Either this parameter, or '%s' must be set.", PARAM_PROJECT)
82       .setDeprecatedSince("6.5")
83       .setExampleValue(UUID_EXAMPLE_09);
84   }
85
86   @Override
87   public void handle(Request request, Response response) throws Exception {
88     userSession.checkLoggedIn();
89
90     try (DbSession dbSession = dbClient.openSession(false)) {
91       ComponentDto project = loadProject(dbSession, request);
92       QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.from(request));
93       OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
94       checkPermissions(dbSession, organization, profile, project);
95       if (!profile.getOrganizationUuid().equals(project.getOrganizationUuid())) {
96         throw new IllegalArgumentException("Project and Quality profile must have the same organization");
97       }
98
99       dbClient.qualityProfileDao().deleteProjectProfileAssociation(dbSession, project, profile);
100       dbSession.commit();
101
102       response.noContent();
103     }
104   }
105
106   private ComponentDto loadProject(DbSession dbSession, Request request) {
107     String projectKey = request.param(PARAM_PROJECT);
108     String projectUuid = request.param(PARAM_PROJECT_UUID);
109     return componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey, ComponentFinder.ParamNames.PROJECT_UUID_AND_PROJECT);
110   }
111
112   private void checkPermissions(DbSession dbSession, OrganizationDto organization, QProfileDto profile, ComponentDto project) {
113     if (wsSupport.canEdit(dbSession, organization, profile) || userSession.hasComponentPermission(UserRole.ADMIN, project)) {
114       return;
115     }
116
117     throw insufficientPrivilegesException();
118   }
119 }