import org.sonar.server.plugins.privileged.PrivilegedPluginsStopper;
import org.sonar.server.property.InternalPropertiesImpl;
import org.sonar.server.qualityprofile.QProfileLookup;
-import org.sonar.server.qualityprofile.QProfileProjectOperations;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.CommonRuleDefinitionsImpl;
AnnotationProfileParser.class,
Rules.QProfiles.class,
QProfileLookup.class,
- QProfileProjectOperations.class,
// rule
RuleIndexer.class,
assertThat(picoContainer.getComponentAdapters())
.hasSize(
CONTAINER_ITSELF
- + 78 // level 4
+ + 77 // level 4
+ 4 // content of CeConfigurationModule
+ 3 // content of CeHttpModule
+ 5 // content of CeQueueModule
*/
package org.sonar.server.qualityprofile;
-import javax.annotation.Nullable;
-import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.MyBatis;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.user.UserSession;
+import org.sonar.server.ws.WsUtils;
/**
* Should be refactored in order to use project key. Maybe should it be move to {@link QProfileFactory}
* Permission checks should also be done in the upper service.
*/
@ServerSide
-@ComputeEngineSide
public class QProfileProjectOperations {
private final DbClient db;
+ private final UserSession userSession;
- public QProfileProjectOperations(DbClient db) {
+ public QProfileProjectOperations(DbClient db, UserSession userSession) {
this.db = db;
+ this.userSession = userSession;
}
- public void addProject(String profileKey, String projectUuid, UserSession userSession) {
- DbSession session = db.openSession(false);
- try {
- addProject(profileKey, projectUuid, userSession, session);
- session.commit();
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
+ public void addProject(DbSession dbSession, String profileKey, ComponentDto project) {
+ checkAdminOnProject(project.key());
+ QualityProfileDto qualityProfile = selectProfileByKey(dbSession, profileKey);
- private void addProject(String profileKey, String projectUuid, UserSession userSession, DbSession session) {
- ComponentDto project = db.componentDao().selectOrFailByUuid(session, projectUuid);
- checkPermission(userSession, project.key());
- QualityProfileDto qualityProfile = findNotNull(profileKey, session);
-
- QualityProfileDto currentProfile = db.qualityProfileDao().selectByProjectAndLanguage(session, project.key(), qualityProfile.getLanguage());
+ QualityProfileDto currentProfile = db.qualityProfileDao().selectByProjectAndLanguage(dbSession, project.key(), qualityProfile.getLanguage());
boolean updated = false;
if (currentProfile == null) {
- db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), session);
+ db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), dbSession);
updated = true;
} else if (!profileKey.equals(currentProfile.getKey())) {
- db.qualityProfileDao().updateProjectProfileAssociation(projectUuid, profileKey, currentProfile.getKey(), session);
+ db.qualityProfileDao().updateProjectProfileAssociation(project.uuid(), profileKey, currentProfile.getKey(), dbSession);
updated = true;
}
if (updated) {
- session.commit();
- }
- }
-
- public void removeProject(String profileKey, String projectUuid, UserSession userSession) {
- DbSession session = db.openSession(false);
- try {
- ComponentDto project = db.componentDao().selectOrFailByUuid(session, projectUuid);
- checkPermission(userSession, project.key());
- QualityProfileDto qualityProfile = findNotNull(profileKey, session);
-
- db.qualityProfileDao().deleteProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), session);
- session.commit();
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- void removeProject(String language, long projectId, UserSession userSession) {
- DbSession session = db.openSession(false);
- try {
- ComponentDto project = db.componentDao().selectOrFailById(session, projectId);
- checkPermission(userSession, project.key());
-
- QualityProfileDto associatedProfile = db.qualityProfileDao().selectByProjectAndLanguage(session, project.getKey(), language);
- if (associatedProfile != null) {
- db.qualityProfileDao().deleteProjectProfileAssociation(project.uuid(), associatedProfile.getKey(), session);
- session.commit();
- }
- } finally {
- MyBatis.closeQuietly(session);
+ dbSession.commit();
}
}
- void removeAllProjects(String profileKey, UserSession userSession) {
- checkPermission(userSession);
- DbSession session = db.openSession(false);
- try {
- QualityProfileDto qualityProfile = findNotNull(profileKey, session);
- db.qualityProfileDao().deleteAllProjectProfileAssociation(qualityProfile.getKey(), session);
- session.commit();
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
+ public void removeProject(DbSession dbSession, String profileKey, ComponentDto project) {
+ checkAdminOnProject(project.key());
+ QualityProfileDto qualityProfile = selectProfileByKey(dbSession, profileKey);
- private QualityProfileDto findNotNull(String key, DbSession session) {
- QualityProfileDto qualityProfile = db.qualityProfileDao().selectByKey(session, key);
- checkProfileIsNotNull(qualityProfile);
- return qualityProfile;
+ db.qualityProfileDao().deleteProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), dbSession);
+ dbSession.commit();
}
- private static void checkPermission(UserSession userSession) {
- userSession.checkPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+ private QualityProfileDto selectProfileByKey(DbSession session, String profileKey) {
+ QualityProfileDto qualityProfile = db.qualityProfileDao().selectByKey(session, profileKey);
+ return WsUtils.checkFound(qualityProfile, "Quality profile does not exist");
}
- private static void checkPermission(UserSession userSession, String projectKey) {
+ private void checkAdminOnProject(String projectKey) {
if (!userSession.hasPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN) && !userSession.hasComponentPermission(UserRole.ADMIN, projectKey)) {
throw new ForbiddenException("Insufficient privileges");
}
}
- private static QualityProfileDto checkProfileIsNotNull(@Nullable QualityProfileDto profile) {
- if (profile == null) {
- throw new NotFoundException("This quality profile does not exists.");
- }
- return profile;
- }
-
}
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.NewAction;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.component.ComponentDto;
import org.sonar.server.qualityprofile.QProfileProjectOperations;
-import org.sonar.server.user.UserSession;
import org.sonarqube.ws.client.qualityprofile.AddProjectRequest;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ADD_PROJECT;
private final ProjectAssociationParameters projectAssociationParameters;
private final ProjectAssociationFinder projectAssociationFinder;
private final QProfileProjectOperations profileProjectOperations;
- private final UserSession userSession;
+ private final DbClient dbClient;
public AddProjectAction(ProjectAssociationParameters projectAssociationParameters, QProfileProjectOperations profileProjectOperations,
- ProjectAssociationFinder projectAssociationFinder, UserSession userSession) {
+ ProjectAssociationFinder projectAssociationFinder, DbClient dbClient) {
this.projectAssociationParameters = projectAssociationParameters;
this.profileProjectOperations = profileProjectOperations;
this.projectAssociationFinder = projectAssociationFinder;
- this.userSession = userSession;
+ this.dbClient = dbClient;
}
@Override
@Override
public void handle(Request request, Response response) throws Exception {
AddProjectRequest addProjectRequest = toWsRequest(request);
- String profileKey = projectAssociationFinder.getProfileKey(addProjectRequest.getLanguage(), addProjectRequest.getProfileName(), addProjectRequest.getProfileKey());
- String projectUuid = projectAssociationFinder.getProjectUuid(addProjectRequest.getProjectKey(), addProjectRequest.getProjectUuid());
- profileProjectOperations.addProject(profileKey, projectUuid, userSession);
+
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ String profileKey = projectAssociationFinder.getProfileKey(addProjectRequest.getLanguage(), addProjectRequest.getProfileName(), addProjectRequest.getProfileKey());
+ ComponentDto project = projectAssociationFinder.getProject(dbSession, addProjectRequest.getProjectKey(), addProjectRequest.getProjectUuid());
+ profileProjectOperations.addProject(dbSession, profileKey, project);
+ }
+
response.noContent();
}
package org.sonar.server.qualityprofile.ws;
import javax.annotation.Nullable;
-import org.sonar.server.component.ComponentService;
+import org.sonar.db.DbSession;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.qualityprofile.QProfile;
import org.sonar.server.qualityprofile.QProfileLookup;
public class ProjectAssociationFinder {
private static final String BAD_PROFILE_PARAMETERS_ERROR = "Either profileKey or profileName + language must be set";
- private static final String BAD_PROJECT_PARAMETERS_ERROR = "Either projectKey or projectUuid must be set";
private final QProfileLookup profileLookup;
- private final ComponentService componentService;
+ private final ComponentFinder componentFinder;
- public ProjectAssociationFinder(QProfileLookup profileLookup, ComponentService componentService) {
+ public ProjectAssociationFinder(QProfileLookup profileLookup, ComponentFinder componentFinder) {
this.profileLookup = profileLookup;
- this.componentService = componentService;
+ this.componentFinder = componentFinder;
}
public String getProfileKey(@Nullable String language, @Nullable String profileName, @Nullable String profileKey) {
return profileKey == null ? getProfileKeyFromLanguageAndName(language, profileName) : profileKey;
}
- public String getProjectUuid(@Nullable String projectKey, @Nullable String projectUuid) {
- checkArgument(!isEmpty(projectKey) ^ !isEmpty(projectUuid), BAD_PROJECT_PARAMETERS_ERROR);
- return projectUuid == null ? componentService.getByKey(projectKey).uuid() : projectUuid;
+ public ComponentDto getProject(DbSession dbSession, @Nullable String projectKey, @Nullable String projectUuid) {
+ return componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey, ComponentFinder.ParamNames.PROJECT_UUID_AND_KEY);
}
private String getProfileKeyFromLanguageAndName(String language, String profileName) {
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.NewAction;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.component.ComponentDto;
import org.sonar.server.qualityprofile.QProfileProjectOperations;
-import org.sonar.server.user.UserSession;
import org.sonarqube.ws.client.qualityprofile.RemoveProjectRequest;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_REMOVE_PROJECT;
private final ProjectAssociationParameters projectAssociationParameters;
private final ProjectAssociationFinder projectAssociationFinder;
private final QProfileProjectOperations profileProjectOperations;
- private final UserSession userSession;
+ private final DbClient dbClient;
public RemoveProjectAction(ProjectAssociationParameters projectAssociationParameters, ProjectAssociationFinder projectAssociationFinder,
- QProfileProjectOperations profileProjectOperations, UserSession userSession) {
+ QProfileProjectOperations profileProjectOperations, DbClient dbClient) {
this.projectAssociationParameters = projectAssociationParameters;
this.projectAssociationFinder = projectAssociationFinder;
this.profileProjectOperations = profileProjectOperations;
- this.userSession = userSession;
+ this.dbClient = dbClient;
}
@Override
@Override
public void handle(Request request, Response response) throws Exception {
RemoveProjectRequest removeProjectRequest = toWsRequest(request);
- String profileKey = projectAssociationFinder.getProfileKey(removeProjectRequest.getLanguage(), removeProjectRequest.getProfileName(), removeProjectRequest.getProfileKey());
- String projectUuid = projectAssociationFinder.getProjectUuid(removeProjectRequest.getProjectKey(), removeProjectRequest.getProjectUuid());
- profileProjectOperations.removeProject(profileKey, projectUuid, userSession);
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ String profileKey = projectAssociationFinder.getProfileKey(removeProjectRequest.getLanguage(), removeProjectRequest.getProfileName(), removeProjectRequest.getProfileKey());
+ ComponentDto project = projectAssociationFinder.getProject(dbSession, removeProjectRequest.getProjectKey(), removeProjectRequest.getProjectUuid());
+ profileProjectOperations.removeProject(dbSession, profileKey, project);
+ }
response.noContent();
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.qualityprofile;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.ComponentTesting;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
-import org.sonar.db.permission.UserPermissionDao;
-import org.sonar.db.permission.UserPermissionDto;
-import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.db.user.UserDto;
-import org.sonar.server.organization.DefaultOrganization;
-import org.sonar.server.organization.DefaultOrganizationProvider;
-import org.sonar.server.tester.MockUserSession;
-import org.sonar.server.tester.ServerTester;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.user.UserSession;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class QProfileProjectOperationsMediumTest {
-
- @ClassRule
- public static ServerTester tester = new ServerTester().withEsIndexes();
- @Rule
- public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
-
- DbClient db;
- DbSession dbSession;
- QProfileFactory factory;
- QProfileProjectOperations projectOperations;
- OrganizationDto organization;
- ComponentDto project;
- QualityProfileDto profile;
- static final String PROJECT_KEY = "SonarQube";
- static final String PROJECT_UUID = "ABCD";
-
- UserSession authorizedProfileAdminUserSession = new MockUserSession("john").setName("John").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- UserSession authorizedProjectAdminUserSession = new MockUserSession("john").setName("John").addProjectPermissions(UserRole.ADMIN, PROJECT_KEY);
-
- @Before
- public void before() {
- tester.clearDbAndIndexes();
- db = tester.get(DbClient.class);
- dbSession = db.openSession(false);
- factory = tester.get(QProfileFactory.class);
- projectOperations = tester.get(QProfileProjectOperations.class);
-
- organization = OrganizationTesting.newOrganizationDto();
- db.organizationDao().insert(dbSession, organization);
- project = ComponentTesting.newProjectDto(organization, PROJECT_UUID)
- .setKey(PROJECT_KEY)
- .setName("SonarQube")
- .setLongName("SonarQube");
- db.componentDao().insert(dbSession, project);
-
- profile = QProfileTesting.newXooP1();
- db.qualityProfileDao().insert(dbSession, profile);
-
- dbSession.commit();
- }
-
- @After
- public void after() {
- dbSession.close();
- }
-
- @Test
- public void add_project() {
- projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
-
- assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
- }
-
- @Test
- public void add_project_with_only_project_admin_permission() {
- projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProjectAdminUserSession);
-
- assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
- }
-
- @Test
- public void remove_project_from_project_id() {
- projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
- assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
-
- projectOperations.removeProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
- assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNull();
- }
-
- @Test
- public void remove_project_from_language() {
- projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
- assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
-
- projectOperations.removeProject(profile.getLanguage(), project.getId(), authorizedProfileAdminUserSession);
- assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNull();
- }
-
- @Test
- public void remove_all_projects() {
- ComponentDto project1 = ComponentTesting.newProjectDto(organization, "BCDE")
- .setKey("project1")
- .setName("project1")
- .setLongName("project1");
- ComponentDto project2 = ComponentTesting.newProjectDto(organization, "CDEF")
- .setKey("project2")
- .setName("project2")
- .setLongName("project2");
- db.componentDao().insert(dbSession, project1);
- db.componentDao().insert(dbSession, project2);
-
- // Create a user having user permission on the two projects and the global quality profile admin permission
- UserDto user = new UserDto().setLogin("john").setName("John").setEmail("jo@hn.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis());
- db.userDao().insert(dbSession, user);
- DefaultOrganization defaultOrganization = tester.get(DefaultOrganizationProvider.class).get();
- tester.get(UserPermissionDao.class).insert(dbSession, new UserPermissionDto(defaultOrganization.getUuid(), UserRole.USER, user.getId(), project1.getId()));
- tester.get(UserPermissionDao.class).insert(dbSession, new UserPermissionDto(defaultOrganization.getUuid(), UserRole.USER, user.getId(), project2.getId()));
- UserSession userSession = userSessionRule.login("john").setUserId(user.getId().intValue()).setName("John")
- .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
-
- dbSession.commit();
-
- projectOperations.addProject(profile.getKey(), project1.uuid(), userSession);
- projectOperations.addProject(profile.getKey(), project2.uuid(), userSession);
-
- assertThat(db.qualityProfileDao().selectProjects(profile.getName(), profile.getLanguage()))
- .extracting(ComponentDto::uuid)
- .containsOnly(project1.uuid(), project2.uuid());
-
- projectOperations.removeAllProjects(profile.getKey(), userSession);
- assertThat(db.qualityProfileDao().selectProjects(profile.getName(), profile.getLanguage())).isEmpty();
- }
-}
import org.sonar.db.qualityprofile.QualityProfileDbTester;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.ComponentService;
-import org.sonar.server.component.index.ComponentIndexer;
import org.sonar.server.es.EsTester;
import org.sonar.server.language.LanguageTesting;
import org.sonar.server.measure.index.ProjectMeasuresIndexDefinition;
-import org.sonar.server.measure.index.ProjectMeasuresIndexer;
import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.qualityprofile.QProfileName;
import org.sonar.server.qualityprofile.QProfileProjectOperations;
private ComponentDbTester componentDb = new ComponentDbTester(dbTester);
private QualityProfileDbTester qualityProfileDbTester = new QualityProfileDbTester(dbTester);
- private QProfileProjectOperations qProfileProjectOperations = new QProfileProjectOperations(dbClient);
+ private QProfileProjectOperations qProfileProjectOperations = new QProfileProjectOperations(dbClient, userSession);
private Languages languages = LanguageTesting.newLanguages(LANGUAGE_1, LANGUAGE_2);
private ProjectAssociationParameters projectAssociationParameters = new ProjectAssociationParameters(languages);
private WsActionTester ws = new WsActionTester(new AddProjectAction(projectAssociationParameters,
qProfileProjectOperations, new ProjectAssociationFinder(new QProfileLookup(dbClient),
- new ComponentService(dbClient, null, userSession, null, new ComponentFinder(dbClient), new ProjectMeasuresIndexer(system2, dbClient, es.client()),
- new ComponentIndexer(dbClient, es.client()))),
- userSession));
+ new ComponentFinder(dbClient)),dbClient));
@Before
public void setUp() throws Exception {