package fr.openwide.core.basicapp.core.security.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.vt.middleware.dictionary.ArrayWordList;
import edu.vt.middleware.dictionary.WordListDictionary;
import edu.vt.middleware.password.DictionaryRule;
import edu.vt.middleware.password.DigitCharacterRule;
import edu.vt.middleware.password.IllegalCharacterRule;
import edu.vt.middleware.password.LengthRule;
import edu.vt.middleware.password.LowercaseCharacterRule;
import edu.vt.middleware.password.NonAlphanumericCharacterRule;
import edu.vt.middleware.password.NumericalSequenceRule;
import edu.vt.middleware.password.RegexRule;
import edu.vt.middleware.password.Rule;
import edu.vt.middleware.password.UppercaseCharacterRule;
import edu.vt.middleware.password.UsernameRule;
import edu.vt.middleware.password.WhitespaceRule;
import fr.openwide.core.basicapp.core.business.user.model.User;

public class SecurityPasswordRules implements Serializable {

	private static final long serialVersionUID = -2309617143631151956L;

	private Set<Rule> rules = new HashSet<Rule>();

	public static final SecurityPasswordRules DEFAULT = new SecurityPasswordRules()
			.minMaxLength(User.MIN_PASSWORD_LENGTH, User.MAX_PASSWORD_LENGTH);

	public SecurityPasswordRules minLength(int min) {
		rules.add(new LengthRule(min, Integer.MAX_VALUE));
		return this;
	}

	public SecurityPasswordRules maxLength(int max) {
		rules.add(new LengthRule(0, max));
		return this;
	}

	public SecurityPasswordRules minMaxLength(int min, int max) {
		rules.add(new LengthRule(min, max));
		return this;
	}

	public SecurityPasswordRules mandatoryDigits(int min) {
		rules.add(new DigitCharacterRule(min));
		return this;
	}

	public SecurityPasswordRules mandatoryDigits() {
		rules.add(new DigitCharacterRule());
		return this;
	}

	public SecurityPasswordRules mandatoryNonAlphanumericCharacters() {
		rules.add(new NonAlphanumericCharacterRule());
		return this;
	}

	public SecurityPasswordRules mandatoryNonAlphanumericCharacters(int min) {
		rules.add(new NonAlphanumericCharacterRule(min));
		return this;
	}

	public SecurityPasswordRules mandatoryUpperCase() {
		rules.add(new UppercaseCharacterRule());
		return this;
	}

	public SecurityPasswordRules mandatoryUpperCase(int upperCase) {
		rules.add(new UppercaseCharacterRule(upperCase));
		return this;
	}

	public SecurityPasswordRules mandatoryLowerCase() {
		rules.add(new LowercaseCharacterRule());
		return this;
	}

	public SecurityPasswordRules mandatoryLowerCase(int minLowerCase) {
		rules.add(new LowercaseCharacterRule(minLowerCase));
		return this;
	}

	public SecurityPasswordRules mandatoryUpperLowerCase() {
		rules.add(new LowercaseCharacterRule());
		rules.add(new UppercaseCharacterRule());
		return this;
	}

	public SecurityPasswordRules mandatoryUpperLowerCase(int minUpperCase, int minLowerCase) {
		rules.add(new UppercaseCharacterRule(minUpperCase));
		rules.add(new LowercaseCharacterRule(minLowerCase));
		return this;
	}

	public SecurityPasswordRules forbiddenWhiteSpace() {
		rules.add(new WhitespaceRule());
		return this;
	}

	public SecurityPasswordRules forbiddenRegex(String regex) {
		rules.add(new RegexRule(regex));
		return this;
	}

	public SecurityPasswordRules forbiddenOrderedNumericalSequence(int sequenceMaxLength, boolean wrap) {
		rules.add(new NumericalSequenceRule(sequenceMaxLength, wrap));
		return this;
	}

	public SecurityPasswordRules forbiddenCharacters(String characters) {
		rules.add(new IllegalCharacterRule(characters.toCharArray()));
		return this;
	}

	public SecurityPasswordRules forbiddenUsername() {
		rules.add(new UsernameRule());
		return this;
	}

	public SecurityPasswordRules forbiddenUsername(boolean matchBackwards, boolean caseInsensitive) {
		rules.add(new UsernameRule(matchBackwards, caseInsensitive));
		return this;
	}

	public SecurityPasswordRules forbiddenPasswords(List<String> forbiddenPasswords) {
		return forbiddenPasswords(forbiddenPasswords, true);
	}

	public SecurityPasswordRules forbiddenPasswords(List<String> forbiddenPasswords, boolean caseInsensitive) {
		if (forbiddenPasswords == null || forbiddenPasswords.isEmpty()) {
			return this;
		}
		rules.add(new DictionaryRule(new WordListDictionary(new ArrayWordList(forbiddenPasswords.toArray(new String[forbiddenPasswords.size()]), !caseInsensitive))));
		return this;
	}

	public Set<Rule> getRules() {
		return rules;
	}

}

@@ -161,141 +161,18 @@
 ort 
-edu.vt.middleware.dictionary.ArrayWordList;%0Aimport edu.vt.middleware.dictionary.WordListDictionary;%0Aimport edu.vt.middleware.password
+org.passay
 .Dic
@@ -195,34 +195,18 @@
 ort 
-edu.vt.middleware
+org
 .pass
-word
+ay
 .Dig
@@ -233,34 +233,18 @@
 ort 
-edu.vt.middleware
+org
 .pass
-word
+ay
 .Ill
@@ -273,154 +273,94 @@
 ort 
-edu.vt.middleware.password.LengthRule;%0Aimport edu.vt.middleware.password.LowercaseCharacterRule;%0Aimport edu.vt.middleware.password.NonAlphanumeric
+org.passay.IllegalRegexRule;%0Aimport org.passay.LengthRule;%0Aimport org.passay.Lowercase
 Char
@@ -381,34 +381,18 @@
 ort 
-edu.vt.middleware
+org
 .pass
-word
+ay
 .Num
@@ -422,119 +422,82 @@
 ort 
-edu.vt.middleware.password.RegexRule;%0Aimport edu.vt.middleware.password.Rule;%0Aimport edu.vt.middleware
+org.passay.Rule;%0Aimport org.passay.SpecialCharacterRule;%0Aimport org
 .pass
-word
+ay
 .Upp
@@ -528,34 +528,18 @@
 ort 
-edu.vt.middleware
+org
 .pass
-word
+ay
 .Use
@@ -560,34 +560,18 @@
 ort 
-edu.vt.middleware
+org
 .pass
-word
+ay
 .Whi
@@ -579,24 +579,118 @@
 espaceRule;%0A
+import org.passay.dictionary.ArrayWordList;%0Aimport org.passay.dictionary.WordListDictionary;%0A%0A
 import fr.op
@@ -1733,39 +1733,31 @@
 les.add(new 
-NonAlphanumeric
+Special
 CharacterRul
@@ -1873,31 +1873,23 @@
 add(new 
-NonAlphanumeric
+Special
 Characte
@@ -2979,24 +2979,24 @@
 ng regex) %7B%0A
-
 %09%09rules.add(
@@ -2999,16 +2999,23 @@
 add(new 
+Illegal
 RegexRul

passage de la basic-application sur passay