Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package web_projekt;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.ejb.EJB;
  6. import javax.faces.application.FacesMessage;
  7. import javax.faces.bean.ManagedBean;
  8. import javax.faces.context.FacesContext;
  9. import javax.servlet.http.HttpSession;
  10.  
  11. import projekt.dao.UserDAO;
  12. import projekt.entities.User;
  13.  
  14.  
  15. @ManagedBean
  16. public class LoginBB {
  17.     private static final String PAGE_MAIN = "taskList?faces-redirect=true";
  18.     private static final String PAGE_LOGIN = "login";
  19.     private static final String PAGE_STAY = null;
  20.    
  21.     private String login;
  22.     private String password;
  23.    
  24.     @EJB
  25.     UserDAO userDAO;
  26.    
  27.     public String getLogin() {
  28.         return login;
  29.     }
  30.    
  31.     public void setLogin(String login) {
  32.         this.login = login;
  33.     }
  34.    
  35.     public String getPassword() {
  36.         return password;
  37.     }
  38.    
  39.     public void setPassword(String password) {
  40.         this.password = password;
  41.     }
  42.    
  43.     public boolean validateData() {
  44.         boolean result = true;
  45.         FacesContext ctx = FacesContext.getCurrentInstance();
  46.        
  47.         if (login == null || login.length() == 0) {
  48.             ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Podaj login", "null"));
  49.         }
  50.        
  51.         if (password == null || password.length() == 0) {
  52.             ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Podaj hasło", "null"));
  53.         }
  54.        
  55.         if (!ctx.getMessageList().isEmpty()) {
  56.             result = false;
  57.         }
  58.        
  59.         return result;
  60.     }
  61.    
  62.     public String doLogin() {
  63.         FacesContext ctx = FacesContext.getCurrentInstance();
  64.         User user = null;
  65.        
  66.         if (!validateData()) {
  67.             return PAGE_STAY;
  68.         }
  69.        
  70.         if (login.length() < 1) {
  71.             ctx.addMessage(null, new FacesMessage(
  72.                     FacesMessage.SEVERITY_ERROR, "NIE MA LOGINU", null
  73.                     ));
  74.             return PAGE_STAY;
  75.         }
  76.         if (password.length() < 1) {
  77.             ctx.addMessage(null, new FacesMessage(
  78.                     FacesMessage.SEVERITY_ERROR, "NIE MA HASŁA", null
  79.                     ));
  80.             return PAGE_STAY;
  81.         }
  82.        
  83.         user = getUserFromDB(login, password);
  84.        
  85.         if (user == null) {
  86.             ctx.addMessage(null, new FacesMessage(
  87.                     FacesMessage.SEVERITY_ERROR, "Niepoprawne dane logowania.", null
  88.                     ));
  89.            
  90.             return PAGE_STAY;
  91.         }
  92.        
  93.         HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);
  94.         session.setAttribute("userSession", user);
  95.        
  96.         return PAGE_MAIN;
  97.     }
  98.    
  99.     public User getUser() {
  100.         HttpSession session = (HttpSession)
  101.                 FacesContext.getCurrentInstance().getExternalContext().getSession(true);
  102.        
  103.         return (User) session.getAttribute("userSession");
  104.     }
  105.    
  106.     public String doLogout() {
  107.         HttpSession session = (HttpSession)
  108.                 FacesContext.getCurrentInstance().getExternalContext().getSession(true);
  109.         session.invalidate();
  110.        
  111.         return PAGE_LOGIN;
  112.     }
  113.    
  114.     public User getUserFromDB(String login, String password) {
  115.         List<User> users = userDAO.getFullList();
  116.         User user = null;
  117.        
  118.         for (User list_user : users) {
  119.             if ((list_user.getLoginUser().equals(login))
  120.                     && list_user.getPasswordUser().equals(password)) {
  121.                 user = list_user;
  122.             }
  123.         }
  124.        
  125.         return user;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement