This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from daschJulia/28733-no_external_mials_with_c…
…ontargo add domain validator
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/net/contargo/types/contactinfo/validation/SupportedDomain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/net/contargo/types/contactinfo/validation/SupportedDomainValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |