a2dca8bf6e6edabcec56ea1f6f5895cc1d94e0ab
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / data / validator / PasswordsPolicyValidator.java
1 /**
2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.vaadin.data.validator;
10
11 import java.util.List;
12 import java.util.stream.Collectors;
13
14 import com.vaadin.data.Validator;
15 import com.vaadin.ui.PasswordField;
16
17 import eu.etaxonomy.cdm.validation.constraint.ValidPasswordValidator;
18 import eu.etaxonomy.cdm.validation.constraint.ValidPasswordValidator.PasswordRulesValidator;
19
20 /**
21 * Checks that the passwords entered in two password fields match.
22 * Intended to be added as validator to the second field of the
23 * {@link PasswordField} pair.
24 *
25 * @author a.kohlbecker
26 * @since Nov 12, 2021
27 */
28 public class PasswordsPolicyValidator implements Validator {
29
30 private static final long serialVersionUID = -9048318480638222817L;
31
32 private PasswordRulesValidator validator = new ValidPasswordValidator.PasswordRulesValidator();
33
34 @Override
35 public void validate(Object value) throws InvalidValueException {
36 String password = (String)value;
37 if(password != null && !password.isEmpty()) {
38 List<String> violationMessages = validator.validateUserPassword(password);
39 if(!violationMessages.isEmpty()) {
40 throw new InvalidValueException(violationMessages.stream().collect(Collectors.joining(" ")));
41 }
42 }
43 }
44
45
46
47 }