Search in sources :

Example 71 with Account

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

the class ReviewersUtil method suggestGroupAsReviewer.

private GroupAsReviewer suggestGroupAsReviewer(SuggestReviewers suggestReviewers, Project project, GroupReference group, VisibilityControl visibilityControl) throws IOException {
    GroupAsReviewer result = new GroupAsReviewer();
    int maxAllowed = suggestReviewers.getMaxAllowed();
    int maxAllowedWithoutConfirmation = suggestReviewers.getMaxAllowedWithoutConfirmation();
    logger.atFine().log("maxAllowedWithoutConfirmation: %s", maxAllowedWithoutConfirmation);
    if (!ReviewerModifier.isLegalReviewerGroup(group.getUUID())) {
        logger.atFine().log("Ignore group %s that is not legal as reviewer", group.getUUID());
        return result;
    }
    try {
        Set<Account> members = groupMembers.listAccounts(group.getUUID(), project.getNameKey());
        if (members.isEmpty()) {
            logger.atFine().log("Ignore group %s since it has no members", group.getUUID());
            return result;
        }
        result.size = members.size();
        if (maxAllowed > 0 && result.size > maxAllowed) {
            return result;
        }
        boolean needsConfirmation = maxAllowedWithoutConfirmation > 0 && result.size > maxAllowedWithoutConfirmation;
        if (needsConfirmation) {
            logger.atFine().log("group %s needs confirmation to be added as reviewer, it has %d members", group.getUUID(), result.size);
        }
        // require that at least one member in the group can see the change
        for (Account account : members) {
            if (visibilityControl.isVisibleTo(account.id())) {
                if (needsConfirmation) {
                    result.allowedWithConfirmation = true;
                } else {
                    result.allowed = true;
                }
                logger.atFine().log("Suggest group %s", group.getUUID());
                return result;
            }
        }
        logger.atFine().log("Ignore group %s since none of its members can see the change", group.getUUID());
    } catch (NoSuchProjectException e) {
        return result;
    }
    return result;
}
Also used : Account(com.google.gerrit.entities.Account) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException)

Example 72 with Account

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

the class AddMembers method apply.

@Override
public Response<List<AccountInfo>> apply(GroupResource resource, Input input) throws AuthException, NotInternalGroupException, UnprocessableEntityException, IOException, ConfigInvalidException, ResourceNotFoundException, PermissionBackendException {
    GroupDescription.Internal internalGroup = resource.asInternalGroup().orElseThrow(NotInternalGroupException::new);
    input = Input.init(input);
    GroupControl control = resource.getControl();
    if (!control.canAddMember()) {
        throw new AuthException("Cannot add members to group " + internalGroup.getName());
    }
    Set<Account.Id> newMemberIds = new LinkedHashSet<>();
    for (String nameOrEmailOrId : input.members) {
        Account a = findAccount(nameOrEmailOrId);
        if (!a.isActive()) {
            throw new UnprocessableEntityException(String.format("Account Inactive: %s", nameOrEmailOrId));
        }
        newMemberIds.add(a.id());
    }
    AccountGroup.UUID groupUuid = internalGroup.getGroupUUID();
    try {
        addMembers(groupUuid, newMemberIds);
    } catch (NoSuchGroupException e) {
        throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
    }
    return Response.ok(toAccountInfoList(newMemberIds));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) AuthException(com.google.gerrit.extensions.restapi.AuthException) IdString(com.google.gerrit.extensions.restapi.IdString) 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) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 73 with Account

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

the class LsUserRefs method run.

