use of org.sonar.db.organization.OrganizationDto in project sonarqube by SonarSource.
the class GroupWsSupport method findGroup.
/**
* Finds a user group by its reference. If organization is not defined then group
* is searched in default organization.
*
* @return non-null group
* @throws NotFoundException if the requested group does not exist
* @throws NotFoundException if the requested group is Anyone
*/
public GroupId findGroup(DbSession dbSession, GroupWsRef ref) {
if (ref.hasId()) {
GroupDto group = dbClient.groupDao().selectById(dbSession, ref.getId());
checkFound(group, "No group with id '%s'", ref.getId());
return GroupId.from(group);
}
OrganizationDto org = findOrganizationByKey(dbSession, ref.getOrganizationKey());
Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, org.getUuid(), ref.getName());
checkFoundWithOptional(group, "No group with name '%s' in organization '%s'", ref.getName(), org.getKey());
return GroupId.from(group.get());
}
use of org.sonar.db.organization.OrganizationDto in project sonarqube by SonarSource.
the class GroupWsSupport method findGroupOrAnyone.
public GroupIdOrAnyone findGroupOrAnyone(DbSession dbSession, GroupWsRef ref) {
if (ref.hasId()) {
GroupDto group = dbClient.groupDao().selectById(dbSession, ref.getId());
checkFound(group, "No group with id '%s'", ref.getId());
return GroupIdOrAnyone.from(group);
}
OrganizationDto org = findOrganizationByKey(dbSession, ref.getOrganizationKey());
if (ref.isAnyone()) {
return GroupIdOrAnyone.forAnyone(org.getUuid());
}
Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, org.getUuid(), ref.getName());
checkFoundWithOptional(group, "No group with name '%s' in organization '%s'", ref.getName(), org.getKey());
return GroupIdOrAnyone.from(group.get());
}
use of org.sonar.db.organization.OrganizationDto in project sonarqube by SonarSource.
the class SearchAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
int page = request.mandatoryParamAsInt(Param.PAGE);
int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
SearchOptions options = new SearchOptions().setPage(page, pageSize);
String query = defaultIfBlank(request.param(Param.TEXT_QUERY), "");
Set<String> fields = neededFields(request);
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto organization = groupWsSupport.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY));
userSession.checkLoggedIn().checkPermission(ADMINISTER, organization);
int limit = dbClient.groupDao().countByQuery(dbSession, organization.getUuid(), query);
List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, organization.getUuid(), query, options.getOffset(), pageSize);
List<Integer> groupIds = groups.stream().map(GroupDto::getId).collect(Collectors.toList(groups.size()));
Map<String, Integer> userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupIds);
JsonWriter json = response.newJsonWriter().beginObject();
options.writeJson(json, limit);
writeGroups(json, groups, userCountByGroup, fields);
json.endObject().close();
}
}
use of org.sonar.db.organization.OrganizationDto in project sonarqube by SonarSource.
the class UpdateAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
int groupId = request.mandatoryParamAsInt(PARAM_GROUP_ID);
GroupDto group = dbClient.groupDao().selectById(dbSession, groupId);
checkFound(group, "Could not find a user group with id '%s'.", groupId);
Optional<OrganizationDto> org = dbClient.organizationDao().selectByUuid(dbSession, group.getOrganizationUuid());
checkFoundWithOptional(org, "Could not find organization with id '%s'.", group.getOrganizationUuid());
userSession.checkPermission(ADMINISTER, org.get());
boolean changed = false;
String newName = request.param(PARAM_GROUP_NAME);
if (newName != null) {
changed = true;
UserGroupValidation.validateGroupName(newName);
support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), newName);
String oldName = group.getName();
group.setName(newName);
updateDefaultGroupIfNeeded(dbSession, org.get(), oldName, newName);
}
String description = request.param(PARAM_GROUP_DESCRIPTION);
if (description != null) {
changed = true;
group.setDescription(support.validateDescription(description));
}
if (changed) {
dbClient.groupDao().update(dbSession, group);
dbSession.commit();
}
writeResponse(dbSession, request, response, org.get(), group);
}
}
use of org.sonar.db.organization.OrganizationDto in project sonarqube by SonarSource.
the class CreateAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto organization = support.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY));
userSession.checkPermission(ADMINISTER, organization);
GroupDto group = new GroupDto().setOrganizationUuid(organization.getUuid()).setName(request.mandatoryParam(PARAM_GROUP_NAME)).setDescription(request.param(PARAM_GROUP_DESCRIPTION));
// validations
UserGroupValidation.validateGroupName(group.getName());
support.validateDescription(group.getDescription());
support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), group.getName());
dbClient.groupDao().insert(dbSession, group);
dbSession.commit();
writeResponse(request, response, organization, group);
}
}
Aggregations