Search in sources :

Example 11 with EmailException

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;
}
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 EmailException

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();
}
Also used : EmailException(com.google.gerrit.exceptions.EmailException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Aggregations

EmailException (com.google.gerrit.exceptions.EmailException)12 StorageException (com.google.gerrit.exceptions.StorageException)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 UsedAt (com.google.gerrit.common.UsedAt)2 Address (com.google.gerrit.entities.Address)2 EmailInput (com.google.gerrit.extensions.api.accounts.EmailInput)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 PublicKeyStore (com.google.gerrit.gpg.PublicKeyStore)2 PublicKeyStore.keyIdToString (com.google.gerrit.gpg.PublicKeyStore.keyIdToString)2 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)2 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)2 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)2 IOException (java.io.IOException)2 AuthSMTPClient (org.apache.commons.net.smtp.AuthSMTPClient)2 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)2 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 ByteSource (com.google.common.io.ByteSource)1 Account (com.google.gerrit.entities.Account)1