Search in sources :

Example 66 with Account

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

the class GroupMembers method getGroupMembers.

private Set<Account> getGroupMembers(InternalGroup group, @Nullable Project.NameKey project, Set<AccountGroup.UUID> seen) throws NoSuchProjectException, IOException {
    seen.add(group.getGroupUUID());
    GroupControl groupControl = groupControlFactory.controlFor(new InternalGroupDescription(group));
    Set<Account> directMembers = group.getMembers().stream().filter(groupControl::canSeeMember).map(accountCache::get).flatMap(Streams::stream).map(AccountState::account).collect(toImmutableSet());
    Set<Account> indirectMembers = new HashSet<>();
    if (groupControl.canSeeGroup()) {
        for (AccountGroup.UUID subgroupUuid : group.getSubgroups()) {
            if (!seen.contains(subgroupUuid)) {
                indirectMembers.addAll(listAccounts(subgroupUuid, project, seen));
            }
        }
    }
    return Sets.union(directMembers, indirectMembers);
}
Also used : Account(com.google.gerrit.entities.Account) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) AccountGroup(com.google.gerrit.entities.AccountGroup) Streams(com.google.common.collect.Streams) HashSet(java.util.HashSet)

Example 67 with Account

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

the class InternalAccountDirectory method fill.

private void fill(AccountInfo info, AccountState accountState, Set<FillOptions> options) {
    Account account = accountState.account();
    if (options.contains(FillOptions.ID)) {
        info._accountId = account.id().get();
    } else {
        // Was previously set to look up account for filling.
        info._accountId = null;
    }
    if (options.contains(FillOptions.NAME)) {
        info.name = Strings.emptyToNull(account.fullName());
        if (info.name == null) {
            info.name = accountState.userName().orElse(null);
        }
    }
    if (options.contains(FillOptions.EMAIL)) {
        info.email = account.preferredEmail();
    }
    if (options.contains(FillOptions.SECONDARY_EMAILS)) {
        info.secondaryEmails = getSecondaryEmails(account, accountState.externalIds());
    }
    if (options.contains(FillOptions.USERNAME)) {
        info.username = accountState.userName().orElse(null);
    }
    if (options.contains(FillOptions.DISPLAY_NAME)) {
        info.displayName = account.displayName();
    }
    if (options.contains(FillOptions.STATUS)) {
        info.status = account.status();
    }
    if (options.contains(FillOptions.STATE)) {
        info.inactive = account.inactive() ? true : null;
    }
    if (options.contains(FillOptions.TAGS)) {
        List<String> tags = getTags(account.id());
        if (!tags.isEmpty()) {
            info.tags = tags;
        }
    }
    if (options.contains(FillOptions.AVATARS)) {
        AvatarProvider ap = avatar.get();
        if (ap != null) {
            info.avatars = new ArrayList<>();
            IdentifiedUser user = userFactory.create(account.id());
            // PolyGerrit UI uses the following sizes for avatars:
            // - 32px for avatars next to names e.g. on the dashboard. This is also Gerrit's default.
            // - 56px for the user's own avatar in the menu
            // - 100ox for other user's avatars on dashboards
            // - 120px for the user's own profile settings page
            addAvatar(ap, info, user, AvatarInfo.DEFAULT_SIZE);
            if (!info.avatars.isEmpty()) {
                addAvatar(ap, info, user, 56);
                addAvatar(ap, info, user, 100);
                addAvatar(ap, info, user, 120);
            }
        }
    }
}
Also used : Account(com.google.gerrit.entities.Account) AvatarProvider(com.google.gerrit.server.avatar.AvatarProvider) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 68 with Account

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

the class AllAccountsIndexer method reindexAccounts.

