Search in sources :

Example 1 with NoSuchGroupException

use of com.google.gerrit.exceptions.NoSuchGroupException in project gerrit by GerritCodeReview.

the class PutOptions method apply.

@Override
public Response<GroupOptionsInfo> apply(GroupResource resource, GroupOptionsInfo input) throws NotInternalGroupException, AuthException, BadRequestException, ResourceNotFoundException, IOException, ConfigInvalidException {
    GroupDescription.Internal internalGroup = resource.asInternalGroup().orElseThrow(NotInternalGroupException::new);
    if (!resource.getControl().isOwner()) {
        throw new AuthException("Not group owner");
    }
    if (input == null) {
        throw new BadRequestException("options are required");
    }
    if (input.visibleToAll == null) {
        input.visibleToAll = false;
    }
    if (internalGroup.isVisibleToAll() != input.visibleToAll) {
        AccountGroup.UUID groupUuid = internalGroup.getGroupUUID();
        GroupDelta groupDelta = GroupDelta.builder().setVisibleToAll(input.visibleToAll).build();
        try {
            groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta);
        } catch (NoSuchGroupException e) {
            throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
        }
    }
    GroupOptionsInfo options = new GroupOptionsInfo();
    if (input.visibleToAll) {
        options.visibleToAll = true;
    }
    return Response.ok(options);
}
Also used : GroupDescription(com.google.gerrit.entities.GroupDescription) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupDelta(com.google.gerrit.server.group.db.GroupDelta) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) GroupOptionsInfo(com.google.gerrit.extensions.common.GroupOptionsInfo) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException)

Example 2 with NoSuchGroupException

use of com.google.gerrit.exceptions.NoSuchGroupException in project gerrit by GerritCodeReview.

the class PutName method renameGroup.

private void renameGroup(GroupDescription.Internal group, String newName) throws ResourceConflictException, ResourceNotFoundException, IOException, ConfigInvalidException {
    AccountGroup.UUID groupUuid = group.getGroupUUID();
    GroupDelta groupDelta = GroupDelta.builder().setName(AccountGroup.nameKey(newName)).build();
    try {
        groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta);
    } catch (NoSuchGroupException e) {
        throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
    } catch (DuplicateKeyException e) {
        throw new ResourceConflictException("group with name " + newName + " already exists", e);
    }
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupDelta(com.google.gerrit.server.group.db.GroupDelta) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) DuplicateKeyException(com.google.gerrit.exceptions.DuplicateKeyException)

Example 3 with NoSuchGroupException

use of com.google.gerrit.exceptions.NoSuchGroupException in project gerrit by GerritCodeReview.

the class ListSubgroups method getDirectSubgroups.

public List<GroupInfo> getDirectSubgroups(GroupDescription.Internal group, GroupControl groupControl) throws PermissionBackendException {
    boolean ownerOfParent = groupControl.isOwner();
    List<GroupInfo> included = new ArrayList<>();
    for (AccountGroup.UUID subgroupUuid : group.getSubgroups()) {
        try {
            GroupControl i = controlFactory.controlFor(subgroupUuid);
            if (ownerOfParent || i.isVisible()) {
                included.add(json.format(i.getGroup()));
            }
        } catch (NoSuchGroupException notFound) {
            logger.atWarning().log("Group %s no longer available, subgroup of %s", subgroupUuid, group.getName());
            continue;
        }
    }
    included.sort(comparing((GroupInfo g) -> nullToEmpty(g.name)).thenComparing(g -> nullToEmpty(g.id)));
    return included;
}
Also used : GroupControl(com.google.gerrit.server.account.GroupControl) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupResource(com.google.gerrit.server.group.GroupResource) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RestReadView(com.google.gerrit.extensions.restapi.RestReadView) GroupControl(com.google.gerrit.server.account.GroupControl) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) Inject(com.google.inject.Inject) Response(com.google.gerrit.extensions.restapi.Response) ArrayList(java.util.ArrayList) List(java.util.List) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) GroupDescription(com.google.gerrit.entities.GroupDescription) Comparator.comparing(java.util.Comparator.comparing) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) ArrayList(java.util.ArrayList) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException)

