Search in sources :

Example 1 with CreateGroupArgs

use of com.google.gerrit.server.account.CreateGroupArgs 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 2 with CreateGroupArgs

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

the class CreateGroup method apply.

@Override
public GroupInfo apply(TopLevelResource resource, GroupInput input) throws AuthException, BadRequestException, UnprocessableEntityException, ResourceConflictException, OrmException, IOException {
    if (input == null) {
        input = new GroupInput();
    }
    if (input.name != null && !name.equals(input.name)) {
        throw new BadRequestException("name must match URL");
    }
    AccountGroup.Id ownerId = owner(input);
    CreateGroupArgs args = new CreateGroupArgs();
    args.setGroupName(name);
    args.groupDescription = Strings.emptyToNull(input.description);
    args.visibleToAll = MoreObjects.firstNonNull(input.visibleToAll, defaultVisibleToAll);
    args.ownerGroupId = ownerId;
    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.getId());
        }
        args.initialMembers = members;
    } else {
        args.initialMembers = ownerId == null ? Collections.singleton(self.get().getAccountId()) : Collections.<Account.Id>emptySet();
    }
    for (GroupCreationValidationListener l : groupCreationValidationListeners) {
        try {
            l.validateNewGroup(args);
        } catch (ValidationException e) {
            throw new ResourceConflictException(e.getMessage(), e);
        }
    }
    return json.format(GroupDescriptions.forAccountGroup(createGroup(args)));
}
Also used : GroupInput(com.google.gerrit.extensions.api.groups.GroupInput) Account(com.google.gerrit.reviewdb.client.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ValidationException(com.google.gerrit.server.validators.ValidationException) GroupCreationValidationListener(com.google.gerrit.server.validators.GroupCreationValidationListener) ArrayList(java.util.ArrayList) CreateGroupArgs(com.google.gerrit.server.account.CreateGroupArgs) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Aggregations

GroupInput (com.google.gerrit.extensions.api.groups.GroupInput)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)2 CreateGroupArgs (com.google.gerrit.server.account.CreateGroupArgs)2 ValidationException (com.google.gerrit.server.validators.ValidationException)2 ArrayList (java.util.ArrayList)2 Account (com.google.gerrit.entities.Account)1 AccountGroup (com.google.gerrit.entities.AccountGroup)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 Account (com.google.gerrit.reviewdb.client.Account)1 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)1 InternalGroupDescription (com.google.gerrit.server.group.InternalGroupDescription)1 GroupCreationValidationListener (com.google.gerrit.server.validators.GroupCreationValidationListener)1