| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 
 |  
@Getter
@Setter
@Entity
@UniqueCompteEmail
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Compte implements Serializable, UserDetails {
 
	private static final long serialVersionUID = -5230227676515387462L;
 
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private Integer id;
 
	@NotBlank
	@NotNull
	@Column(unique = true)
	private String username;
 
	@NotNull
	@NotBlank
	@Size(min = 6)
	private String password;
 
	@Email
	@NotNull
	@NotBlank
	@Column(unique = true)
	private String email;
 
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return new HashSet<GrantedAuthority>();
	}
 
	@Override
	public String getPassword() {
		return this.password;
	}
 
	@Override
	public String getUsername() {
		return this.username;
	}
 
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}
 
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}
 
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}
 
	@Override
	public boolean isEnabled() {
		return true;
	}
 
	public abstract CompteType getTypeCompte();
 
	public abstract void setTypeCompte(CompteType typeCompte);
 
	public static enum CompteType {
		ETUDIANT, ADMINISTRATEUR
	}
}
 
@Repository
public interface CompteRepository extends JpaRepository<Compte, Integer> {
 
	public Optional<Compte> findByUsername(String username);
 
	public Optional<Compte> findByEmail(String email);
}
 
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = UniqueCompteEmailValidator.class)
@Target({ ElementType.TYPE })
public @interface UniqueCompteEmail {
 
	String message() default "{com.mssmfactory.bacsimulator.uniquecompteemail.message}";
 
	Class<?>[] groups() default {};
 
	Class<? extends Payload>[] payload() default {};
}
 
public class UniqueCompteEmailValidator implements ConstraintValidator<UniqueCompteEmail, Compte> {
 
    @Autowired
    private CompteRepository compteRepository;
 
    @Override
    public void initialize(UniqueCompteEmail constraintAnnotation) {
    }
 
    @Override
    public boolean isValid(Compte value, ConstraintValidatorContext context) {
        if (value != null) {
            Optional<Compte> compte = this.compteRepository.findByEmail(value.getEmail());
 
            return !compte.isPresent();
        } else
            return false;
    }
}
 
@Component
public class ValidatorAddingCustomizer implements HibernatePropertiesCustomizer {
 
	@Autowired
	private ValidatorFactory validatorFactory;
 
	public void customize(Map<String, Object> hibernateProperties) {
		if (validatorFactory != null) {
			hibernateProperties.put("javax.persistence.validation.factory", validatorFactory);
		}
	}
} | 
Partager