Example 4 with NoSuchGroupException

use of com.google.gerrit.exceptions.NoSuchGroupException in project gerrit by GerritCodeReview.

the class DeleteSubgroups method apply.

@Override
public Response<?> apply(GroupResource resource, Input input) throws AuthException, NotInternalGroupException, UnprocessableEntityException, ResourceNotFoundException, IOException, ConfigInvalidException {
    GroupDescription.Internal internalGroup = resource.asInternalGroup().orElseThrow(NotInternalGroupException::new);
    input = Input.init(input);
    final GroupControl control = resource.getControl();
    if (!control.canRemoveGroup()) {
        throw new AuthException(String.format("Cannot delete groups from group %s", internalGroup.getName()));
    }
    Set<AccountGroup.UUID> subgroupsToRemove = new HashSet<>();
    for (String subgroupIdentifier : input.groups) {
        GroupDescription.Basic subgroup = groupResolver.parse(subgroupIdentifier);
        subgroupsToRemove.add(subgroup.getGroupUUID());
    }
    AccountGroup.UUID groupUuid = internalGroup.getGroupUUID();
    try {
        removeSubgroups(groupUuid, subgroupsToRemove);
    } 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 5 with NoSuchGroupException

use of com.google.gerrit.exceptions.NoSuchGroupException in project gerrit by GerritCodeReview.

the class GroupsUpdate method updateGroupInNoteDb.

@VisibleForTesting
public UpdateResult updateGroupInNoteDb(AccountGroup.UUID groupUuid, GroupDelta groupDelta) throws IOException, ConfigInvalidException, DuplicateKeyException, NoSuchGroupException {
    try (Repository allUsersRepo = repoManager.openRepository(allUsersName)) {
        GroupConfig groupConfig = GroupConfig.loadForGroup(allUsersName, allUsersRepo, groupUuid);
        groupConfig.setGroupDelta(groupDelta, auditLogFormatter);
        if (!groupConfig.getLoadedGroup().isPresent()) {
            throw new NoSuchGroupException(groupUuid);
        }
        InternalGroup originalGroup = groupConfig.getLoadedGroup().get();
        GroupNameNotes groupNameNotes = null;
        if (groupDelta.getName().isPresent()) {
            AccountGroup.NameKey oldName = originalGroup.getNameKey();
            AccountGroup.NameKey newName = groupDelta.getName().get();
            groupNameNotes = GroupNameNotes.forRename(allUsersName, allUsersRepo, groupUuid, oldName, newName);
        }
        commit(allUsersRepo, groupConfig, groupNameNotes);
        InternalGroup updatedGroup = groupConfig.getLoadedGroup().orElseThrow(() -> new IllegalStateException("Updated group wasn't automatically loaded"));
        return getUpdateResult(originalGroup, updatedGroup);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) AccountGroup(com.google.gerrit.entities.AccountGroup) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) InternalGroup(com.google.gerrit.entities.InternalGroup) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

NoSuchGroupException (com.google.gerrit.exceptions.NoSuchGroupException)26 AccountGroup (com.google.gerrit.entities.AccountGroup)22 GroupDescription (com.google.gerrit.entities.GroupDescription)13 AuthException (com.google.gerrit.extensions.restapi.AuthException)12 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)12 GroupControl (com.google.gerrit.server.account.GroupControl)12 GroupDelta (com.google.gerrit.server.group.db.GroupDelta)12 Account (com.google.gerrit.entities.Account)10 ArrayList (java.util.ArrayList)10 Inject (com.google.inject.Inject)9 IOException (java.io.IOException)9 Singleton (com.google.inject.Singleton)8 Sets (com.google.common.collect.Sets)7 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)7 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)6 IdString (com.google.gerrit.extensions.restapi.IdString)6 Response (com.google.gerrit.extensions.restapi.Response)6 GroupResource (com.google.gerrit.server.group.GroupResource)6 GroupsUpdate (com.google.gerrit.server.group.db.GroupsUpdate)6