@Override
protected void run() throws Failure {
    enableGracefulStop();
    Account.Id userAccountId;
    try {
        userAccountId = accountResolver.resolve(userName).asUnique().account().id();
    } catch (UnprocessableEntityException e) {
        stdout.println(e.getMessage());
        stdout.flush();
        return;
    } catch (StorageException | IOException | ConfigInvalidException e) {
        throw die(e);
    }
    Project.NameKey projectName = projectState.getNameKey();
    try (Repository repo = repoManager.openRepository(projectName);
        ManualRequestContext ctx = requestContext.openAs(userAccountId)) {
        try {
            Collection<Ref> refsMap = permissionBackend.user(ctx.getUser()).project(projectName).filter(repo.getRefDatabase().getRefs(), repo, RefFilterOptions.defaults());
            for (Ref ref : refsMap) {
                if (!onlyRefsHeads || ref.getName().startsWith(RefNames.REFS_HEADS)) {
                    stdout.println(ref);
                }
            }
        } catch (IOException | PermissionBackendException e) {
            throw new Failure(1, "fatal: Error reading refs: '" + projectName, e);
        }
    } catch (RepositoryNotFoundException e) {
        throw die("'" + projectName + "': not a git archive", e);
    } catch (IOException e) {
        throw die("Error opening: '" + projectName, e);
    }
}
Also used : Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Project(com.google.gerrit.entities.Project) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ManualRequestContext(com.google.gerrit.server.util.ManualRequestContext) StorageException(com.google.gerrit.exceptions.StorageException)

Example 74 with Account

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

the class QueryChangesIT method queryByFullNameEmailFormatWithEmptyFullNameWhenEmailMatchesSeveralAccounts.

@Test
public void queryByFullNameEmailFormatWithEmptyFullNameWhenEmailMatchesSeveralAccounts() throws Exception {
    // Create 2 accounts with the same preferred email (both account must have no external ID for
    // the email because otherwise the account with the external ID takes precedence).
    String email = "foo.bar@example.com";
    Account.Id account1 = accountOperations.newAccount().create();
    accountOperations.account(account1).forInvalidation().preferredEmailWithoutExternalId(email).invalidate();
    Account.Id account2 = accountOperations.newAccount().create();
    accountOperations.account(account2).forInvalidation().preferredEmailWithoutExternalId(email).invalidate();
    // Search with "Full Name <email>" format, but without full name. Both created accounts match
    // the email. In this case Gerrit falls back to match on the full name. Check that this logic
    // doesn't fail if the full name in the input string is not present.
    QueryChanges queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("<" + email + ">");
    assertThat(queryChanges.apply(TopLevelResource.INSTANCE).statusCode()).isEqualTo(SC_OK);
}
Also used : Account(com.google.gerrit.entities.Account) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 75 with Account

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

the class PostGpgKeys method apply.

@Override
public Response<Map<String, GpgKeyInfo>> apply(AccountResource rsrc, GpgKeysInput input) throws RestApiException, PGPException, IOException, ConfigInvalidException {
    GpgKeys.checkVisible(self, rsrc);
    Collection<ExternalId> existingExtIds = externalIds.byAccount(rsrc.getUser().getAccountId(), SCHEME_GPGKEY);
    try (PublicKeyStore store = storeProvider.get()) {
        Map<ExternalId, Fingerprint> toRemove = readKeysToRemove(input, existingExtIds);
        Collection<Fingerprint> fingerprintsToRemove = toRemove.values();
        List<PGPPublicKeyRing> newKeys = readKeysToAdd(input, fingerprintsToRemove);
        List<ExternalId> newExtIds = new ArrayList<>(existingExtIds.size());
        for (PGPPublicKeyRing keyRing : newKeys) {
            PGPPublicKey key = keyRing.getPublicKey();
            ExternalId.Key extIdKey = toExtIdKey(key.getFingerprint());
            Account account = getAccountByExternalId(extIdKey);
            if (account != null) {
                if (!account.id().equals(rsrc.getUser().getAccountId())) {
                    throw new ResourceConflictException("GPG key already associated with another account");
                }
            } else {
                newExtIds.add(externalIdFactory.create(extIdKey, rsrc.getUser().getAccountId()));
            }
        }
        storeKeys(rsrc, newKeys, fingerprintsToRemove);
        accountsUpdateProvider.get().update("Update GPG Keys via API", rsrc.getUser().getAccountId(), u -> u.replaceExternalIds(toRemove.keySet(), newExtIds));
        return Response.ok(toJson(newKeys, fingerprintsToRemove, store, rsrc.getUser()));
    }
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Account(com.google.gerrit.entities.Account) Fingerprint(com.google.gerrit.gpg.Fingerprint) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore)

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