Search in sources :

Example 11 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class CommitMessageOutputTest method anonymousUser.

@Test
public void anonymousUser() throws Exception {
    Account anon = new Account(new Account.Id(3), TimeUtil.nowTs());
    accountCache.put(anon);
    Change c = newChange();
    ChangeUpdate update = newUpdate(c, userFactory.create(anon.getId()));
    update.setChangeMessage("Comment on the change.");
    update.commit();
    RevCommit commit = parseCommit(update.getResult());
    assertBodyEquals("Update patch set 1\n\nComment on the change.\n\nPatch-set: 1\n", commit);
    PersonIdent author = commit.getAuthorIdent();
    assertThat(author.getName()).isEqualTo("Anonymous Coward (3)");
    assertThat(author.getEmailAddress()).isEqualTo("3@gerrit");
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Change(com.google.gerrit.reviewdb.client.Change) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 12 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class PutPreferred method apply.

public Response<String> apply(IdentifiedUser user, String email) throws ResourceNotFoundException, OrmException, IOException {
    AtomicBoolean alreadyPreferred = new AtomicBoolean(false);
    Account a = dbProvider.get().accounts().atomicUpdate(user.getAccountId(), new AtomicUpdate<Account>() {

        @Override
        public Account update(Account a) {
            if (email.equals(a.getPreferredEmail())) {
                alreadyPreferred.set(true);
            } else {
                a.setPreferredEmail(email);
            }
            return a;
        }
    });
    if (a == null) {
        throw new ResourceNotFoundException("account not found");
    }
    byIdCache.evict(a.getId());
    return alreadyPreferred.get() ? Response.ok("") : Response.created("");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Account(com.google.gerrit.reviewdb.client.Account) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 13 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class DeleteActive method apply.

@Override
public Response<?> apply(AccountResource rsrc, Input input) throws RestApiException, OrmException, IOException {
    if (self.get() == rsrc.getUser()) {
        throw new ResourceConflictException("cannot deactivate own account");
    }
    AtomicBoolean alreadyInactive = new AtomicBoolean(false);
    Account a = dbProvider.get().accounts().atomicUpdate(rsrc.getUser().getAccountId(), new AtomicUpdate<Account>() {

        @Override
        public Account update(Account a) {
            if (!a.isActive()) {
                alreadyInactive.set(true);
            } else {
                a.setActive(false);
            }
            return a;
        }
    });
    if (a == null) {
        throw new ResourceNotFoundException("account not found");
    }
    if (alreadyInactive.get()) {
        throw new ResourceConflictException("account not active");
    }
    byIdCache.evict(a.getId());
    return Response.none();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Account(com.google.gerrit.reviewdb.client.Account) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 14 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class GroupMembers method getGroupMembers.

private Set<Account> getGroupMembers(final AccountGroup group, final Project.NameKey project, final Set<AccountGroup.UUID> seen) throws NoSuchGroupException, OrmException, NoSuchProjectException, IOException {
    seen.add(group.getGroupUUID());
    final GroupDetail groupDetail = groupDetailFactory.create(group.getId()).call();
    final Set<Account> members = new HashSet<>();
    if (groupDetail.members != null) {
        for (final AccountGroupMember member : groupDetail.members) {
            members.add(accountCache.get(member.getAccountId()).getAccount());
        }
    }
    if (groupDetail.includes != null) {
        for (final AccountGroupById groupInclude : groupDetail.includes) {
            final AccountGroup includedGroup = groupCache.get(groupInclude.getIncludeUUID());
            if (includedGroup != null && !seen.contains(includedGroup.getGroupUUID())) {
                members.addAll(listAccounts(includedGroup.getGroupUUID(), project, seen));
            }
        }
    }
    return members;
}
Also used : GroupDetail(com.google.gerrit.common.data.GroupDetail) Account(com.google.gerrit.reviewdb.client.Account) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AccountGroupById(com.google.gerrit.reviewdb.client.AccountGroupById) HashSet(java.util.HashSet)

Example 15 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class ReceiveCommits method validateNewCommits.

private void validateNewCommits(RefControl ctl, ReceiveCommand cmd) {
    if (ctl.canForgeAuthor() && ctl.canForgeCommitter() && ctl.canForgeGerritServerIdentity() && ctl.canUploadMerges() && !projectControl.getProjectState().isUseSignedOffBy() && Iterables.isEmpty(rejectCommits) && !RefNames.REFS_CONFIG.equals(ctl.getRefName()) && !(MagicBranch.isMagicBranch(cmd.getRefName()) || NEW_PATCHSET.matcher(cmd.getRefName()).matches())) {
        logDebug("Short-circuiting new commit validation");
        return;
    }
    boolean defaultName = Strings.isNullOrEmpty(user.getAccount().getFullName());
    RevWalk walk = rp.getRevWalk();
    walk.reset();
    walk.sort(RevSort.NONE);
    try {
        RevObject parsedObject = walk.parseAny(cmd.getNewId());
        if (!(parsedObject instanceof RevCommit)) {
            return;
        }
        ListMultimap<ObjectId, Ref> existing = changeRefsById();
        walk.markStart((RevCommit) parsedObject);
        markHeadsAsUninteresting(walk, cmd.getRefName());
        int i = 0;
        for (RevCommit c; (c = walk.next()) != null; ) {
            i++;
            if (existing.keySet().contains(c)) {
                continue;
            } else if (!validCommit(walk, ctl, cmd, c)) {
                break;
            }
            if (defaultName && user.hasEmailAddress(c.getCommitterIdent().getEmailAddress())) {
                try {
                    Account a = db.accounts().get(user.getAccountId());
                    if (a != null && Strings.isNullOrEmpty(a.getFullName())) {
                        a.setFullName(c.getCommitterIdent().getName());
                        db.accounts().update(Collections.singleton(a));
                        user.getAccount().setFullName(a.getFullName());
                        accountCache.evict(a.getId());
                    }
                } catch (OrmException e) {
                    logWarn("Cannot default full_name", e);
                } finally {
                    defaultName = false;
                }
            }
        }
        logDebug("Validated {} new commits", i);
    } catch (IOException err) {
        cmd.setResult(REJECTED_MISSING_OBJECT);
        logError("Invalid pack upload; one or more objects weren't sent", err);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Ref(org.eclipse.jgit.lib.Ref) RevObject(org.eclipse.jgit.revwalk.RevObject) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

Account (com.google.gerrit.reviewdb.client.Account)75 ArrayList (java.util.ArrayList)13 OrmException (com.google.gwtorm.server.OrmException)11 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)10 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)10 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)8 HashSet (java.util.HashSet)8 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)7 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)7 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)7 PersonIdent (org.eclipse.jgit.lib.PersonIdent)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)6 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)6 CurrentUser (com.google.gerrit.server.CurrentUser)6 Ref (org.eclipse.jgit.lib.Ref)6 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)4