3 Replies Latest reply on Mar 22, 2011 5:32 AM by matkapx

    How use IdentityManager JpaIdentityStore

    mailojahandrea

      hi, I'm new here, I greet you.
      My problem is with identity management because I can not understand the documentation (15.4 Identity Manager). Do you have a working example and easy to show so I can understand?
      I'm used to log in using the configuration in components.xml 


      <securuty:identity authenticate-method="#{authenticator.authenticate}"/>



      ...
      Thank you for your availability. Greetings to all.
      Andrea

        • 1. Re: How use IdentityManager JpaIdentityStore
          matkapx

          Identity manager and jpaIdentityStore are different concepts.
          You can customize a identity manager authentication method.
          if you use jpaIdentityStore it will not authentica user with the authenticator method.
          (authenticator class generated default by seam-gen)


          Instead of using JpaIdentityStore , (you don't have to user JpaIdentityStore) .You can use Session scoped Entities.

          • 2. Re: How use IdentityManager JpaIdentityStore
            mailojahandrea

            So what you say but I can not understand. I also tried to use the Observer as in Seamspace, but nothing! Sicutro dsa show that you have a simple example to understand? Thanks

            • 3. Re: How use IdentityManager JpaIdentityStore
              matkapx

              Ok here is a simple classes used for authentication , user and user role Entities
              Lets start first with authentication class.
              first define your class authentication class in components.xml (which is default with seam-gen or jboss tools  generated project)


              In componentes.xml you should have this line


              <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>



              And two classes UserAccount.java and UserRole.java


              UserAccount.java




              package org.domain.test.entity;
              
              import java.io.Serializable;
              import java.util.ArrayList;
              import java.util.List;
              import javax.persistence.CascadeType;
              import javax.persistence.Entity;
              import javax.persistence.FetchType;
              import javax.persistence.GenerationType;
              import javax.persistence.Id;
              import javax.persistence.GeneratedValue;
              import javax.persistence.JoinColumn;
              import javax.persistence.JoinTable;
              import javax.persistence.ManyToMany;
              import javax.persistence.SequenceGenerator;
              
              
              @Entity
              public class UserAccount implements Serializable
              {
                   @Id @GeneratedValue
                     private Integer userId;
                     private String username;       
                     private String passwordHash;
              
                     @ManyToMany(fetch=FetchType.LAZY ,cascade = CascadeType.PERSIST)
                     @JoinTable(name="ROLE_ACCOUNT",
                     joinColumns= {@JoinColumn (name="userId", referencedColumnName="userId")},
                     inverseJoinColumns= {@JoinColumn (name="roleId", referencedColumnName="roleId")})
                     private List<UserRole> roles = new ArrayList<UserRole>();       
                     public Integer getUserId() { return userId; }
                     public void setUserId(Integer userId) { this.userId = userId; }
                     public String getUsername() { return username; }
                     public void setUsername(String username) { this.username = username; }
                     public String getPasswordHash() { return passwordHash; }
                     public void setPasswordHash(String passwordHash) { this.passwordHash = passwordHash; }
                     public void setRoles(List<UserRole> roles) {
                        this.roles = roles;
                     }
                     public List<UserRole> getRoles() {
                        return roles;
                     }
              }



              And UserRole.java




              package org.domain.test.entity;
              
              import java.io.Serializable;
              import java.util.ArrayList;
              import java.util.List;
              
              import javax.persistence.Entity;
              import javax.persistence.FetchType;
              import javax.persistence.GenerationType;
              import javax.persistence.Id;
              import javax.persistence.GeneratedValue;
              import javax.persistence.ManyToMany;
              import javax.persistence.SequenceGenerator;
              import org.hibernate.validator.NotEmpty;
              import org.jboss.seam.annotations.Name;
              
              @Entity
              @Name("userRole")
              public class UserRole implements Serializable
              {
                   @Id@GeneratedValue
                   private Integer roleId;
                   @NotEmpty
                   private String rolename;     
                   @ManyToMany(fetch=FetchType.LAZY, mappedBy="roles")
                   private List<UserAccount> roles = new ArrayList<UserAccount>();
                   public Integer getRoleId(){ return roleId; }     
                   public void setRoleId(Integer roleId) { this.roleId = roleId; }     
                   public String getRolename(){ return rolename; }     
                   public void setRolename(String rolename){ this.rolename = rolename; }
                   public void setRoles(List<UserAccount> roles){
                        this.roles = roles;
                   }
                   public List<UserAccount> getRoles(){
                        return roles;
                   }
              }
              



              And last class which is using for authenctication. (as i said default java class generated with seam-gen)





              package org.domain.test.session;
              
              
              import javax.faces.application.FacesMessage;
              import javax.faces.application.FacesMessage.Severity;
              import javax.persistence.EntityManager;
              import javax.persistence.NoResultException;
              
              import org.domain.test.entity.UserAccount;
              import org.domain.test.entity.UserRole;
              import org.jboss.seam.ScopeType;
              import org.jboss.seam.annotations.In;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.Out;
              import org.jboss.seam.faces.FacesMessages;
              import org.jboss.seam.security.Credentials;
              import org.jboss.seam.security.Identity;
              
              
              @Name("authenticator")
              public class Authenticator
              {
                   @In Credentials credentials;
                   @In Identity identity;
                   @In FacesMessages facesMessages;
                   @In EntityManager entityManager;
                   @Out(scope=ScopeType.SESSION)
                   UserAccount user;
                   
                   public boolean authenticate(){
                        try{
                             user = (UserAccount)entityManager.createQuery("from UserAccount where " +
                                                                                    "username = :username and passwordHash = :password")
                                                                     .setParameter("username", credentials.getUsername())
                                                                     .setParameter("password", credentials.getPassword())
                                                                     .getSingleResult();
                             if (user.getRoles() != null){
                                  for (UserRole mr : user.getRoles()){
                                       identity.addRole(mr.getRolename());
                                  }
                                  
                                  return true;
                             }
                             else {
                                  return false;
                             }
                        }
                        catch (NoResultException ex){
                             System.out.println("kullanici yok");
                             facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR,"org.jboss.seam.NoResult");
                             return false;
                        }
                   }
              
              }
              



              As you see we did not used any jpaIdentityStore annotations like @user @role etc...
              Hope this examples would work for you.
              if you have any questions please don't hesistate , and please don't forget to rate :)