Search in sources :

Example 6 with AccountGroupById

use of com.google.gerrit.reviewdb.client.AccountGroupById in project gerrit by GerritCodeReview.

the class DeleteIncludedGroups method apply.

@Override
public Response<?> apply(GroupResource resource, Input input) throws AuthException, MethodNotAllowedException, UnprocessableEntityException, OrmException {
    AccountGroup internalGroup = resource.toAccountGroup();
    if (internalGroup == null) {
        throw new MethodNotAllowedException();
    }
    input = Input.init(input);
    final GroupControl control = resource.getControl();
    final Map<AccountGroup.UUID, AccountGroupById> includedGroups = getIncludedGroups(internalGroup.getId());
    final List<AccountGroupById> toRemove = new ArrayList<>();
    for (final String includedGroup : input.groups) {
        GroupDescription.Basic d = groupsCollection.parse(includedGroup);
        if (!control.canRemoveGroup()) {
            throw new AuthException(String.format("Cannot delete group: %s", d.getName()));
        }
        AccountGroupById g = includedGroups.remove(d.getGroupUUID());
        if (g != null) {
            toRemove.add(g);
        }
    }
    if (!toRemove.isEmpty()) {
        writeAudits(toRemove);
        db.get().accountGroupById().delete(toRemove);
        for (final AccountGroupById g : toRemove) {
            groupIncludeCache.evictParentGroupsOf(g.getIncludeUUID());
        }
        groupIncludeCache.evictSubgroupsOf(internalGroup.getGroupUUID());
    }
    return Response.none();
}
Also used : GroupControl(com.google.gerrit.server.account.GroupControl) GroupDescription(com.google.gerrit.common.data.GroupDescription) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) AccountGroupById(com.google.gerrit.reviewdb.client.AccountGroupById)

Example 7 with AccountGroupById

use of com.google.gerrit.reviewdb.client.AccountGroupById in project gerrit by GerritCodeReview.

the class AddIncludedGroups method apply.

@Override
public List<GroupInfo> apply(GroupResource resource, Input input) throws MethodNotAllowedException, AuthException, UnprocessableEntityException, OrmException {
    AccountGroup group = resource.toAccountGroup();
    if (group == null) {
        throw new MethodNotAllowedException();
    }
    input = Input.init(input);
    GroupControl control = resource.getControl();
    Map<AccountGroup.UUID, AccountGroupById> newIncludedGroups = new HashMap<>();
    List<GroupInfo> result = new ArrayList<>();
    Account.Id me = control.getUser().getAccountId();
    for (String includedGroup : input.groups) {
        GroupDescription.Basic d = groupsCollection.parse(includedGroup);
        if (!control.canAddGroup()) {
            throw new AuthException(String.format("Cannot add group: %s", d.getName()));
        }
        if (!newIncludedGroups.containsKey(d.getGroupUUID())) {
            AccountGroupById.Key agiKey = new AccountGroupById.Key(group.getId(), d.getGroupUUID());
            AccountGroupById agi = db.get().accountGroupById().get(agiKey);
            if (agi == null) {
                agi = new AccountGroupById(agiKey);
                newIncludedGroups.put(d.getGroupUUID(), agi);
            }
        }
        result.add(json.format(d));
    }
    if (!newIncludedGroups.isEmpty()) {
        auditService.dispatchAddGroupsToGroup(me, newIncludedGroups.values());
        db.get().accountGroupById().insert(newIncludedGroups.values());
        for (AccountGroupById agi : newIncludedGroups.values()) {
            groupIncludeCache.evictParentGroupsOf(agi.getIncludeUUID());
        }
        groupIncludeCache.evictSubgroupsOf(group.getGroupUUID());
    }
    return result;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) HashMap(java.util.HashMap) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) GroupControl(com.google.gerrit.server.account.GroupControl) GroupDescription(com.google.gerrit.common.data.GroupDescription) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AccountGroupById(com.google.gerrit.reviewdb.client.AccountGroupById)

Example 8 with AccountGroupById

use of com.google.gerrit.reviewdb.client.AccountGroupById in project gerrit by GerritCodeReview.

the class ListMembers method getMembers.

private Map<Account.Id, AccountInfo> getMembers(final AccountGroup.UUID groupUUID, final HashSet<AccountGroup.UUID> seenGroups) throws OrmException {
    seenGroups.add(groupUUID);
    final Map<Account.Id, AccountInfo> members = new HashMap<>();
    final AccountGroup group = groupCache.get(groupUUID);
    if (group == null) {
        // the included group is an external group and can't be resolved
        return Collections.emptyMap();
    }
    final GroupDetail groupDetail;
    try {
        groupDetail = groupDetailFactory.create(group.getId()).call();
    } catch (NoSuchGroupException e) {
        // the included group is not visible
        return Collections.emptyMap();
    }
    if (groupDetail.members != null) {
        for (final AccountGroupMember m : groupDetail.members) {
            if (!members.containsKey(m.getAccountId())) {
                members.put(m.getAccountId(), accountLoader.get(m.getAccountId()));
            }
        }
    }
    if (recursive) {
        if (groupDetail.includes != null) {
            for (final AccountGroupById includedGroup : groupDetail.includes) {
                if (!seenGroups.contains(includedGroup.getIncludeUUID())) {
                    members.putAll(getMembers(includedGroup.getIncludeUUID(), seenGroups));
                }
            }
        }
    }
    accountLoader.fill();
    return members;
}
Also used : GroupDetail(com.google.gerrit.common.data.GroupDetail) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) HashMap(java.util.HashMap) AccountGroupById(com.google.gerrit.reviewdb.client.AccountGroupById) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) NoSuchGroupException(com.google.gerrit.common.errors.NoSuchGroupException) AccountGroupById(com.google.gerrit.reviewdb.client.AccountGroupById)

Aggregations

AccountGroupById (com.google.gerrit.reviewdb.client.AccountGroupById)8 ArrayList (java.util.ArrayList)6 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)5 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)3 GroupControl (com.google.gerrit.server.account.GroupControl)3 GroupDescription (com.google.gerrit.common.data.GroupDescription)2 GroupDetail (com.google.gerrit.common.data.GroupDetail)2 NoSuchGroupException (com.google.gerrit.common.errors.NoSuchGroupException)2 GroupInfo (com.google.gerrit.extensions.common.GroupInfo)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 Account (com.google.gerrit.reviewdb.client.Account)2 AccountGroupByIdAud (com.google.gerrit.reviewdb.client.AccountGroupByIdAud)2 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 OrmException (com.google.gwtorm.server.OrmException)2 HashMap (java.util.HashMap)2 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)1 HashSet (java.util.HashSet)1