private SiteIndexer.Result reindexAccounts(AccountIndex index, List<Account.Id> ids, ProgressMonitor progress) {
    progress.beginTask("Reindexing accounts", ids.size());
    List<ListenableFuture<?>> futures = new ArrayList<>(ids.size());
    AtomicBoolean ok = new AtomicBoolean(true);
    AtomicInteger done = new AtomicInteger();
    AtomicInteger failed = new AtomicInteger();
    Stopwatch sw = Stopwatch.createStarted();
    for (Account.Id id : ids) {
        String desc = "account " + id;
        ListenableFuture<?> future = executor.submit(() -> {
            try {
                Optional<AccountState> a = accountCache.get(id);
                if (a.isPresent()) {
                    if (isFirstInsertForEntry.equals(IsFirstInsertForEntry.YES)) {
                        index.insert(a.get());
                    } else {
                        index.replace(a.get());
                    }
                } else {
                    index.delete(id);
                }
                verboseWriter.println("Reindexed " + desc);
                done.incrementAndGet();
            } catch (Exception e) {
                failed.incrementAndGet();
                throw e;
            }
            return null;
        });
        addErrorListener(future, desc, progress, ok);
        futures.add(future);
    }
    try {
        Futures.successfulAsList(futures).get();
    } catch (ExecutionException | InterruptedException e) {
        logger.atSevere().withCause(e).log("Error waiting on account futures");
        return SiteIndexer.Result.create(sw, false, 0, 0);
    }
    progress.endTask();
    return SiteIndexer.Result.create(sw, ok.get(), done.get(), failed.get());
}
Also used : Account(com.google.gerrit.entities.Account) ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) AccountState(com.google.gerrit.server.account.AccountState) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException)

Example 69 with Account

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

the class CommitRewriter method verifyCommit.

/**
 * Verifies that the commit does not contain user data of accounts in {@code accounts}.
 */
private boolean verifyCommit(String commitMessage, PersonIdent author, Collection<AccountState> accounts) {
    for (AccountState accountState : accounts) {
        Account account = accountState.account();
        if (commitMessage.contains(account.getName())) {
            return false;
        }
        if (account.fullName() != null && commitMessage.contains(account.fullName())) {
            return false;
        }
        if (account.displayName() != null && commitMessage.contains(account.displayName())) {
            return false;
        }
        if (account.preferredEmail() != null && commitMessage.contains(account.preferredEmail())) {
            return false;
        }
        if (accountState.userName().isPresent() && commitMessage.contains(accountState.userName().get())) {
            return false;
        }
        Stream<String> allEmails = accountState.externalIds().stream().map(ExternalId::email).filter(Objects::nonNull);
        if (allEmails.anyMatch(email -> commitMessage.contains(email))) {
            return false;
        }
        if (author.toString().contains(account.getName())) {
            return false;
        }
    }
    return true;
}
Also used : Account(com.google.gerrit.entities.Account) Objects(java.util.Objects) AccountState(com.google.gerrit.server.account.AccountState)

Example 70 with Account

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

the class ReviewersUtil method suggestAccounts.

