1 2 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package forms;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import beans.Candidat;
import dao.CandidatDao;
import org.jasypt.util.password.ConfigurablePasswordEncryptor;
public final class ConnexionForm {
private static final String CHAMP_EMAIL = "email";
private static final String CHAMP_PASS = "pwd";
private CandidatDao candidatDao;
private String resultat;
private String echec;
private Map<String, String> erreurs = new HashMap<String,String>();
private static final String ALGO_CHIFFREMENT = "SHA-256";
public ConnexionForm( CandidatDao candidatDao ) {
this.candidatDao = candidatDao;
}
public Map<String, String> getErreurs() {
return erreurs;
}
public String getEchec() {
return echec;
}
public String getResultat() {
return resultat;
}
public Candidat connecterCandidat( HttpServletRequest request ) {
/* Récupération des champs du formulaire */
String email = getValeurChamp( request, CHAMP_EMAIL );
String userPassword = getValeurChamp( request, CHAMP_PASS );
System.out.println("lemail est :"+email);
System.out.println("le password est :"+userPassword);
Candidat candidat = new Candidat();
/* Validation du champ email. */
try {
traiterEmail( email,userPassword, candidat );
} catch ( Exception e ) {
setErreur( CHAMP_EMAIL, e.getMessage() );
}
candidat.setEmail( email );
/* Validation du champ mot de passe. */
try {
traiterPassword( userPassword, candidat );
} catch ( Exception e ) {
setErreur( CHAMP_PASS, e.getMessage() );
}
candidat.setPassword( userPassword );
/* Initialisation du résultat global de la validation. */
if ( erreurs.isEmpty() ) {
resultat = "Succès de la connexion.";
} else {
resultat = "<p class=\"resultat_echec\">Email ou mot de passe incorrect. Veuillez réessayer!</p>";
}
return candidat;
}
private void traiterPassword( String password, Candidat candidat ) {
try {
validationPassword( password );
} catch ( FormValidationException e ) {
setErreur( CHAMP_PASS, e.getMessage() );
candidat.setPassword(password);
}
}
private void traiterEmail( String email,String pwd, Candidat candidat ) {
try {
validationEmail( email,pwd );
} catch ( FormValidationException e ) {
setErreur( CHAMP_EMAIL, e.getMessage() );
}
candidat.setEmail( email );
}
private void validationEmail( String email, String pwd ) throws FormValidationException
{
ConfigurablePasswordEncryptor passwordEncryptor = new ConfigurablePasswordEncryptor();
passwordEncryptor.setAlgorithm( ALGO_CHIFFREMENT );
passwordEncryptor.setPlainDigest( false );
if ( email != null )
{
if ( !email.matches("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) )
{
throw new FormValidationException( "Merci de saisir une adresse mail valide." );
}
else
{
if(find(email)== null)
{
throw new FormValidationException( "L'adresse mail ou le mot de passe saisi est incorrect." );
}
else
{
if( passwordEncryptor.checkPassword( pwd, find(email).getPassword() ))
{
if (find(email).getActivationEmail() != null)
{
if (find(email).getStatutActivation()== null)
{
throw new FormValidationException( "Votre compte n'a pas été activé." );
}
}
else
{
System.out.println("le statut du mail est: "+find(email).getActivationEmail());
throw new FormValidationException( "Votre adresse mail n'a pas encore été validée. Veuillez cliquer sur le lien qui vous a été envoyé par mail!" );
}
}
else
{
throw new FormValidationException( "L'adresse mail ou le mot de passe saisi est incorrect." );
}
}
}
}
else
{
throw new FormValidationException( "Merci de saisir une adresse mail." );
}
}
private void validationPassword( String password ) throws FormValidationException {
if ( password == null ) {
throw new FormValidationException( "Merci d'entrer un mot de passe." );
}
}
public Candidat find(String email) {
Candidat u = null;
u = candidatDao.trouver(email);
return u;
}
/*
* Ajoute un message correspondant au champ spécifié à la map des
erreurs.
*/
private void setErreur( String champ, String message ) {
erreurs.put( champ, message );
}
/*
* Méthode utilitaire qui retourne null si un champ est vide, et son
contenu
* sinon.
*/
private static String getValeurChamp( HttpServletRequest request, String nomChamp ) {
String valeur = request.getParameter( nomChamp );
if ( valeur == null || valeur.trim().length() == 0 ) {
return null;
} else {
return valeur;
}
}
} |