I have a session scoped bean to manage my user login and also a login filter, but i'm not sure how to get the user object inside of another bean to show certain data based on the user who is logged in.
this is my Login Bean:
@ManagedBean(name="user")
@SessionScoped
public class LoginBean {
private String username;
private String password;
private UsuariosEntity current;
public LoginBean(){}
@EJB
UsuariosDAO userService=new UsuariosDAO();
public String login() {
try{
current = userService.buscar(username, password);
System.out.println(current.getUsuario());
}catch(Exception e){
}
if (current == null) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Unknown login, try again"));
return (username = password = null);
} else {
return "/Contenido/vistaProcesos?faces-redirect=true";
}
}
public String logout() {
System.out.println("logout");
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/Login?faces-redirect=true";
}
public boolean isLoggedIn() {
return current != null;
}
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;
}
public UsuariosEntity getCurrent() {
return current;
}
}
Login Filter:
@WebFilter("/Contenido/*")
public class FiltroLogin implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) request;
LoginBean user = (LoginBean) req.getSession().getAttribute("user");
if (user != null && user.isLoggedIn()) {
// User is logged in, so just continue request.
chain.doFilter(request, response);
} else {
// User is not logged in, so redirect to index.
HttpServletResponse res = (HttpServletResponse) response;
res.sendRedirect(req.getContextPath() + "/Login.jsf");
}
}
@Override
public void destroy() {
}
}
and this is what i want to do:
@ManagedBean(name = "procesos")
@ViewScoped
public class ProcesosBean implements Serializable {
private List<VistaProcesosEntity> procesos;
@PostConstruct
public void init(){
//i want to fill procesos List with data, depending of the user who is logged on.
procesos=new ArrayList<VistaProcesosEntity>();
/* LoginBean user; // i dont know from where should i get the user object..
if (user.getCurrent().getTipo()=="Planta") {
procesos= Procesos.getALL();
}else if(user.getCurrent().getTipo()=="Exportadora"){
procesos=Procesos.getALLbyExportadora(user.getCurrent().getUsuario());
} else if (user.getCurrent().getTipo()=="Productor"){
procesos=Procesos.getALLbyProductor(user.getCurrent().getUsuario());
} else{
procesos= Procesos.getALL();
} */
}
public List<VistaProcesosEntity> getProcesos() {
return procesos;
}
@ManagedBean(name = "procesos")
@ViewScoped
public class ProcesosBean implements Serializable {
@ManagedProperty(value="#{user}") // this references the @ManagedBean named user
private LoginBean loginBean;
private List<VistaProcesosEntity> procesos;
@PostConstruct
public void init(){
UsariosEntity current = loginBean.getCurrent();
}
}
I have extended your ProcesosBean
a bit. Notice the referenced LoginBean
and retrieving the UsariosEntity
in the init()
function. If you have any more questions or something isn't clear (enough), please ask.