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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package authentification;
import javax.ejb.EJB;
import org.jpa.entities.Utilisateur;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@ManagedBean
@SessionScoped
public class loginBean {
@PersistenceContext
private EntityManager em;
private String login;
private String password;
private String nom;
private String prenom;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String validateUser() {
FacesContext context = FacesContext.getCurrentInstance();
Utilisateur user = getUser();
if (user != null) {
if (!user.getUtMotDePasse().equals(password)) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Login Failed!",
"The password specified is not correct.");
context.addMessage(null, message);
return null;
}
context.getExternalContext().getSessionMap().put(USER_SESSION_KEY, user);
System.out.println(user.getUtStatut());
if ("agent".equalsIgnoreCase(user.getUtStatut().trim())) {
return "acceuilAgent?faces-redirect=true";
} else {
return "/chef_service/chefAcceuil?faces-redirect=true";
} where u define role
} else {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Login Failed!",
"Username '"
+ login
+ "' does not exist.");
context.addMessage(null, message);
return null;
}
}
@EJB
private org.jpa.sessionbean.UtilisateurFacade jpaController;
private Utilisateur getUser() {
try {
EntityManager em =jpaController.getEntityManager();
Query query = em.createNamedQuery("Utilisateur.findByUtLogin");
query.setParameter("utLogin", login);
Utilisateur user= ( Utilisateur) query.getSingleResult();
//Utilisateur user = (Utilisateur) jpaController.getEntityManager().createNamedQuery("Utilisateur.findByUtLogin").
//setParameter("utLogin", login).getSingleResult();
return user;
} catch (NoResultException nre) {
return null;
}
}
} |
Partager