Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #18 from daschJulia/28733-no_external_mials_with_c…
Browse files Browse the repository at this point in the history
…ontargo

add domain validator
  • Loading branch information
vanherpen authored Sep 26, 2019
2 parents 9203ecc + 09763f7 commit 90457a7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.contargo.types.contactinfo.validation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;


/**
* @author Marius van Herpen - [email protected]
* @author Julia Dasch - [email protected]
*/
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = SupportedDomainValidator.class)
public @interface SupportedDomain {

String message() default "{constraint.violation.invaliddomain}";


String[] domains() default { "contargo", "dit-duisburg" };


Class<?>[] groups() default {};


Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.contargo.types.contactinfo.validation;

import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
* This validator checks the domain of the email address against a configured list of domain which aren't allowed. The
* TLD is ignored as we basically want to prevent external users to switch their email address to one that belongs to
* our own company (contargo, dit-duisburg).
*
* @author Marius van Herpen - [email protected]
* @author Julia Dasch - [email protected]
*/
public class SupportedDomainValidator implements ConstraintValidator<SupportedDomain, String> {

private List<String> domains;

@Override
public void initialize(SupportedDomain constraintAnnotation) {

this.domains = Arrays.asList(constraintAnnotation.domains());
}


@Override
public boolean isValid(String value, ConstraintValidatorContext context) {

boolean isValid = true;

if (value == null || value.isEmpty()) {
// is checked else where in the separate user completeness validators
return true;
}

if (!value.contains("@") || !value.contains(".")) {
return false;
}

int endIndex = value.lastIndexOf('.');
int beginIndex = value.lastIndexOf('@') + 1;

if (beginIndex > endIndex) {
isValid = false;
}

String domain = value.substring(beginIndex, endIndex);
domain = StringUtils.lowerCase(domain);

if (domains.stream().anyMatch(domain::contains)) {
isValid = false;
}

return isValid;
}
}

0 comments on commit 90457a7

Please sign in to comment.