Search in sources :

Example 1 with EmailInfo

use of com.google.gerrit.extensions.common.EmailInfo in project gerrit by GerritCodeReview.

the class GetEmail method apply.

@Override
public EmailInfo apply(AccountResource.Email rsrc) {
    EmailInfo e = new EmailInfo();
    e.email = rsrc.getEmail();
    e.preferred(rsrc.getUser().getAccount().getPreferredEmail());
    return e;
}
Also used : EmailInfo(com.google.gerrit.extensions.common.EmailInfo)

Example 2 with EmailInfo

use of com.google.gerrit.extensions.common.EmailInfo in project gerrit by GerritCodeReview.

the class GetEmails method apply.

@Override
public List<EmailInfo> apply(AccountResource rsrc) {
    List<EmailInfo> emails = new ArrayList<>();
    for (String email : rsrc.getUser().getEmailAddresses()) {
        if (email != null) {
            EmailInfo e = new EmailInfo();
            e.email = email;
            e.preferred(rsrc.getUser().getAccount().getPreferredEmail());
            emails.add(e);
        }
    }
    Collections.sort(emails, new Comparator<EmailInfo>() {

        @Override
        public int compare(EmailInfo a, EmailInfo b) {
            return a.email.compareTo(b.email);
        }
    });
    return emails;
}
Also used : ArrayList(java.util.ArrayList) EmailInfo(com.google.gerrit.extensions.common.EmailInfo)

Example 3 with EmailInfo

use of com.google.gerrit.extensions.common.EmailInfo in project gerrit by GerritCodeReview.

the class CreateEmail method apply.

