Advertisement
chrisasl

UserStateBean

Aug 31st, 2014
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. package com.uoadi.users.user;
  2.  
  3. import com.uoadi.users.user.entities.UserEntity;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6.  
  7. import javax.annotation.PostConstruct;
  8. import javax.ejb.EJB;
  9. import javax.faces.application.FacesMessage;
  10. import javax.faces.bean.ManagedBean;
  11. import javax.faces.bean.SessionScoped;
  12. import javax.faces.context.FacesContext;
  13. import java.io.Serializable;
  14.  
  15.  
  16. @ManagedBean
  17. @SessionScoped
  18. public class UserStateBean implements Serializable {
  19.     private static final long serialVersionUID = -4560687049109606063L;
  20.     static final Logger LOG = LoggerFactory.getLogger(UserStateBean.class);
  21.     private String username;
  22.     private String password;
  23.     private UserEntity current;
  24.     @EJB
  25.     private UserService userService;
  26.  
  27.     // -------------------------------------------------
  28.     // UserService logic
  29.     /**
  30.      * Attempts to login the user by searching to the DB
  31.      * If no user was found, an error is displayed
  32.      * Otherwise, the user is redirected to a page
  33.      * depending on their role in the application
  34.      * @return The outcome string
  35.      */
  36.     public String login() {
  37.         current = userService.find(username, password);
  38.         if (current == null ) {
  39.             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
  40.                     "error msg", ""));
  41.             return (username = password = null);
  42.         }
  43.         else {
  44.             // Found user, now where to redirect them
  45.             return userService.handleRedirectAfterLogin(current);
  46.         }
  47.     }
  48.  
  49.     public String logout() {
  50.         FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
  51.         return "index.xhtml?faces-redirect=true";
  52.     }
  53.  
  54.     public boolean isLoggedIn() {
  55.         return current != null;
  56.     }
  57.  
  58.     public boolean isAdmin() { return userService.isAdminDoubleVerification(current);}
  59.  
  60.     /**
  61.      * Return true if the user is of role "role"
  62.      * @param role The role that the user will be checked for
  63.      * @return True if the user's role is "role"
  64.      */
  65.     public boolean isRole(String role) {
  66.         if (current == null)
  67.             return false;
  68.         else {
  69.             if (role.equals("buyer"))
  70.                 return current.getIsBuyer() == 1;
  71.             else if (role.equals("salesman"))
  72.                 return current.getIsSalesman() == 1;
  73.             else
  74.                 return false;
  75.         }
  76.     }
  77.     // -------------------------------------------------
  78.     // Needed for Serializable
  79.     @Override
  80.     public boolean equals(Object o) {
  81.         if (this == o) return true;
  82.         if (!(o instanceof UserStateBean)) return false;
  83.  
  84.         UserStateBean that = (UserStateBean) o;
  85.  
  86.         if (current != null ? !current.equals(that.current) : that.current != null) return false;
  87.         if (password != null ? !password.equals(that.password) : that.password != null) return false;
  88.         if (!userService.equals(that.userService)) return false;
  89.         if (username != null ? !username.equals(that.username) : that.username != null) return false;
  90.  
  91.         return true;
  92.     }
  93.  
  94.     @Override
  95.     public int hashCode() {
  96.         int result = username != null ? username.hashCode() : 0;
  97.         result = 31 * result + (password != null ? password.hashCode() : 0);
  98.         result = 31 * result + (current != null ? current.hashCode() : 0);
  99.         result = 31 * result + userService.hashCode();
  100.         return result;
  101.     }
  102.  
  103.     // -------------------------------------------------
  104.     // Getters/ Setters
  105.     public String getUsername() {
  106.         return username;
  107.     }
  108.  
  109.     public void setUsername(String username) {
  110.         this.username = username;
  111.     }
  112.  
  113.     public String getPassword() {
  114.         return password;
  115.     }
  116.  
  117.     public void setPassword(String password) {
  118.         this.password = password;
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement