use of com.google.gerrit.server.group.InternalGroupDescription in project gerrit by GerritCodeReview.
the class QueryGroups method apply.
@Override
public Response<List<GroupInfo>> apply(TopLevelResource resource) throws BadRequestException, MethodNotAllowedException, PermissionBackendException {
if (Strings.isNullOrEmpty(query)) {
throw new BadRequestException("missing query field");
}
GroupQueryProcessor queryProcessor = queryProcessorProvider.get();
if (queryProcessor.isDisabled()) {
throw new MethodNotAllowedException("query disabled");
}
if (start != 0) {
queryProcessor.setStart(start);
}
if (limit != 0) {
queryProcessor.setUserProvidedLimit(limit);
}
try {
QueryResult<InternalGroup> result = queryProcessor.query(queryBuilder.parse(query));
List<InternalGroup> groups = result.entities();
ArrayList<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groups.size());
json.addOptions(options);
for (InternalGroup group : groups) {
groupInfos.add(json.format(new InternalGroupDescription(group)));
}
if (!groupInfos.isEmpty() && result.more()) {
groupInfos.get(groupInfos.size() - 1)._moreGroups = true;
}
return Response.ok(groupInfos);
} catch (QueryParseException e) {
throw new BadRequestException(e.getMessage());
}
}
use of com.google.gerrit.server.group.InternalGroupDescription in project gerrit by GerritCodeReview.
the class ListMembers method getTransitiveMembers.
public List<AccountInfo> getTransitiveMembers(InternalGroup group) throws PermissionBackendException {
InternalGroupDescription internalGroup = new InternalGroupDescription(group);
GroupControl groupControl = groupControlFactory.controlFor(internalGroup);
return getTransitiveMembers(internalGroup, groupControl);
}
use of com.google.gerrit.server.group.InternalGroupDescription in project gerrit by GerritCodeReview.
the class CreateGroup method apply.
@Override
public Response<GroupInfo> apply(TopLevelResource resource, IdString id, GroupInput input) throws AuthException, BadRequestException, UnprocessableEntityException, ResourceConflictException, IOException, ConfigInvalidException, ResourceNotFoundException, PermissionBackendException {
String name = id.get();
if (input == null) {
input = new GroupInput();
}
if (input.name != null && !name.equals(input.name)) {
throw new BadRequestException("name must match URL");
}
AccountGroup.UUID ownerUuid = owner(input);
CreateGroupArgs args = new CreateGroupArgs();
args.setGroupName(name);
args.uuid = Strings.isNullOrEmpty(input.uuid) ? null : AccountGroup.UUID.parse(input.uuid);
if (args.uuid != null) {
if (!args.uuid.isInternalGroup()) {
throw new BadRequestException(String.format("invalid group UUID '%s'", args.uuid.get()));
}
if (groupCache.get(args.uuid).isPresent()) {
throw new ResourceConflictException(String.format("group with UUID '%s' already exists", args.uuid.get()));
}
}
args.groupDescription = Strings.emptyToNull(input.description);
args.visibleToAll = MoreObjects.firstNonNull(input.visibleToAll, defaultVisibleToAll);
args.ownerGroupUuid = ownerUuid;
if (input.members != null && !input.members.isEmpty()) {
List<Account.Id> members = new ArrayList<>();
for (String nameOrEmailOrId : input.members) {
Account a = addMembers.findAccount(nameOrEmailOrId);
if (!a.isActive()) {
throw new UnprocessableEntityException(String.format("Account Inactive: %s", nameOrEmailOrId));
}
members.add(a.id());
}
args.initialMembers = members;
} else {
args.initialMembers = ownerUuid == null ? Collections.singleton(self.get().getAccountId()) : Collections.emptySet();
}
try {
groupCreationValidationListeners.runEach(l -> l.validateNewGroup(args), ValidationException.class);
} catch (ValidationException e) {
throw new ResourceConflictException(e.getMessage(), e);
}
return Response.created(json.format(new InternalGroupDescription(createGroup(args))));
}
use of com.google.gerrit.server.group.InternalGroupDescription in project gerrit by GerritCodeReview.
the class GroupMembers method getGroupMembers.
private Set<Account> getGroupMembers(InternalGroup group, @Nullable Project.NameKey project, Set<AccountGroup.UUID> seen) throws NoSuchProjectException, IOException {
seen.add(group.getGroupUUID());
GroupControl groupControl = groupControlFactory.controlFor(new InternalGroupDescription(group));
Set<Account> directMembers = group.getMembers().stream().filter(groupControl::canSeeMember).map(accountCache::get).flatMap(Streams::stream).map(AccountState::account).collect(toImmutableSet());
Set<Account> indirectMembers = new HashSet<>();
if (groupControl.canSeeGroup()) {
for (AccountGroup.UUID subgroupUuid : group.getSubgroups()) {
if (!seen.contains(subgroupUuid)) {
indirectMembers.addAll(listAccounts(subgroupUuid, project, seen));
}
}
}
return Sets.union(directMembers, indirectMembers);
}
use of com.google.gerrit.server.group.InternalGroupDescription in project gerrit by GerritCodeReview.
the class GetAuditLog method apply.
@Override
public Response<List<? extends GroupAuditEventInfo>> apply(GroupResource rsrc) throws AuthException, NotInternalGroupException, IOException, ConfigInvalidException, PermissionBackendException {
GroupDescription.Internal group = rsrc.asInternalGroup().orElseThrow(NotInternalGroupException::new);
if (!rsrc.getControl().isOwner()) {
throw new AuthException("Not group owner");
}
AccountLoader accountLoader = accountLoaderFactory.create(true);
List<GroupAuditEventInfo> auditEvents = new ArrayList<>();
try (Repository allUsersRepo = repoManager.openRepository(allUsers)) {
for (AccountGroupMemberAudit auditEvent : groups.getMembersAudit(allUsersRepo, group.getGroupUUID())) {
AccountInfo member = accountLoader.get(auditEvent.memberId());
auditEvents.add(GroupAuditEventInfo.createAddUserEvent(accountLoader.get(auditEvent.addedBy()), auditEvent.addedOn(), member));
if (!auditEvent.isActive()) {
auditEvents.add(GroupAuditEventInfo.createRemoveUserEvent(accountLoader.get(auditEvent.removedBy().orElse(null)), auditEvent.removedOn().orElse(null), member));
}
}
List<AccountGroupByIdAudit> subGroupsAudit = groups.getSubgroupsAudit(allUsersRepo, group.getGroupUUID());
Map<AccountGroup.UUID, InternalGroup> groups = groupCache.get(subGroupsAudit.stream().map(a -> a.includeUuid()).collect(toImmutableList()));
for (AccountGroupByIdAudit auditEvent : subGroupsAudit) {
AccountGroup.UUID includedGroupUUID = auditEvent.includeUuid();
InternalGroup includedGroup = groups.get(includedGroupUUID);
GroupInfo member;
if (includedGroup != null) {
member = groupJson.format(new InternalGroupDescription(includedGroup));
} else {
member = new GroupInfo();
member.id = Url.encode(includedGroupUUID.get());
GroupDescription.Basic groupDescription = groupBackend.get(includedGroupUUID);
if (groupDescription != null) {
member.name = groupDescription.getName();
}
}
auditEvents.add(GroupAuditEventInfo.createAddGroupEvent(accountLoader.get(auditEvent.addedBy()), auditEvent.addedOn(), member));
if (!auditEvent.isActive()) {
auditEvents.add(GroupAuditEventInfo.createRemoveGroupEvent(accountLoader.get(auditEvent.removedBy().orElse(null)), auditEvent.removedOn().orElse(null), member));
}
}
}
accountLoader.fill();
// sort by date and then reverse so that the newest audit event comes first
auditEvents.sort(comparing((GroupAuditEventInfo a) -> a.date).reversed());
return Response.ok(auditEvents);
}
Aggregations