public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
GroupIdOrAnyone group = support.findGroup(dbSession, request);
- Optional<ProjectId> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
checkProjectAdmin(userSession, group.getOrganizationUuid(), projectId);
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
import org.sonar.server.permission.ProjectId;
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
UserId user = support.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
- Optional<ProjectId> projectId = support.findProject(dbSession, request);
+ Optional<ComponentDto> project = support.findProject(dbSession, request);
String organizationKey = request.param(PARAM_ORGANIZATION_KEY);
- checkArgument(!projectId.isPresent() || organizationKey == null, "Organization must not be set when project is set.");
- OrganizationDto org = support.findOrganization(dbSession, organizationKey);
+ checkArgument(!project.isPresent() || organizationKey == null, "Organization must not be set when project is set.");
+ OrganizationDto org = project
+ .map(dto -> dbClient.organizationDao().selectByUuid(dbSession, dto.getOrganizationUuid()))
+ .orElseGet(() -> Optional.ofNullable(support.findOrganization(dbSession, organizationKey)))
+ .orElseThrow(() -> new NotFoundException(String.format("Organization with key '%s' not found", organizationKey)));
+ Optional<ProjectId> projectId = project.map(ProjectId::new);
checkProjectAdmin(userSession, org.getUuid(), projectId);
PermissionChange change = new UserPermissionChange(
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
- Optional<ProjectId> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
checkProjectAdmin(userSession, org.getUuid(), projectId);
PermissionQuery query = buildPermissionQuery(request, projectId);
/**
* @throws org.sonar.server.exceptions.NotFoundException if a project does not exist
*/
- public ProjectId findProject(DbSession dbSession, ProjectWsRef ref) {
+ public ProjectId findProjectId(DbSession dbSession, ProjectWsRef ref) {
ComponentDto project = componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, ref.uuid(), ref.key(), resourceTypes);
return new ProjectId(project.getId(), project.uuid());
}
- public Optional<ProjectId> findProject(DbSession dbSession, Request request) {
+ public Optional<ProjectId> findProjectId(DbSession dbSession, Request request) {
+ return findProject(dbSession, request)
+ .map(ProjectId::new);
+ }
+
+ public Optional<ComponentDto> findProject(DbSession dbSession, Request request) {
String uuid = request.param(PermissionsWsParameters.PARAM_PROJECT_ID);
String key = request.param(PermissionsWsParameters.PARAM_PROJECT_KEY);
if (uuid != null || key != null) {
ProjectWsRef ref = ProjectWsRef.newWsProjectRef(uuid, key);
- return Optional.of(findProject(dbSession, ref));
+ return Optional.of(componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, ref.uuid(), ref.key(), resourceTypes));
}
return Optional.empty();
}
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
GroupIdOrAnyone group = support.findGroup(dbSession, request);
- Optional<ProjectId> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
checkProjectAdmin(userSession, group.getOrganizationUuid(), projectId);
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
UserId user = support.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
- Optional<ProjectId> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
checkProjectAdmin(userSession, org.getUuid(), projectId);
com.google.common.base.Optional<ProjectWsRef> projectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
Optional<ProjectId> projectId;
if (projectRef.isPresent()) {
- projectId = Optional.of(wsSupport.findProject(dbSession, projectRef.get()));
+ projectId = Optional.of(wsSupport.findProjectId(dbSession, projectRef.get()));
} else {
projectId = Optional.empty();
}
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
- Optional<ProjectId> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
checkProjectAdmin(userSession, org.getUuid(), projectId);
PermissionQuery query = buildPermissionQuery(request, projectId);
}
@Test
- public void add_permission_to_user() throws Exception {
+ public void add_permission_to_user_on_default_organization_if_organization_is_not_specified() throws Exception {
loginAsAdmin(db.getDefaultOrganization());
newRequest()
.setParam(PARAM_USER_LOGIN, user.getLogin())
assertThat(db.users().selectGlobalPermissionsOfUser(user, db.getDefaultOrganization())).containsOnly(SYSTEM_ADMIN);
}
+ @Test
+ public void add_permission_to_user_on_specified_organization() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ loginAsAdmin(organization);
+ newRequest()
+ .setParam(PARAM_ORGANIZATION_KEY, organization.getKey())
+ .setParam(PARAM_USER_LOGIN, user.getLogin())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+
+ assertThat(db.users().selectGlobalPermissionsOfUser(user, organization)).containsOnly(SYSTEM_ADMIN);
+ }
+
@Test
public void add_permission_to_project_referenced_by_its_id() throws Exception {
- ComponentDto project = db.components().insertProject();
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertProject(organization);
- loginAsAdmin(db.getDefaultOrganization());
+ loginAsAdmin(organization);
newRequest()
.setParam(PARAM_USER_LOGIN, user.getLogin())
.setParam(PARAM_PROJECT_ID, project.uuid())
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
- assertThat(db.users().selectGlobalPermissionsOfUser(user, db.getDefaultOrganization())).isEmpty();
+ assertThat(db.users().selectGlobalPermissionsOfUser(user, organization)).isEmpty();
assertThat(db.users().selectProjectPermissionsOfUser(user, project)).containsOnly(SYSTEM_ADMIN);
}