Search in sources :

Example 16 with GroupControl

use of com.google.gerrit.server.account.GroupControl in project gerrit by GerritCodeReview.

the class AddSubgroups method apply.

@Override
public Response<List<GroupInfo>> apply(GroupResource resource, Input input) throws NotInternalGroupException, AuthException, UnprocessableEntityException, ResourceNotFoundException, IOException, ConfigInvalidException, PermissionBackendException {
    GroupDescription.Internal group = resource.asInternalGroup().orElseThrow(NotInternalGroupException::new);
    input = Input.init(input);
    GroupControl control = resource.getControl();
    if (!control.canAddGroup()) {
        throw new AuthException(String.format("Cannot add groups to group %s", group.getName()));
    }
    List<GroupInfo> result = new ArrayList<>();
    Set<AccountGroup.UUID> subgroupUuids = new LinkedHashSet<>();
    for (String subgroupIdentifier : input.groups) {
        GroupDescription.Basic subgroup = groupResolver.parse(subgroupIdentifier);
        subgroupUuids.add(subgroup.getGroupUUID());
        result.add(json.format(subgroup));
    }
    AccountGroup.UUID groupUuid = group.getGroupUUID();
    try {
        addSubgroups(groupUuid, subgroupUuids);
    } catch (NoSuchGroupException e) {
        throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
    }
    return Response.ok(result);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) IdString(com.google.gerrit.extensions.restapi.IdString) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) GroupDescription(com.google.gerrit.entities.GroupDescription) GroupControl(com.google.gerrit.server.account.GroupControl) AccountGroup(com.google.gerrit.entities.AccountGroup) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 17 with GroupControl

use of com.google.gerrit.server.account.GroupControl in project gerrit by GerritCodeReview.

the class DeleteMembers method apply.

@Override
public Response<?> apply(GroupResource resource, Input input) throws AuthException, NotInternalGroupException, UnprocessableEntityException, IOException, ConfigInvalidException, ResourceNotFoundException {
    GroupDescription.Internal internalGroup = resource.asInternalGroup().orElseThrow(NotInternalGroupException::new);
    input = Input.init(input);
    final GroupControl control = resource.getControl();
    if (!control.canRemoveMember()) {
        throw new AuthException("Cannot delete members from group " + internalGroup.getName());
    }
    Set<Account.Id> membersToRemove = new HashSet<>();
    for (String nameOrEmail : input.members) {
        if (Strings.isNullOrEmpty(nameOrEmail)) {
            continue;
        }
        membersToRemove.add(accountResolver.resolve(nameOrEmail).asUnique().account().id());
    }
    AccountGroup.UUID groupUuid = internalGroup.getGroupUUID();
    try {
        removeGroupMembers(groupUuid, membersToRemove);
    } catch (NoSuchGroupException e) {
        throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
    }
    return Response.none();
}
Also used : AuthException(com.google.gerrit.extensions.restapi.AuthException) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) GroupDescription(com.google.gerrit.entities.GroupDescription) GroupControl(com.google.gerrit.server.account.GroupControl) AccountGroup(com.google.gerrit.entities.AccountGroup) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) HashSet(java.util.HashSet)

Example 18 with GroupControl

use of com.google.gerrit.server.account.GroupControl in project gerrit by GerritCodeReview.

the class GroupsCollection method parse.

@Override
public GroupResource parse(TopLevelResource parent, IdString id) throws AuthException, ResourceNotFoundException {
    final CurrentUser user = self.get();
    if (user instanceof AnonymousUser) {
        throw new AuthException("Authentication required");
    } else if (!(user.isIdentifiedUser() || user.isInternalUser())) {
        throw new ResourceNotFoundException(id);
    }
    GroupDescription.Basic group = groupResolver.parseId(id.get());
    if (group == null) {
        throw new ResourceNotFoundException(id.get());
    }
    GroupControl ctl = groupControlFactory.controlFor(group);
    if (!ctl.isVisible()) {
        throw new ResourceNotFoundException(id);
    }
    return new GroupResource(ctl);
}
Also used : GroupDescription(com.google.gerrit.entities.GroupDescription) GroupControl(com.google.gerrit.server.account.GroupControl) CurrentUser(com.google.gerrit.server.CurrentUser) AuthException(com.google.gerrit.extensions.restapi.AuthException) AnonymousUser(com.google.gerrit.server.AnonymousUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) GroupResource(com.google.gerrit.server.group.GroupResource)

Example 19 with GroupControl

use of com.google.gerrit.server.account.GroupControl in project gerrit by GerritCodeReview.

the class ListMembers method getIndirectMemberIds.

private Set<Account.Id> getIndirectMemberIds(GroupDescription.Internal group, HashSet<AccountGroup.UUID> seenGroups) {
    Set<Account.Id> indirectMembers = new HashSet<>();
    Set<AccountGroup.UUID> subgroupMembersToLoad = new HashSet<>();
    for (AccountGroup.UUID subgroupUuid : group.getSubgroups()) {
        if (!seenGroups.contains(subgroupUuid)) {
            seenGroups.add(subgroupUuid);
            subgroupMembersToLoad.add(subgroupUuid);
        }
    }
    groupCache.get(subgroupMembersToLoad).values().stream().map(InternalGroupDescription::new).forEach(subgroup -> {
        GroupControl subgroupControl = groupControlFactory.controlFor(subgroup);
        indirectMembers.addAll(getTransitiveMemberIds(subgroup, subgroupControl, seenGroups));
    });
    return indirectMembers;
}
Also used : GroupControl(com.google.gerrit.server.account.GroupControl) AccountGroup(com.google.gerrit.entities.AccountGroup) HashSet(java.util.HashSet)

Aggregations

GroupControl (com.google.gerrit.server.account.GroupControl)19 AuthException (com.google.gerrit.extensions.restapi.AuthException)10 ArrayList (java.util.ArrayList)9 AccountGroup (com.google.gerrit.entities.AccountGroup)7 NoSuchGroupException (com.google.gerrit.exceptions.NoSuchGroupException)7 GroupDescription (com.google.gerrit.entities.GroupDescription)6 GroupInfo (com.google.gerrit.extensions.common.GroupInfo)6 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)6 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)6 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)5 GroupResource (com.google.gerrit.server.group.GroupResource)4 HashSet (java.util.HashSet)4 GroupDescription (com.google.gerrit.common.data.GroupDescription)3 NoSuchGroupException (com.google.gerrit.common.errors.NoSuchGroupException)3 Account (com.google.gerrit.reviewdb.client.Account)3 AccountGroupById (com.google.gerrit.reviewdb.client.AccountGroupById)3 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)3 Account (com.google.gerrit.entities.Account)2 AgreementInfo (com.google.gerrit.extensions.common.AgreementInfo)2 IdString (com.google.gerrit.extensions.restapi.IdString)2