Search in sources :

Example 71 with CurrentUser

use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.

the class ThreadLocalRequestContext method module.

public static Module module() {
    return new AbstractModule() {

        @Override
        protected void configure() {
            bind(ThreadLocalRequestContext.class);
            bind(RequestContext.class).annotatedWith(Names.named(FALLBACK)).to(FallbackRequestContext.class);
        }

        @Provides
        RequestContext provideRequestContext(@Named(FALLBACK) RequestContext fallback) {
            return MoreObjects.firstNonNull(local.get(), fallback);
        }

        @Provides
        CurrentUser provideCurrentUser(RequestContext ctx) {
            return ctx.getUser();
        }

        @Provides
        IdentifiedUser provideCurrentUser(CurrentUser user) {
            if (user.isIdentifiedUser()) {
                return user.asIdentifiedUser();
            }
            throw new ProvisionException(NotSignedInException.MESSAGE, new NotSignedInException());
        }
    };
}
Also used : Named(com.google.inject.name.Named) ProvisionException(com.google.inject.ProvisionException) CurrentUser(com.google.gerrit.server.CurrentUser) NotSignedInException(com.google.gerrit.exceptions.NotSignedInException) AbstractModule(com.google.inject.AbstractModule)

Example 72 with CurrentUser

use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.

the class ChangeNotesTest method copiedApprovals_withRealUserAndTag_keepsUUID.

@Test
public void copiedApprovals_withRealUserAndTag_keepsUUID() throws Exception {
    ImmutableList<String> strangeTags = ImmutableList.of(", ", ":\"", ",", "!@#$%^\0&*):\" \n: \r\"#$@,. :");
    for (String strangeTag : strangeTags) {
        Change c = newChange();
        CurrentUser otherUserAsOwner = userFactory.runAs(null, changeOwner.getAccountId(), otherUser);
        ChangeUpdate update = newUpdate(c, otherUserAsOwner);
        update.putApproval(LabelId.CODE_REVIEW, (short) 2);
        update.setTag(strangeTag);
        update.commit();
        ChangeNotes notes = newNotes(c);
        PatchSetApproval originalPsa = Iterables.getOnlyElement(notes.getApprovals().get(c.currentPatchSetId()));
        assertThat(originalPsa.accountId()).isEqualTo(changeOwner.getAccountId());
        assertThat(originalPsa.label()).isEqualTo(LabelId.CODE_REVIEW);
        assertThat(originalPsa.value()).isEqualTo(2);
        assertThat(originalPsa.tag()).hasValue(NoteDbUtil.sanitizeFooter(strangeTag));
        assertThat(originalPsa.realAccountId()).isEqualTo(otherUserId);
        assertParsedUuid(originalPsa);
        // Copied approvals are persisted at the patch set upload, add new patch set
        incrementPatchSet(c);
        addCopiedApproval(c, changeOwner, originalPsa);
        notes = newNotes(c);
        assertThat(notes.getApprovalsWithCopied().keySet()).hasSize(2);
        PatchSetApproval copiedApproval = Iterables.getOnlyElement(notes.getApprovalsWithCopied().get(c.currentPatchSetId()).stream().filter(a -> a.copied()).collect(toImmutableList()));
        PatchSetApproval nonCopiedApproval = Iterables.getOnlyElement(notes.getApprovalsWithCopied().get(originalPsa.patchSetId()).stream().filter(a -> !a.copied()).collect(toImmutableList()));
        // Still same original PSA is returned
        assertThat(nonCopiedApproval).isEqualTo(originalPsa);
        // The copied approval matches the original approval, including UUID
        assertCopiedApproval(originalPsa, copiedApproval);
    }
}
Also used : CurrentUser(com.google.gerrit.server.CurrentUser) Change(com.google.gerrit.entities.Change) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) Test(org.junit.Test)

Example 73 with CurrentUser

use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.

the class ChangeNotesTest method realUser.

@Test
public void realUser() throws Exception {
    Change c = newChange();
    CurrentUser ownerAsOtherUser = userFactory.runAs(null, otherUserId, changeOwner);
    ChangeUpdate update = newUpdate(c, ownerAsOtherUser);
    update.setChangeMessage("Message on behalf of other user");
    update.commit();
    ChangeMessage msg = Iterables.getLast(newNotes(c).getChangeMessages());
    assertThat(msg.getMessage()).isEqualTo("Message on behalf of other user");
    assertThat(msg.getAuthor()).isEqualTo(otherUserId);
    assertThat(msg.getRealAuthor()).isEqualTo(changeOwner.getAccountId());
}
Also used : CurrentUser(com.google.gerrit.server.CurrentUser) ChangeMessage(com.google.gerrit.entities.ChangeMessage) Change(com.google.gerrit.entities.Change) Test(org.junit.Test)

Aggregations

CurrentUser (com.google.gerrit.server.CurrentUser)73 AuthException (com.google.gerrit.extensions.restapi.AuthException)21 Account (com.google.gerrit.reviewdb.client.Account)11 Account (com.google.gerrit.entities.Account)10 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)10 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)10 ChangeData (com.google.gerrit.server.query.change.ChangeData)10 Change (com.google.gerrit.entities.Change)9 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)9 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)9 IOException (java.io.IOException)9 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)8 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)8 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)8 Provider (com.google.inject.Provider)8 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Test (org.junit.Test)6 PatchSet (com.google.gerrit.entities.PatchSet)5