Search in sources :

Example 6 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class StarredChangesUtil method byAccountId.

public ImmutableSet<Change.Id> byAccountId(Account.Id accountId, String label) {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        ImmutableSet.Builder<Change.Id> builder = ImmutableSet.builder();
        for (Ref ref : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_STARRED_CHANGES)) {
            Account.Id currentAccountId = Account.Id.fromRef(ref.getName());
            // Skip all refs that don't correspond with accountId.
            if (currentAccountId == null || !currentAccountId.equals(accountId)) {
                continue;
            }
            // Skip all refs that don't contain the required label.
            StarRef starRef = readLabels(repo, ref.getName());
            if (!starRef.labels().contains(label)) {
                continue;
            }
            // Skip invalid change ids.
            Change.Id changeId = Change.Id.fromAllUsersRef(ref.getName());
            if (changeId == null) {
                continue;
            }
            builder.add(changeId);
        }
        return builder.build();
    } catch (IOException e) {
        throw new StorageException(String.format("Get starred changes for account %d failed", accountId.get()), e);
    }
}
Also used : Account(com.google.gerrit.entities.Account) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ImmutableSet(com.google.common.collect.ImmutableSet) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 7 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountState method fromAccountConfig.

/**
 * Creates an AccountState from the given account config.
 *
 * <p>If external ID notes are provided the revision of the external IDs branch from which the
 * external IDs for the account should be loaded is taken from the external ID notes. If external
 * ID notes are not given the revision of the external IDs branch is taken from the account
 * config. Updating external IDs is done via {@link ExternalIdNotes} and if external IDs were
 * updated the revision of the external IDs branch in account config is outdated. Hence after
 * updating external IDs the external ID notes must be provided.
 *
 * @param externalIds class to access external IDs
 * @param accountConfig the account config, must already be loaded
 * @param extIdNotes external ID notes, must already be loaded, may be {@code null}
 * @param defaultPreferences the default preferences for this Gerrit installation
 * @return the account state, {@link Optional#empty()} if the account doesn't exist
 * @throws IOException if accessing the external IDs fails
 */
public static Optional<AccountState> fromAccountConfig(ExternalIds externalIds, AccountConfig accountConfig, @Nullable ExternalIdNotes extIdNotes, CachedPreferences defaultPreferences) throws IOException {
    if (!accountConfig.getLoadedAccount().isPresent()) {
        return Optional.empty();
    }
    Account account = accountConfig.getLoadedAccount().get();
    Optional<ObjectId> extIdsRev = extIdNotes != null ? Optional.ofNullable(extIdNotes.getRevision()) : accountConfig.getExternalIdsRev();
    ImmutableSet<ExternalId> extIds = extIdsRev.isPresent() ? externalIds.byAccount(account.id(), extIdsRev.get()) : ImmutableSet.of();
    // Don't leak references to AccountConfig into the AccountState, since it holds a reference to
    // an open Repository instance.
    ImmutableMap<ProjectWatchKey, ImmutableSet<NotifyType>> projectWatches = accountConfig.getProjectWatches();
    return Optional.of(new AutoValue_AccountState(account, extIds, ExternalId.getUserName(extIds), projectWatches, Optional.of(defaultPreferences), Optional.of(accountConfig.asCachedPreferences())));
}
Also used : Account(com.google.gerrit.entities.Account) ImmutableSet(com.google.common.collect.ImmutableSet) ObjectId(org.eclipse.jgit.lib.ObjectId) ProjectWatchKey(com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey) ExternalId(com.google.gerrit.server.account.externalids.ExternalId)

Example 8 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class GroupMembers method getProjectOwners.

