ProjectDto project = loadProject(dbSession, request);
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromName(request));
- checkPermissions(dbSession, profile, project);
+ checkPermissions(profile, project);
QProfileDto currentProfile = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(dbSession, project, profile.getLanguage());
QProfileDto deactivatedProfile = null;
response.noContent();
}
-
private ProjectDto loadProject(DbSession dbSession, Request request) {
String projectKey = request.mandatoryParam(PARAM_PROJECT);
return componentFinder.getProjectByKey(dbSession, projectKey);
}
- private void checkPermissions(DbSession dbSession, QProfileDto profile, ProjectDto project) {
- if (wsSupport.canEdit(dbSession, profile)
- || userSession.hasProjectPermission(UserRole.ADMIN, project)) {
+ private void checkPermissions(QProfileDto profile, ProjectDto project) {
+ if (wsSupport.canAdministrate(profile) || userSession.hasProjectPermission(UserRole.ADMIN, project)) {
return;
}
try (DbSession dbSession = dbClient.openSession(false)) {
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromName(request));
- wsSupport.checkCanEdit(dbSession, profile);
+ wsSupport.checkCanAdministrate(profile);
Collection<QProfileDto> descendants = selectDescendants(dbSession, profile);
ensureNoneIsMarkedAsDefault(dbSession, profile, descendants);
}
boolean canEdit(DbSession dbSession, QProfileDto profile) {
- if (profile.isBuiltIn() || !userSession.isLoggedIn()) {
- return false;
- }
- if (userSession.hasPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES)) {
+ if (canAdministrate(profile)) {
return true;
}
-
UserDto user = dbClient.userDao().selectByLogin(dbSession, userSession.getLogin());
checkState(user != null, "User from session does not exist");
return dbClient.qProfileEditUsersDao().exists(dbSession, profile, user)
|| dbClient.qProfileEditGroupsDao().exists(dbSession, profile, userSession.getGroups());
}
+ boolean canAdministrate(QProfileDto profile) {
+ if (profile.isBuiltIn() || !userSession.isLoggedIn()) {
+ return false;
+ }
+ return userSession.hasPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
+ }
+
public void checkCanEdit(DbSession dbSession, QProfileDto profile) {
checkNotBuiltIn(profile);
if (!canEdit(dbSession, profile)) {
}
}
+ public void checkCanAdministrate(QProfileDto profile) {
+ checkNotBuiltIn(profile);
+ if (!canAdministrate(profile)) {
+ throw insufficientPrivilegesException();
+ }
+ }
+
void checkNotBuiltIn(QProfileDto profile) {
checkRequest(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s' with language '%s'", profile.getName(), profile.getLanguage());
}
try (DbSession dbSession = dbClient.openSession(false)) {
ProjectDto project = loadProject(dbSession, request);
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromName(request));
- checkPermissions(dbSession, profile, project);
+ checkPermissions(profile, project);
dbClient.qualityProfileDao().deleteProjectProfileAssociation(dbSession, project, profile);
dbSession.commit();
return componentFinder.getProjectByKey(dbSession, projectKey);
}
- private void checkPermissions(DbSession dbSession, QProfileDto profile, ProjectDto project) {
- if (wsSupport.canEdit(dbSession, profile) || userSession.hasProjectPermission(UserRole.ADMIN, project)) {
+ private void checkPermissions(QProfileDto profile, ProjectDto project) {
+ if (wsSupport.canAdministrate(profile) || userSession.hasProjectPermission(UserRole.ADMIN, project)) {
return;
}
.setEdit(!profile.isBuiltIn() && (isGlobalQProfileAdmin || data.isEditable(profile)))
.setSetAsDefault(!isDefault && isGlobalQProfileAdmin)
.setCopy(isGlobalQProfileAdmin)
- .setDelete(!isDefault && !profile.isBuiltIn() && (isGlobalQProfileAdmin || data.isEditable(profile)))
- .setAssociateProjects(!isDefault && (isGlobalQProfileAdmin || data.isEditable(profile))));
+ .setDelete(!isDefault && !profile.isBuiltIn() && isGlobalQProfileAdmin)
+ .setAssociateProjects(!isDefault && isGlobalQProfileAdmin));
}
return response.build();
}
"edit": true,
"setAsDefault": false,
"copy": false,
- "delete": true,
- "associateProjects": true
+ "delete": false,
+ "associateProjects": false
}
},
{
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.permission.GlobalPermission;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.user.UserDto;
}
@Test
- public void as_qprofile_editor() {
+ public void as_qprofile_editor_and_global_admin() {
UserDto user = db.users().insertUser();
QProfileDto qualityProfile = db.qualityProfiles().insert(qp -> qp.setLanguage(LANGUAGE_1));
db.qualityProfiles().addUserPermission(qualityProfile, user);
ProjectDto project = db.components().insertPrivateProjectDto();
- userSession.logIn(user);
+ userSession.logIn(user).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
call(project, qualityProfile);
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.permission.GlobalPermission;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.user.UserDto;
}
@Test
- public void as_qprofile_editor() {
+ public void as_qprofile_editor_and_global_admin() {
QProfileDto profile = createProfile();
UserDto user = db.users().insertUser();
db.qualityProfiles().addUserPermission(profile, user);
- userSession.logIn(user);
+ userSession.logIn(user).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
TestResponse response = ws.newRequest()
.setMethod("POST")
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ResourceTypesRule;
+import org.sonar.db.permission.GlobalPermission;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.user.UserDto;
}
@Test
- public void as_qprofile_editor() {
+ public void as_qprofile_editor_and_global_admin() {
ProjectDto project = db.components().insertPrivateProjectDto();
QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_1));
db.qualityProfiles().associateWithProject(project, profile);
UserDto user = db.users().insertUser();
db.qualityProfiles().addUserPermission(profile, user);
- userSession.logIn(user);
+ userSession.logIn(user).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
call(project, profile);
verify(qualityProfileChangeEventService).publishRuleActivationToSonarLintClients(project, null, profile);
}
+ @Test
+ public void as_qprofile_editor_fail_if_not_project_nor_global_admin() {
+ ProjectDto project = db.components().insertPrivateProjectDto();
+ QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_1));
+ db.qualityProfiles().associateWithProject(project, profile);
+ UserDto user = db.users().insertUser();
+ db.qualityProfiles().addUserPermission(profile, user);
+ userSession.logIn(user);
+
+ assertThatThrownBy(() -> call(project, profile))
+ .isInstanceOf(ForbiddenException.class)
+ .hasMessage("Insufficient privileges");
+ }
+
@Test
public void fail_if_not_enough_permissions() {
userSession.logIn(db.users().insertUser());
.extracting(QualityProfile::getKey, qp -> qp.getActions().getEdit(), qp -> qp.getActions().getCopy(), qp -> qp.getActions().getSetAsDefault(),
qp -> qp.getActions().getDelete(), qp -> qp.getActions().getAssociateProjects())
.containsExactlyInAnyOrder(
- tuple(profile1.getKee(), true, false, false, true, true),
+ tuple(profile1.getKee(), true, false, false, false, false),
tuple(profile2.getKee(), false, false, false, false, false),
- tuple(profile3.getKee(), true, false, false, true, true),
+ tuple(profile3.getKee(), true, false, false, false, false),
tuple(builtInProfile.getKee(), false, false, false, false, false));
assertThat(result.getActions().getCreate()).isFalse();
}