public Response<EmailInfo> apply(IdentifiedUser user, EmailInput input) throws AuthException, BadRequestException, ResourceConflictException, ResourceNotFoundException, OrmException, EmailException, MethodNotAllowedException, IOException, ConfigInvalidException, PermissionBackendException {
    if (input.email != null && !email.equals(input.email)) {
        throw new BadRequestException("email address must match URL");
    }
    EmailInfo info = new EmailInfo();
    info.email = email;
    if (input.noConfirmation || isDevMode) {
        if (isDevMode) {
            log.warn("skipping email validation in developer mode");
        }
        try {
            accountManager.link(user.getAccountId(), AuthRequest.forEmail(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 sender = registerNewEmailFactory.create(email);
            if (!sender.isAllowed()) {
                throw new MethodNotAllowedException("Not allowed to add email address " + email);
            }
            sender.send();
            info.pendingConfirmation = true;
        } catch (EmailException | RuntimeException e) {
            log.error("Cannot send email verification message to " + email, e);
            throw e;
        }
    }
    return Response.created(info);
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) RegisterNewEmailSender(com.google.gerrit.server.mail.send.RegisterNewEmailSender) EmailException(com.google.gerrit.common.errors.EmailException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) EmailInfo(com.google.gerrit.extensions.common.EmailInfo)

Example 4 with EmailInfo

use of com.google.gerrit.extensions.common.EmailInfo in project gerrit by GerritCodeReview.

the class EmailIT method emailApi.

@Test
public void emailApi() throws Exception {
    String email = "foo@example.com";
    assertThat(getEmails()).doesNotContain(email);
    // Create email
    EmailInput emailInput = new EmailInput();
    emailInput.email = email;
    emailInput.noConfirmation = true;
    gApi.accounts().self().createEmail(emailInput);
    assertThat(getEmails()).contains(email);
    assertThat(gApi.accounts().self().get().email).isNotEqualTo(email);
    // Get email
    requestScopeOperations.resetCurrentApiUser();
    EmailApi emailApi = gApi.accounts().self().email(email);
    EmailInfo emailInfo = emailApi.get();
    assertThat(emailInfo.email).isEqualTo(email);
    assertThat(emailInfo.preferred).isNull();
    assertThat(emailInfo.pendingConfirmation).isNull();
    // Set as preferred email
    emailApi.setPreferred();
    assertThat(gApi.accounts().self().get().email).isEqualTo(email);
    // Get email again (now it's the preferred email)
    requestScopeOperations.resetCurrentApiUser();
    emailApi = gApi.accounts().self().email(email);
    emailInfo = emailApi.get();
    assertThat(emailInfo.email).isEqualTo(email);
    assertThat(emailInfo.preferred).isTrue();
    assertThat(emailInfo.pendingConfirmation).isNull();
    // Delete email
    emailApi.delete();
    assertThat(getEmails()).doesNotContain(email);
    // Now the email is no longer found
    requestScopeOperations.resetCurrentApiUser();
    assertThrows(ResourceNotFoundException.class, () -> gApi.accounts().self().email(email).get());
}
Also used : EmailApi(com.google.gerrit.extensions.api.accounts.EmailApi) EmailInfo(com.google.gerrit.extensions.common.EmailInfo) IdString(com.google.gerrit.extensions.restapi.IdString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with EmailInfo

use of com.google.gerrit.extensions.common.EmailInfo in project gerrit by GerritCodeReview.

the class EmailIT method getEmails.

private Set<String> getEmails() throws Exception {
    RestResponse r = adminRestSession.get("/accounts/self/emails");
    r.assertOK();
    List<EmailInfo> emails = newGson().fromJson(r.getReader(), new TypeToken<List<EmailInfo>>() {
    }.getType());
    return emails.stream().map(e -> e.email).collect(toSet());
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) ExternalIdKeyFactory(com.google.gerrit.server.account.externalids.ExternalIdKeyFactory) TypeToken(com.google.gson.reflect.TypeToken) IdString(com.google.gerrit.extensions.restapi.IdString) Inject(com.google.inject.Inject) RestResponse(com.google.gerrit.acceptance.RestResponse) Multimap(com.google.common.collect.Multimap) AnonymousCowardName(com.google.gerrit.server.config.AnonymousCowardName) CanonicalWebUrl(com.google.gerrit.server.config.CanonicalWebUrl) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) ExternalIdFactory(com.google.gerrit.server.account.externalids.ExternalIdFactory) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) Collectors.toSet(java.util.stream.Collectors.toSet) EnablePeerIPInReflogRecord(com.google.gerrit.server.config.EnablePeerIPInReflogRecord) AccountsUpdate(com.google.gerrit.server.account.AccountsUpdate) Account(com.google.gerrit.entities.Account) EmailExpander(com.google.gerrit.server.account.EmailExpander) Set(java.util.Set) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) RequestScopeOperations(com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations) Provider(com.google.inject.Provider) List(java.util.List) Emails(com.google.gerrit.server.account.Emails) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) DefaultRealm(com.google.gerrit.server.account.DefaultRealm) Context(com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context) ServerInitiated(com.google.gerrit.server.ServerInitiated) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Optional(java.util.Optional) GerritJUnit.assertThrows(com.google.gerrit.testing.GerritJUnit.assertThrows) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AuthConfig(com.google.gerrit.server.config.AuthConfig) EmailApi(com.google.gerrit.extensions.api.accounts.EmailApi) Realm(com.google.gerrit.server.account.Realm) EmailInfo(com.google.gerrit.extensions.common.EmailInfo) RestResponse(com.google.gerrit.acceptance.RestResponse) TypeToken(com.google.gson.reflect.TypeToken) EmailInfo(com.google.gerrit.extensions.common.EmailInfo)

Aggregations

EmailInfo (com.google.gerrit.extensions.common.EmailInfo)9 EmailInput (com.google.gerrit.extensions.api.accounts.EmailInput)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)4 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 IdString (com.google.gerrit.extensions.restapi.IdString)3 Test (org.junit.Test)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Truth8.assertThat (com.google.common.truth.Truth8.assertThat)2 RequestScopeOperations (com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations)2 Account (com.google.gerrit.entities.Account)2 EmailApi (com.google.gerrit.extensions.api.accounts.EmailApi)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ServerInitiated (com.google.gerrit.server.ServerInitiated)2 AccountsUpdate (com.google.gerrit.server.account.AccountsUpdate)2 Emails (com.google.gerrit.server.account.Emails)2 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)2 ExternalIdFactory (com.google.gerrit.server.account.externalids.ExternalIdFactory)2 ExternalIdKeyFactory (com.google.gerrit.server.account.externalids.ExternalIdKeyFactory)2 ExternalIds (com.google.gerrit.server.account.externalids.ExternalIds)2