private Set<Account> getProjectOwners(final Project.NameKey project, Set<AccountGroup.UUID> seen) throws NoSuchProjectException, IOException {
    seen.add(SystemGroupBackend.PROJECT_OWNERS);
    if (project == null) {
        return Collections.emptySet();
    }
    ProjectState projectState = projectCache.get(project).orElseThrow(noSuchProject(project));
    final HashSet<Account> projectOwners = new HashSet<>();
    for (AccountGroup.UUID ownerGroup : projectState.getAllOwners()) {
        if (!seen.contains(ownerGroup)) {
            projectOwners.addAll(listAccounts(ownerGroup, project, seen));
        }
    }
    return projectOwners;
}
Also used : Account(com.google.gerrit.entities.Account) AccountGroup(com.google.gerrit.entities.AccountGroup) ProjectState(com.google.gerrit.server.project.ProjectState) HashSet(java.util.HashSet)

Example 9 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountIdHandler method parseArguments.

@Override
public int parseArguments(Parameters params) throws CmdLineException {
    String token = params.getParameter(0);
    Account.Id accountId;
    try {
        try {
            accountId = accountResolver.resolve(token).asUnique().account().id();
        } catch (UnprocessableEntityException e) {
            switch(authType) {
                case HTTP_LDAP:
                case CLIENT_SSL_CERT_LDAP:
                case LDAP:
                    accountId = createAccountByLdap(token);
                    break;
                case CUSTOM_EXTENSION:
                case DEVELOPMENT_BECOME_ANY_ACCOUNT:
                case HTTP:
                case LDAP_BIND:
                case OAUTH:
                case OPENID:
                case OPENID_SSO:
                default:
                    String msg = "user \"%s\" not found";
                    logger.atSevere().withCause(e).log(msg, token);
                    throw new CmdLineException(owner, localizable(msg), token);
            }
        }
    } catch (StorageException e) {
        CmdLineException newException = new CmdLineException(owner, localizable("database is down"));
        newException.initCause(e);
        throw newException;
    } catch (IOException e) {
        throw new CmdLineException(owner, "Failed to load account", e);
    } catch (ConfigInvalidException e) {
        throw new CmdLineException(owner, "Invalid account config", e);
    }
    setter.addValue(accountId);
    return 1;
}
Also used : Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 10 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class ChangeData method draftRefs.

private Map<Account.Id, ObjectId> draftRefs() {
    if (draftsByUser == null) {
        if (!lazyload()) {
            return Collections.emptyMap();
        }
        Change c = change();
        if (c == null) {
            return Collections.emptyMap();
        }
        draftsByUser = new HashMap<>();
        for (Ref ref : commentsUtil.getDraftRefs(notes().getChangeId())) {
            Account.Id account = Account.Id.fromRefSuffix(ref.getName());
            if (account != null && // this point.
            !notes().getDraftComments(account, ref).isEmpty()) {
                draftsByUser.put(account, ref.getObjectId());
            }
        }
    }
    return draftsByUser;
}
Also used : Account(com.google.gerrit.entities.Account) StarRef(com.google.gerrit.server.StarredChangesUtil.StarRef) Ref(org.eclipse.jgit.lib.Ref) Change(com.google.gerrit.entities.Change)

Aggregations

Account (com.google.gerrit.entities.Account)124 Test (org.junit.Test)59 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)37 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)35 AccountState (com.google.gerrit.server.account.AccountState)35 IOException (java.io.IOException)31 Repository (org.eclipse.jgit.lib.Repository)31 Change (com.google.gerrit.entities.Change)28 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)26 Inject (com.google.inject.Inject)25 PersonIdent (org.eclipse.jgit.lib.PersonIdent)25 List (java.util.List)24 ArrayList (java.util.ArrayList)23 HashSet (java.util.HashSet)23 Set (java.util.Set)22 RefNames (com.google.gerrit.entities.RefNames)21 AuthRequest (com.google.gerrit.server.account.AuthRequest)21 Map (java.util.Map)21 ObjectId (org.eclipse.jgit.lib.ObjectId)21 ImmutableList (com.google.common.collect.ImmutableList)20