private List<Account.Id> suggestAccounts(SuggestReviewers suggestReviewers) throws BadRequestException {
    try (Timer0.Context ctx = metrics.queryAccountsLatency.start()) {
        // For performance reasons we don't use AccountQueryProvider as it would always load the
        // complete account from the cache (or worse, from NoteDb) even though we only need the ID
        // which we can directly get from the returned results.
        Predicate<AccountState> pred = Predicate.and(AccountPredicates.isActive(), accountQueryBuilder.defaultQuery(suggestReviewers.getQuery()));
        logger.atFine().log("accounts index query: %s", pred);
        accountIndexRewriter.validateMaxTermsInQuery(pred);
        boolean useLegacyNumericFields = accountIndexes.getSearchIndex().getSchema().useLegacyNumericFields();
        FieldDef<AccountState, ?> idField = useLegacyNumericFields ? AccountField.ID : AccountField.ID_STR;
        ResultSet<FieldBundle> result = accountIndexes.getSearchIndex().getSource(pred, QueryOptions.create(indexConfig, 0, suggestReviewers.getLimit(), ImmutableSet.of(idField.getName()))).readRaw();
        List<Account.Id> matches = result.toList().stream().map(f -> fromIdField(f, useLegacyNumericFields)).collect(toList());
        logger.atFine().log("Matches: %s", matches);
        return matches;
    } catch (TooManyTermsInQueryException e) {
        throw new BadRequestException(e.getMessage());
    } catch (QueryParseException e) {
        logger.atWarning().withCause(e).log("Suggesting accounts failed, return empty result.");
        return ImmutableList.of();
    } catch (StorageException e) {
        if (e.getCause() instanceof TooManyTermsInQueryException) {
            throw new BadRequestException(e.getMessage());
        }
        if (e.getCause() instanceof QueryParseException) {
            return ImmutableList.of();
        }
        throw e;
    }
}
Also used : GroupBackend(com.google.gerrit.server.account.GroupBackend) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ReviewerModifier(com.google.gerrit.server.change.ReviewerModifier) EnumSet(java.util.EnumSet) FieldDef(com.google.gerrit.index.FieldDef) ImmutableSet(com.google.common.collect.ImmutableSet) GroupBaseInfo(com.google.gerrit.extensions.common.GroupBaseInfo) Timer0(com.google.gerrit.metrics.Timer0) Account(com.google.gerrit.entities.Account) FillOptions(com.google.gerrit.server.account.AccountDirectory.FillOptions) AccountQueryBuilder(com.google.gerrit.server.query.account.AccountQueryBuilder) Set(java.util.Set) AccountIndexCollection(com.google.gerrit.server.index.account.AccountIndexCollection) Sets(com.google.common.collect.Sets) GroupReference(com.google.gerrit.entities.GroupReference) Objects(java.util.Objects) TooManyTermsInQueryException(com.google.gerrit.index.query.TooManyTermsInQueryException) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Url(com.google.gerrit.extensions.restapi.Url) MetricMaker(com.google.gerrit.metrics.MetricMaker) LazyArgs.lazy(com.google.common.flogger.LazyArgs.lazy) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) AccountLoader(com.google.gerrit.server.account.AccountLoader) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IndexConfig(com.google.gerrit.index.IndexConfig) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) ResultSet(com.google.gerrit.index.query.ResultSet) AccountPredicates(com.google.gerrit.server.query.account.AccountPredicates) GroupMembers(com.google.gerrit.server.account.GroupMembers) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) QueryParseException(com.google.gerrit.index.query.QueryParseException) Description(com.google.gerrit.metrics.Description) FieldBundle(com.google.gerrit.index.query.FieldBundle) AccountIndexRewriter(com.google.gerrit.server.index.account.AccountIndexRewriter) Predicate(com.google.gerrit.index.query.Predicate) SuggestedReviewerInfo(com.google.gerrit.extensions.common.SuggestedReviewerInfo) AccountControl(com.google.gerrit.server.account.AccountControl) CurrentUser(com.google.gerrit.server.CurrentUser) AccountField(com.google.gerrit.server.index.account.AccountField) StorageException(com.google.gerrit.exceptions.StorageException) Units(com.google.gerrit.metrics.Description.Units) ProjectState(com.google.gerrit.server.project.ProjectState) QueryOptions(com.google.gerrit.index.QueryOptions) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) Project(com.google.gerrit.entities.Project) AccountState(com.google.gerrit.server.account.AccountState) ServiceUserClassifier(com.google.gerrit.server.account.ServiceUserClassifier) Collections(java.util.Collections) TooManyTermsInQueryException(com.google.gerrit.index.query.TooManyTermsInQueryException) FieldBundle(com.google.gerrit.index.query.FieldBundle) AccountState(com.google.gerrit.server.account.AccountState) QueryParseException(com.google.gerrit.index.query.QueryParseException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Timer0(com.google.gerrit.metrics.Timer0) StorageException(com.google.gerrit.exceptions.StorageException)

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