throw new BadRequestException(format("Key '%s' belongs to a component which is not a Project", query.getModuleKey()));
}
- boolean hasScanPerm = userSession.hasComponentUuidPermission(SCAN_EXECUTION, module.projectUuid());
- boolean hasBrowsePerm = userSession.hasComponentUuidPermission(USER, module.projectUuid());
+ boolean hasScanPerm = userSession.hasComponentPermission(SCAN_EXECUTION, module);
+ boolean hasBrowsePerm = userSession.hasComponentPermission(USER, module);
checkPermission(query.isIssuesMode(), hasScanPerm, hasBrowsePerm);
ComponentDto project = getProject(module, session);
}
private static void appendPermissions(JsonWriter json, ComponentDto component, UserSession userSession) {
- boolean hasBrowsePermission = userSession.hasComponentUuidPermission(UserRole.USER, component.uuid());
+ boolean hasBrowsePermission = userSession.hasComponentPermission(UserRole.USER, component);
json.prop("canMarkAsFavourite", userSession.isLoggedIn() && hasBrowsePermission);
}
import org.sonarqube.ws.WsComponents.ShowWsResponse;
import org.sonarqube.ws.client.component.ShowWsRequest;
-import static com.google.common.base.MoreObjects.firstNonNull;
import static java.lang.String.format;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonar.server.component.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
private ComponentDto getComponentByUuidOrKey(DbSession dbSession, ShowWsRequest request) {
ComponentDto component = componentFinder.getByUuidOrKey(dbSession, request.getId(), request.getKey(), ParamNames.ID_AND_KEY);
- String projectUuid = firstNonNull(component.projectUuid(), component.uuid());
- userSession.checkComponentUuidPermission(UserRole.USER, projectUuid);
+ userSession.checkComponentPermission(UserRole.USER, component);
return component;
}
}
import org.sonarqube.ws.WsComponents.TreeWsResponse;
import org.sonarqube.ws.client.component.TreeWsRequest;
-import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.Sets.newHashSet;
import static java.lang.String.CASE_INSENSITIVE_ORDER;
}
private void checkPermissions(ComponentDto baseComponent) {
- String projectUuid = firstNonNull(baseComponent.projectUuid(), baseComponent.uuid());
- userSession.checkComponentUuidPermission(UserRole.USER, projectUuid);
+ userSession.checkComponentPermission(UserRole.USER, baseComponent);
}
private static TreeWsResponse buildResponse(ComponentDto baseComponent, OrganizationDto organizationDto, List<ComponentDto> components,
import org.sonarqube.ws.WsMeasures.ComponentWsResponse;
import org.sonarqube.ws.client.measure.ComponentWsRequest;
-import static com.google.common.base.MoreObjects.firstNonNull;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
}
private void checkPermissions(ComponentDto baseComponent) {
- String projectUuid = firstNonNull(baseComponent.projectUuid(), baseComponent.uuid());
- userSession.checkComponentUuidPermission(UserRole.USER, projectUuid);
+ userSession.checkComponentPermission(UserRole.USER, baseComponent);
}
}
import org.sonarqube.ws.WsMeasures;
import org.sonarqube.ws.client.measure.ComponentTreeWsRequest;
-import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Sets.newHashSet;
import static java.lang.String.format;
}
private void checkPermissions(ComponentDto baseComponent) {
- String projectUuid = firstNonNull(baseComponent.projectUuid(), baseComponent.uuid());
- userSession.checkComponentUuidPermission(UserRole.USER, projectUuid);
+ userSession.checkComponentPermission(UserRole.USER, baseComponent);
}
private enum IsFileComponent implements Predicate<ComponentDto> {
request.getProjectKey(),
ComponentFinder.ParamNames.PROJECT_ID_AND_KEY);
- if (!userSession.hasComponentUuidPermission(UserRole.ADMIN, component.uuid()) &&
- !userSession.hasComponentUuidPermission(UserRole.USER, component.uuid())) {
+ if (!userSession.hasComponentPermission(UserRole.ADMIN, component) &&
+ !userSession.hasComponentPermission(UserRole.USER, component)) {
throw insufficientPrivilegesException();
}
}
private boolean hasAdminPermission(Optional<ComponentDto> component) {
- return component.isPresent() ? userSession.hasComponentUuidPermission(ADMIN, component.get().uuid()) : userSession.hasPermission(GlobalPermissions.SYSTEM_ADMIN);
+ return component.isPresent() ? userSession.hasComponentPermission(ADMIN, component.get()) : userSession.hasPermission(GlobalPermissions.SYSTEM_ADMIN);
}
private List<PropertyDto> loadGlobalSettings(DbSession dbSession, Optional<String> key) {
private void checkPermission(Long projectId, DbSession session) {
ComponentDto project = componentDao.selectOrFailById(session, projectId);
if (!userSession.hasPermission(GlobalPermissions.QUALITY_GATE_ADMIN)
- && !userSession.hasComponentUuidPermission(UserRole.ADMIN, project.uuid())) {
+ && !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
throw new ForbiddenException("Insufficient privileges");
}
}
private ComponentDto getProject(DbSession dbSession, String projectUuid, String projectKey) {
ComponentDto project = componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey, ParamNames.PROJECT_ID_AND_KEY);
- if (!userSession.hasComponentUuidPermission(UserRole.USER, projectUuid) &&
- !userSession.hasComponentUuidPermission(UserRole.ADMIN, projectUuid)) {
+ if (!userSession.hasComponentPermission(UserRole.USER, project) &&
+ !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
throw insufficientPrivilegesException();
}
DbSession dbSession = dbClient.openSession(false);
try {
ProjectAndSnapshot projectAndSnapshot = getProjectAndSnapshot(dbSession, request);
- checkPermission(projectAndSnapshot.project.uuid());
+ checkPermission(projectAndSnapshot.project);
Optional<String> measureData = getQualityGateDetailsMeasureData(dbSession, projectAndSnapshot.project);
return ProjectStatusWsResponse.newBuilder()
return projectStatusWsRequest;
}
- private void checkPermission(String projectUuid) {
- if (!userSession.hasComponentUuidPermission(UserRole.ADMIN, projectUuid) &&
- !userSession.hasComponentUuidPermission(UserRole.USER, projectUuid)) {
+ private void checkPermission(ComponentDto project) {
+ if (!userSession.hasComponentPermission(UserRole.ADMIN, project) &&
+ !userSession.hasComponentPermission(UserRole.USER, project)) {
throw insufficientPrivilegesException();
}
}
.or(() -> componentFinder.getByUuidOrKey(dbSession, projectId, projectKey, ParamNames.PROJECT_ID_AND_KEY));
if (!userSession.hasPermission(GlobalPermissions.QUALITY_GATE_ADMIN) &&
- !userSession.hasComponentUuidPermission(UserRole.ADMIN, project.uuid())) {
+ !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
throw insufficientPrivilegesException();
}
}
private boolean hasPermission(String permission, Optional<ComponentDto> component) {
- return userSession.hasPermission(permission) || (component.isPresent() && userSession.hasComponentUuidPermission(permission, component.get().uuid()));
+ return userSession.hasPermission(permission) || (component.isPresent() && userSession.hasComponentPermission(permission, component.get()));
}
}
String componentKey = request.mandatoryParam(PARAM_COMPONENT_KEY);
try (DbSession session = dbClient.openSession(false)) {
ComponentDto component = componentFinder.getByKey(session, componentKey);
- if (!(userSession.hasComponentUuidPermission(USER, component.projectUuid()) || userSession.hasComponentUuidPermission(ADMIN, component.projectUuid()))) {
+ if (!(userSession.hasComponentPermission(USER, component) || userSession.hasComponentPermission(ADMIN, component))) {
throw new ForbiddenException("Insufficient privileges");
}
OrganizationDto organizationDto = componentFinder.getOrganization(session, component);
writeComponent(json, session, component, organizationDto, analysis.orElse(null));
writeProfiles(json, session, component);
writeQualityGate(json, session, component);
- if (userSession.hasComponentUuidPermission(ADMIN, component.projectUuid()) || userSession.hasPermission(QUALITY_PROFILE_ADMIN)) {
+ if (userSession.hasComponentPermission(ADMIN, component) || userSession.hasPermission(QUALITY_PROFILE_ADMIN)) {
writeConfiguration(json, component);
}
writeBreadCrumbs(json, session, component);
json.name("extensions").beginArray();
Predicate<Page> isAuthorized = page -> {
String requiredPermission = page.isAdmin() ? UserRole.ADMIN : UserRole.USER;
- return userSession.hasComponentUuidPermission(requiredPermission, component.uuid());
+ return userSession.hasComponentPermission(requiredPermission, component);
};
pages.stream()
.filter(isAuthorized)
}
private void writeConfiguration(JsonWriter json, ComponentDto component) {
- boolean isAdmin = userSession.hasComponentUuidPermission(ADMIN, component.projectUuid());
+ boolean isAdmin = userSession.hasComponentPermission(ADMIN, component);
json.name("configuration").beginObject();
writeConfigPageAccess(json, isAdmin, component);
public ExpectedException expectedException = ExpectedException.none();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- ComponentDbTester componentDb = new ComponentDbTester(db);
- DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ private ComponentDbTester componentDb = new ComponentDbTester(db);
+ private DbClient dbClient = db.getDbClient();
+ private DbSession dbSession = db.getSession();
private WsActionTester ws = new WsActionTester(
new GetByProjectAction(userSession, dbClient, new ComponentFinder(dbClient), new QualityGateFinder(dbClient)));
@Test
public void empty_response() {
- ComponentDto project = componentDb.insertComponent(newProjectDto(db.getDefaultOrganization()));
+ ComponentDto project = componentDb.insertProject();
insertQualityGate("Another QG");
String result = ws.newRequest().setParam(PARAM_PROJECT_ID, project.uuid()).execute().getInput();
@Test
public void get_with_project_admin_permission() {
- ComponentDto project = componentDb.insertComponent(newProjectDto(db.organizations().insert()));
- userSession.anonymous().addProjectUuidPermissions(UserRole.USER, project.uuid());
+ ComponentDto project = componentDb.insertProject();
+ userSession.login().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
QualityGateDto dbQualityGate = insertQualityGate("Sonar way");
setDefaultQualityGate(dbQualityGate.getId());
}
@Test
- public void get_with_project_browse_permission() {
- ComponentDto project = componentDb.insertComponent(newProjectDto(db.getDefaultOrganization()));
- userSession.anonymous().addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+ public void get_with_project_user_permission() {
+ ComponentDto project = componentDb.insertProject();
+ userSession.login().addProjectUuidPermissions(UserRole.USER, project.uuid());
QualityGateDto dbQualityGate = insertQualityGate("Sonar way");
setDefaultQualityGate(dbQualityGate.getId());