Help

Controls

PermLinkWikiLink

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

Forum: Seam Users Forum ListTopic List
23. Apr 2008, 11:49 CET | Link

Hello,

im a new user of the seam framework and it works fine.

Now I wonna add different sites for different roles. But i dont know how i read out this roles from my database.

I ve a table with id, username, password and role. I ve 4 different roles: admin, lender, borrower and caretaker.

The aim is to get out the role for each member of my site and set restrictions p e.

I use the AthenticatorAction class from the seam booking example. Its possible to compare username and pasword from login and password.

code:

package test.session;

import static org.jboss.seam.ScopeType.SESSION;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;

import de.uni_leipzig.wifa.iwi.handapparat.entity.User;

@Stateless
@Name("authenticator")
public class AuthenticatorAction implements Authenticator
{
   @PersistenceContext 
   private EntityManager em;
   
   @Out(required=false, scope = SESSION)
   private User user;
   
   public boolean authenticate()
   {
      List results = em.createQuery("select u from User u where u.username=#{identity.username} and u.password=#{identity.password}")
            .getResultList();
      
      if ( results.size()==0 )
      {
         return false;
      }
      else
      {
         user = (User) results.get(0);
         return true;
      }
   }
}

If there were help, i would be happy. THX Kevin

8 Replies:
23. Apr 2008, 11:55 CET | Link

fetch the user roles from the db and use identity.addRole()

 

If a man speaks in the forest and there is no woman around to hear him, is he still wrong?

23. Apr 2008, 14:03 CET | Link

Thx, but I dont come to a solution. Do u have an code snippet or an example to solve this easily?

23. Apr 2008, 14:16 CET | Link

hi !

you got your user if the password is correct!


user = (User) results.get(0);

now you can call the attributes you like from this object by using the getters...

identity.addRole(user.getRole());

cu

23. Apr 2008, 14:19 CET | Link

something like


List<String> roles = em.createQuery("select r from Roles where r.username=#{identity.username}").getResultList();
for (String role : roles) {
  identity.addRole(role);
}

if you can't get it work from there, you are in a world of trouble anyways ;-)

 

If a man speaks in the forest and there is no woman around to hear him, is he still wrong?

23. Apr 2008, 14:20 CET | Link

Aah, you have only one role. Follow FRG:s advice...

 

If a man speaks in the forest and there is no woman around to hear him, is he still wrong?

23. Apr 2008, 14:57 CET | Link

Thx 4 help, it works. i used that:

identity.addRole(user.getUserRole());

but i had to change the user roles, this were enumerations.

23. Apr 2008, 15:53 CET | Link

Or use .name() on the Enum object :-)

23. Apr 2008, 17:18 CET | Link

OK thx, i'll try it.

Next goal is to set multiple rules. That means an user can claim more than one user roles.

Theres a class User and a class UserRoles with an enumeration class which consists of 4 roles. I hope its the right way to establish the role concept.

Advices are desired. 8)