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;
}
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;
}
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);
}
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());
}
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());
}
Aggregations