3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info 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.
20 package org.sonar.server.qualityprofile.ws;
22 import java.util.Optional;
23 import org.sonar.api.rule.RuleKey;
24 import org.sonar.api.server.ServerSide;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.permission.GlobalPermission;
28 import org.sonar.db.qualityprofile.QProfileDto;
29 import org.sonar.db.rule.RuleDefinitionDto;
30 import org.sonar.db.user.GroupDto;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.user.UserSession;
34 import static com.google.common.base.Preconditions.checkState;
35 import static org.sonar.server.exceptions.BadRequestException.checkRequest;
36 import static org.sonar.server.exceptions.NotFoundException.checkFound;
37 import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
38 import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
41 public class QProfileWsSupport {
43 private final DbClient dbClient;
44 private final UserSession userSession;
46 public QProfileWsSupport(DbClient dbClient, UserSession userSession) {
47 this.dbClient = dbClient;
48 this.userSession = userSession;
51 public RuleDefinitionDto getRule(DbSession dbSession, RuleKey ruleKey) {
52 Optional<RuleDefinitionDto> ruleDefinitionDto = dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey);
53 RuleDefinitionDto rule = checkFoundWithOptional(ruleDefinitionDto, "Rule with key '%s' not found", ruleKey);
54 checkRequest(!rule.isExternal(), "Operation forbidden for rule '%s' imported from an external rule engine.", ruleKey);
59 * Get the Quality profile specified by the reference {@code ref}.
61 * @throws org.sonar.server.exceptions.NotFoundException if the specified organization or profile do not exist
63 public QProfileDto getProfile(DbSession dbSession, QProfileReference ref) {
66 profile = dbClient.qualityProfileDao().selectByUuid(dbSession, ref.getKey());
67 checkFound(profile, "Quality Profile with key '%s' does not exist", ref.getKey());
69 profile = dbClient.qualityProfileDao().selectByNameAndLanguage(dbSession, ref.getName(), ref.getLanguage());
70 checkFound(profile, "Quality Profile for language '%s' and name '%s' does not exist", ref.getLanguage(), ref.getName());
75 public QProfileDto getProfile(DbSession dbSession, String name, String language) {
76 QProfileDto profile = dbClient.qualityProfileDao().selectByNameAndLanguage(dbSession, name, language);
77 checkFound(profile, "Quality Profile for language '%s' and name '%s' does not exist", language, name);
81 public UserDto getUser(DbSession dbSession, String login) {
82 UserDto user = dbClient.userDao().selectActiveUserByLogin(dbSession, login);
83 checkFound(user, "User with login '%s' is not found'", login);
87 GroupDto getGroup(DbSession dbSession, String groupName) {
88 Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, groupName);
89 checkFoundWithOptional(group, "No group with name '%s'", groupName);
93 boolean canEdit(DbSession dbSession, QProfileDto profile) {
94 if (profile.isBuiltIn() || !userSession.isLoggedIn()) {
97 if (userSession.hasPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES)) {
101 UserDto user = dbClient.userDao().selectByLogin(dbSession, userSession.getLogin());
102 checkState(user != null, "User from session does not exist");
103 return dbClient.qProfileEditUsersDao().exists(dbSession, profile, user)
104 || dbClient.qProfileEditGroupsDao().exists(dbSession, profile, userSession.getGroups());
107 public void checkCanEdit(DbSession dbSession, QProfileDto profile) {
108 checkNotBuiltIn(profile);
109 if (!canEdit(dbSession, profile)) {
110 throw insufficientPrivilegesException();
114 void checkNotBuiltIn(QProfileDto profile) {
115 checkRequest(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s' with language '%s'", profile.getName(), profile.getLanguage());