mirror of
https://github.com/google/nomulus
synced 2026-09-12 19:16:25 +00:00
Validate provided email addresses when creating a Registrar
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=240574585
This commit is contained in:
@@ -16,6 +16,7 @@ package google.registry.model.registrar;
|
||||
|
||||
import static com.google.common.base.MoreObjects.firstNonNull;
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Strings.emptyToNull;
|
||||
import static com.google.common.base.Strings.nullToEmpty;
|
||||
import static com.google.common.collect.ImmutableMap.toImmutableMap;
|
||||
@@ -84,6 +85,8 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
@@ -828,7 +831,7 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
|
||||
}
|
||||
|
||||
public Builder setEmailAddress(String emailAddress) {
|
||||
getInstance().emailAddress = emailAddress;
|
||||
getInstance().emailAddress = checkValidEmail(emailAddress);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -848,7 +851,7 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
|
||||
}
|
||||
|
||||
public Builder setIcannReferralEmail(String icannReferralEmail) {
|
||||
getInstance().icannReferralEmail = icannReferralEmail;
|
||||
getInstance().icannReferralEmail = checkValidEmail(icannReferralEmail);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -891,6 +894,20 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
|
||||
}
|
||||
}
|
||||
|
||||
/** Verifies that the email address in question is not null and has a valid format. */
|
||||
static String checkValidEmail(String email) {
|
||||
checkNotNull(email, "Provided email was null");
|
||||
try {
|
||||
InternetAddress internetAddress = new InternetAddress(email, true);
|
||||
internetAddress.validate();
|
||||
} catch (AddressException e) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Provided email %s is not a valid email address", email));
|
||||
}
|
||||
return email;
|
||||
}
|
||||
|
||||
|
||||
/** Loads all registrar entities directly from Datastore. */
|
||||
public static Iterable<Registrar> loadAll() {
|
||||
return ImmutableList.copyOf(ofy().load().type(Registrar.class).ancestor(getCrossTldKey()));
|
||||
|
||||
Reference in New Issue
Block a user