User see only Items of the Operational unit that is in your registration

Asked

Viewed 63 times

0

I have a vehicle management system. The company in which I work has several business units, so the user can only see the cars of the unit in which he works. This I managed to do, but whenever someone logs into the system it replaces the data of the current session with the session that was last entered. I created a Userstatic that made the user only see what is on its drive, but whenever someone enters it overwrites the data. I posted the code of my loginControl, because that’s where when a user enters it arrow to the userStatic a new user. If anyone knows a new way to do that.

 public String logar() {
    usuario = usuarioFacade.buscarUsuario(login, senha);
    if (usuario != null) {
        List<GrantedAuthority> roles = new ArrayList<>();
        roles.add(new GrantedAuthorityImpl(usuario.getPermissaoUsuario().toString()));
        UserStatic.setUsuario(usuario);
        SecurityContext context = SecurityContextHolder.getContext();
        context.setAuthentication(new UsernamePasswordAuthenticationToken(
                login, senha, roles));
  • A static user won’t even work.

  • 1

    This snippet you posted is from your right User Bean?

  • is my bean Login, tbm I have user

  • Well they passed me the information to use Static user, as I should do then ?

  • I answered your question, I do not know if I was very clear but any doubt is only speak.

1 answer

0


Well, I think the problem is in your UserStatic. From one that I took from a reply of @utluiz:

Static attributes: Static attributes have the same value for all the instances of an object (within the same JVM, within a same Classloader).

Did you understand right? Basically all instances of your object will have the same value.

Back to the problem, in your Bean login you can create a variable to represent the logged in user, as follows:

private Usuario usuarioLogado; 

This one of yours Bean shall be marked as @SessionScoped. After you log in, just store the return of the buscarUsuario() in that usuarioLogado.

Done this you can recover this user in other Beans using the annotation ManagedProperty that allows injecting a Bean in the other, ex:

@ManagedProperty(value = "#{loginBean}")
private LoginBean loginBean;

Then to recover the logged in user just do:

//Pega os atributos do usuário Logado 
loginBean.getUsuarioLogado().getLogin();
  • Thanks, but I just don’t quite understand how to implement this..

  • Which part didn’t you understand?

  • In my loginControl I will put private user user ?

  • in my loginControle would look like this ? public String log in() { user = userFacade.searchUsuario(login, password); userLog = userFacade.searchUsuario(login, password);

  • I did this way more it is saving direct, if I log in as another user it keeps the data of the user who logged in first..

  • You have defined your Bean as Sessionscoped?

  • Yes Named Sessionscoped public class loginControl Implements Serializable {

  • And in your Bean you want to manipulate the user you are picking up the user?

  • 1

    Solved as @Diegoaugusto reported above. Thank you

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.