Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.uoadi.users.user;
- import com.uoadi.users.user.entities.UserEntity;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.annotation.PostConstruct;
- import javax.ejb.EJB;
- import javax.faces.application.FacesMessage;
- import javax.faces.bean.ManagedBean;
- import javax.faces.bean.SessionScoped;
- import javax.faces.context.FacesContext;
- import java.io.Serializable;
- @ManagedBean
- @SessionScoped
- public class UserStateBean implements Serializable {
- private static final long serialVersionUID = -4560687049109606063L;
- static final Logger LOG = LoggerFactory.getLogger(UserStateBean.class);
- private String username;
- private String password;
- private UserEntity current;
- @EJB
- private UserService userService;
- // -------------------------------------------------
- // UserService logic
- /**
- * Attempts to login the user by searching to the DB
- * If no user was found, an error is displayed
- * Otherwise, the user is redirected to a page
- * depending on their role in the application
- * @return The outcome string
- */
- public String login() {
- current = userService.find(username, password);
- if (current == null ) {
- FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
- "error msg", ""));
- return (username = password = null);
- }
- else {
- // Found user, now where to redirect them
- return userService.handleRedirectAfterLogin(current);
- }
- }
- public String logout() {
- FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
- return "index.xhtml?faces-redirect=true";
- }
- public boolean isLoggedIn() {
- return current != null;
- }
- public boolean isAdmin() { return userService.isAdminDoubleVerification(current);}
- /**
- * Return true if the user is of role "role"
- * @param role The role that the user will be checked for
- * @return True if the user's role is "role"
- */
- public boolean isRole(String role) {
- if (current == null)
- return false;
- else {
- if (role.equals("buyer"))
- return current.getIsBuyer() == 1;
- else if (role.equals("salesman"))
- return current.getIsSalesman() == 1;
- else
- return false;
- }
- }
- // -------------------------------------------------
- // Needed for Serializable
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof UserStateBean)) return false;
- UserStateBean that = (UserStateBean) o;
- if (current != null ? !current.equals(that.current) : that.current != null) return false;
- if (password != null ? !password.equals(that.password) : that.password != null) return false;
- if (!userService.equals(that.userService)) return false;
- if (username != null ? !username.equals(that.username) : that.username != null) return false;
- return true;
- }
- @Override
- public int hashCode() {
- int result = username != null ? username.hashCode() : 0;
- result = 31 * result + (password != null ? password.hashCode() : 0);
- result = 31 * result + (current != null ? current.hashCode() : 0);
- result = 31 * result + userService.hashCode();
- return result;
- }
- // -------------------------------------------------
- // Getters/ Setters
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement