Search in sources :

Example 11 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class CreateEmail method apply.

/**
 * To be used from plugins that want to create emails without permission checks.
 */
@UsedAt(UsedAt.Project.PLUGIN_SERVICEUSER)
public EmailInfo apply(IdentifiedUser user, IdString id, EmailInput input) throws RestApiException, EmailException, MethodNotAllowedException, IOException, ConfigInvalidException, PermissionBackendException {
    String email = id.get().trim();
    if (input == null) {
        input = new EmailInput();
    }
    if (input.email != null && !email.equals(input.email)) {
        throw new BadRequestException("email address must match URL");
    }
    if (!validator.isValid(email)) {
        throw new BadRequestException("invalid email address");
    }
    EmailInfo info = new EmailInfo();
    info.email = email;
    if (input.noConfirmation || isDevMode) {
        if (isDevMode) {
            logger.atWarning().log("skipping email validation in developer mode");
        }
        try {
            accountManager.link(user.getAccountId(), authRequestFactory.createForEmail(email));
        } catch (AccountException e) {
            throw new ResourceConflictException(e.getMessage());
        }
        if (input.preferred) {
            putPreferred.apply(new AccountResource.Email(user, email), null);
            info.preferred = true;
        }
    } else {
        try {
            RegisterNewEmailSender emailSender = registerNewEmailFactory.create(email);
            if (!emailSender.isAllowed()) {
                throw new MethodNotAllowedException("Not allowed to add email address " + email);
            }
            emailSender.setMessageId(messageIdGenerator.fromAccountUpdate(user.getAccountId()));
            emailSender.send();
            info.pendingConfirmation = true;
        } catch (EmailException | RuntimeException e) {
            logger.atSevere().withCause(e).log("Cannot send email verification message to %s", email);
            throw e;
        }
    }
    return info;
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) IdString(com.google.gerrit.extensions.restapi.IdString) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountResource(com.google.gerrit.server.account.AccountResource) AccountException(com.google.gerrit.server.account.AccountException) RegisterNewEmailSender(com.google.gerrit.server.mail.send.RegisterNewEmailSender) EmailException(com.google.gerrit.exceptions.EmailException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) EmailInfo(com.google.gerrit.extensions.common.EmailInfo) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) UsedAt(com.google.gerrit.common.UsedAt)

Example 12 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class AccountIT method searchForSecondaryEmailRequiresModifyAccountPermission.

@Test
public void searchForSecondaryEmailRequiresModifyAccountPermission() throws Exception {
    String email = "preferred@example.com";
    TestAccount foo = accountCreator.create(name("foo"), email, "Foo", null);
    String secondaryEmail = "secondary@example.com";
    EmailInput input = newEmailInput(secondaryEmail);
    gApi.accounts().id(foo.id().get()).addEmail(input);
    requestScopeOperations.setApiUser(user.id());
    assertThrows(ResourceNotFoundException.class, () -> gApi.accounts().id("secondary"));
    requestScopeOperations.setApiUser(admin.id());
    assertThat(gApi.accounts().id("secondary").get()._accountId).isEqualTo(foo.id().get());
}
Also used : PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) TestAccount(com.google.gerrit.acceptance.TestAccount) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 13 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class AccountIT method getOwnDetail.

@Test
public void getOwnDetail() throws Exception {
    String email = "preferred@example.com";
    String name = "Foo";
    String username = name("foo");
    TestAccount foo = accountCreator.create(username, email, name, null);
    String secondaryEmail = "secondary@example.com";
    EmailInput input = newEmailInput(secondaryEmail);
    gApi.accounts().id(foo.id().get()).addEmail(input);
    String status = "OOO";
    gApi.accounts().id(foo.id().get()).setStatus(status);
    requestScopeOperations.setApiUser(foo.id());
    AccountDetailInfo detail = gApi.accounts().id(foo.id().get()).detail();
    assertThat(detail._accountId).isEqualTo(foo.id().get());
    assertThat(detail.name).isEqualTo(name);
    assertThat(detail.username).isEqualTo(username);
    assertThat(detail.email).isEqualTo(email);
    assertThat(detail.secondaryEmails).containsExactly(secondaryEmail);
    assertThat(detail.status).isEqualTo(status);
    assertThat(detail.registeredOn.getTime()).isEqualTo(getAccount(foo.id()).registeredOn().toEpochMilli());
    assertThat(detail.inactive).isNull();
    assertThat(detail._moreAccounts).isNull();
}
Also used : AccountDetailInfo(com.google.gerrit.extensions.common.AccountDetailInfo) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) TestAccount(com.google.gerrit.acceptance.TestAccount) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 14 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class AccountIT method newEmailInput.

private EmailInput newEmailInput(String email, boolean noConfirmation) {
    EmailInput input = new EmailInput();
    input.email = email;
    input.noConfirmation = noConfirmation;
    return input;
}
Also used : EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput)

Example 15 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class AccountIT method addInvalidEmail.

@Test
public void addInvalidEmail() throws Exception {
    List<String> emails = ImmutableList.of(// Missing domain part
    "new.email", // Missing domain part
    "new.email@", // Missing user part
    "@example.com", // Non-supported TLD  (see tlds-alpha-by-domain.txt)
    "new.email@example.africa");
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        for (String email : emails) {
            EmailInput input = newEmailInput(email);
            BadRequestException thrown = assertThrows(BadRequestException.class, () -> gApi.accounts().self().addEmail(input));
            assertWithMessage(email).that(thrown).hasMessageThat().isEqualTo("invalid email address");
        }
        accountIndexedCounter.assertNoReindex();
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

EmailInput (com.google.gerrit.extensions.api.accounts.EmailInput)24 Test (org.junit.Test)19 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)18 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)16 TestAccount (com.google.gerrit.acceptance.TestAccount)9 AccountIndexedCounter (com.google.gerrit.acceptance.AccountIndexedCounter)7 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)7 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)4 AccountDetailInfo (com.google.gerrit.extensions.common.AccountDetailInfo)4 EmailInfo (com.google.gerrit.extensions.common.EmailInfo)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)2 Sandboxed (com.google.gerrit.acceptance.Sandboxed)2 EmailException (com.google.gerrit.exceptions.EmailException)2 StopStrategies (com.github.rholder.retry.StopStrategies)1 FluentIterable (com.google.common.collect.FluentIterable)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1