Search in sources :

Example 1 with InternalGroupDescription

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());
    }
}
Also used : InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) GroupQueryProcessor(com.google.gerrit.server.query.group.GroupQueryProcessor) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) InternalGroup(com.google.gerrit.entities.InternalGroup) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 2 with InternalGroupDescription

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);
}
Also used : GroupControl(com.google.gerrit.server.account.GroupControl) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription)

Example 3 with InternalGroupDescription

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))));
}
Also used : GroupInput(com.google.gerrit.extensions.api.groups.GroupInput) Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) ValidationException(com.google.gerrit.server.validators.ValidationException) ArrayList(java.util.ArrayList) IdString(com.google.gerrit.extensions.restapi.IdString) CreateGroupArgs(com.google.gerrit.server.account.CreateGroupArgs) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountGroup(com.google.gerrit.entities.AccountGroup) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 4 with InternalGroupDescription

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);
}
Also used : Account(com.google.gerrit.entities.Account) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) AccountGroup(com.google.gerrit.entities.AccountGroup) Streams(com.google.common.collect.Streams) HashSet(java.util.HashSet)

Example 5 with InternalGroupDescription

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);
}
Also used : InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) ArrayList(java.util.ArrayList) AccountGroupByIdAudit(com.google.gerrit.entities.AccountGroupByIdAudit) AuthException(com.google.gerrit.extensions.restapi.AuthException) InternalGroup(com.google.gerrit.entities.InternalGroup) GroupDescription(com.google.gerrit.entities.GroupDescription) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) Repository(org.eclipse.jgit.lib.Repository) GroupAuditEventInfo(com.google.gerrit.extensions.common.GroupAuditEventInfo) AccountGroup(com.google.gerrit.entities.AccountGroup) AccountLoader(com.google.gerrit.server.account.AccountLoader) AccountGroupMemberAudit(com.google.gerrit.entities.AccountGroupMemberAudit) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Aggregations

InternalGroupDescription (com.google.gerrit.server.group.InternalGroupDescription)6 AccountGroup (com.google.gerrit.entities.AccountGroup)4 InternalGroup (com.google.gerrit.entities.InternalGroup)3 Account (com.google.gerrit.entities.Account)2 GroupDescription (com.google.gerrit.entities.GroupDescription)2 GroupInfo (com.google.gerrit.extensions.common.GroupInfo)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ArrayList (java.util.ArrayList)2 Streams (com.google.common.collect.Streams)1 AccountGroupByIdAudit (com.google.gerrit.entities.AccountGroupByIdAudit)1 AccountGroupMemberAudit (com.google.gerrit.entities.AccountGroupMemberAudit)1 GroupReference (com.google.gerrit.entities.GroupReference)1 GroupInput (com.google.gerrit.extensions.api.groups.GroupInput)1 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)1 GroupAuditEventInfo (com.google.gerrit.extensions.common.GroupAuditEventInfo)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)1