Search in sources :

Example 21 with AccountLoader

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

the class ListMembers method toAccountInfos.

private List<AccountInfo> toAccountInfos(Set<Account.Id> members) throws PermissionBackendException {
    AccountLoader accountLoader = accountLoaderFactory.create(true);
    List<AccountInfo> memberInfos = new ArrayList<>(members.size());
    for (Account.Id member : members) {
        memberInfos.add(accountLoader.get(member));
    }
    accountLoader.fill();
    memberInfos.sort(AccountInfoComparator.ORDER_NULLS_FIRST);
    return memberInfos;
}
Also used : Account(com.google.gerrit.entities.Account) AccountLoader(com.google.gerrit.server.account.AccountLoader) ArrayList(java.util.ArrayList) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 22 with AccountLoader

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

the class CreateAccount method apply.

public Response<AccountInfo> apply(IdString id, AccountInput input) throws BadRequestException, ResourceConflictException, UnprocessableEntityException, IOException, ConfigInvalidException, PermissionBackendException {
    String username = applyCaseOfUsername(id.get());
    if (input.username != null && !username.equals(applyCaseOfUsername(input.username))) {
        throw new BadRequestException("username must match URL");
    }
    if (!ExternalId.isValidUsername(username)) {
        throw new BadRequestException("Invalid username '" + username + "'");
    }
    if (input.name == null) {
        input.name = input.username;
    }
    Set<AccountGroup.UUID> groups = parseGroups(input.groups);
    Account.Id accountId = Account.id(seq.nextAccountId());
    List<ExternalId> extIds = new ArrayList<>();
    if (input.email != null) {
        if (!validator.isValid(input.email)) {
            throw new BadRequestException("invalid email address");
        }
        extIds.add(externalIdFactory.createEmail(accountId, input.email));
    }
    extIds.add(externalIdFactory.createUsername(username, accountId, input.httpPassword));
    externalIdCreators.runEach(c -> extIds.addAll(c.create(accountId, username, input.email)));
    try {
        accountsUpdateProvider.get().insert("Create Account via API", accountId, u -> u.setFullName(input.name).setPreferredEmail(input.email).addExternalIds(extIds));
    } catch (DuplicateExternalIdKeyException e) {
        if (e.getDuplicateKey().isScheme(SCHEME_USERNAME)) {
            throw new ResourceConflictException("username '" + e.getDuplicateKey().id() + "' already exists");
        } else if (e.getDuplicateKey().isScheme(SCHEME_MAILTO)) {
            throw new UnprocessableEntityException("email '" + e.getDuplicateKey().id() + "' already exists");
        } else {
            // AccountExternalIdCreator returned an external ID that already exists
            throw e;
        }
    }
    for (AccountGroup.UUID groupUuid : groups) {
        try {
            addGroupMember(groupUuid, accountId);
        } catch (NoSuchGroupException e) {
            throw new UnprocessableEntityException(String.format("Group %s not found", groupUuid), e);
        }
    }
    if (input.sshKey != null) {
        try {
            authorizedKeys.addKey(accountId, input.sshKey);
            sshKeyCache.evict(username);
        } catch (InvalidSshKeyException e) {
            throw new BadRequestException(e.getMessage());
        }
    }
    AccountLoader loader = infoLoader.create(true);
    AccountInfo info = loader.get(accountId);
    loader.fill();
    return Response.created(info);
}
Also used : Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) DuplicateExternalIdKeyException(com.google.gerrit.server.account.externalids.DuplicateExternalIdKeyException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) IdString(com.google.gerrit.extensions.restapi.IdString) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) InvalidSshKeyException(com.google.gerrit.exceptions.InvalidSshKeyException) AccountGroup(com.google.gerrit.entities.AccountGroup) AccountLoader(com.google.gerrit.server.account.AccountLoader) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 23 with AccountLoader

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

the class GetPastAssignees method apply.

@Override
public Response<List<AccountInfo>> apply(ChangeResource rsrc) throws PermissionBackendException {
    Set<Account.Id> pastAssignees = rsrc.getNotes().load().getPastAssignees();
    if (pastAssignees == null) {
        return Response.ok(Collections.emptyList());
    }
    AccountLoader accountLoader = accountLoaderFactory.create(true);
    List<AccountInfo> infos = pastAssignees.stream().map(accountLoader::get).collect(toList());
    accountLoader.fill();
    return Response.ok(infos);
}
Also used : AccountLoader(com.google.gerrit.server.account.AccountLoader) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 24 with AccountLoader

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

the class ListChangeMessages method apply.

@Override
public Response<List<ChangeMessageInfo>> apply(ChangeResource resource) throws PermissionBackendException {
    AccountLoader accountLoader = accountLoaderFactory.create(true);
    List<ChangeMessage> messages = changeMessagesUtil.byChange(resource.getNotes());
    List<ChangeMessageInfo> messageInfos = messages.stream().map(m -> changeMessagesUtil.createChangeMessageInfoWithReplacedTemplates(m, accountLoader)).collect(Collectors.toList());
    accountLoader.fill();
    return Response.ok(messageInfos);
}
Also used : ChangeMessage(com.google.gerrit.entities.ChangeMessage) AccountLoader(com.google.gerrit.server.account.AccountLoader) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) List(java.util.List) RestReadView(com.google.gerrit.extensions.restapi.RestReadView) ChangeResource(com.google.gerrit.server.change.ChangeResource) Inject(com.google.inject.Inject) ChangeMessageInfo(com.google.gerrit.extensions.common.ChangeMessageInfo) Response(com.google.gerrit.extensions.restapi.Response) Collectors(java.util.stream.Collectors) ChangeMessagesUtil(com.google.gerrit.server.ChangeMessagesUtil) Singleton(com.google.inject.Singleton) AccountLoader(com.google.gerrit.server.account.AccountLoader) ChangeMessage(com.google.gerrit.entities.ChangeMessage) ChangeMessageInfo(com.google.gerrit.extensions.common.ChangeMessageInfo)

Aggregations

AccountLoader (com.google.gerrit.server.account.AccountLoader)24 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)13 ArrayList (java.util.ArrayList)10 Account (com.google.gerrit.entities.Account)7 Inject (com.google.inject.Inject)6 Singleton (com.google.inject.Singleton)6 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)5 Set (java.util.Set)5 AuthException (com.google.gerrit.extensions.restapi.AuthException)4 List (java.util.List)4 ImmutableList (com.google.common.collect.ImmutableList)3 Lists (com.google.common.collect.Lists)3 FluentLogger (com.google.common.flogger.FluentLogger)3 Nullable (com.google.gerrit.common.Nullable)3 ChangeMessage (com.google.gerrit.entities.ChangeMessage)3 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)3 SubmitRecord (com.google.gerrit.entities.SubmitRecord)3 ApprovalInfo (com.google.gerrit.extensions.common.ApprovalInfo)3 ChangeMessageInfo (com.google.gerrit.extensions.common.ChangeMessageInfo)3 Url (com.google.gerrit.extensions.restapi.Url)3