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