use of com.google.gerrit.exceptions.EmailException 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.exceptions.EmailException in project gerrit by GerritCodeReview.
the class DeleteSshKey method apply.
@Override
public Response<?> apply(AccountResource.SshKey rsrc, Input input) throws AuthException, RepositoryNotFoundException, IOException, ConfigInvalidException, PermissionBackendException {
if (!self.get().hasSameAccountId(rsrc.getUser())) {
permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
}
IdentifiedUser user = rsrc.getUser();
authorizedKeys.deleteKey(user.getAccountId(), rsrc.getSshKey().seq());
try {
deleteKeySenderFactory.create(user, rsrc.getSshKey()).send();
} catch (EmailException e) {
logger.atSevere().withCause(e).log("Cannot send SSH key deletion message to %s", user.getAccount().preferredEmail());
}
user.getUserName().ifPresent(sshKeyCache::evict);
return Response.none();
}
Aggregations