Extend JpaIdentityStore
hristo Aug 10, 2009 4:38 PMHello,
I'm trying to use IdentityManagement in my application but I need to extend JpaIdentityStore and override it's method public Object lookupUser(String username). In this method I need to look for users in the database not only by 'username' but also by additional field 'organization.
The problem is that in IdentityManager.initIdentityStore() the field identityStore is initialized with Seam's built-in component.
This is my class:
@Name("org.jboss.seam.security.identityStore") @Install(precedence = Install.APPLICATION, value = false) @Scope(ScopeType.APPLICATION) @BypassInterceptors public class JpaIdentityStore extends org.jboss.seam.security.management.JpaIdentityStore { public Object lookupUser(String username) { try { EntityManager entityManager = (EntityManager) Component.getInstance("entityManager"); Object user = entityManager.createQuery("select u from UserAccount u where username = :username and organization = :organization") .setParameter("username", username) .setParameter("organization", Identity.instance().getCredentials()).getOrganization()) .getSingleResult(); return user; } catch (NoResultException ex) { return null; } }
The code :
identityStore = (IdentityStore) Component.getInstance(JpaIdentityStore.class, true);
returns the built-in component org.jboss.seam.security.management.JpaIdentityStore and my custom lookupUser(String username) is never called.
I tried to override the IdentityManager class.
@Scope(EVENT) @Name("org.jboss.seam.security.identityManager") @Install(precedence = Install.APPLICATION) @BypassInterceptors public class IdentityManager extends org.jboss.seam.security.management.IdentityManager { public boolean authenticate(String username, String password) { if (Strings.isEmpty(username)) return false; IdentityStore identityStore = (IdentityStore) Component.getInstance("myIdentityStore",true); return identityStore.authenticate(username, password); }
and give the name myIdentityStore
to my custom JpaIdentityStore class.
(Interestingly in this case in method IdentityManager.instance() the code :
(IdentityManager) Component.getInstance(IdentityManager.class, EVENT);
returns my custom IdentityManager)
This way my custom lookupUser method is called during user authentication but the problem this time is that fields: IdentityManager.identityStore and IdentityManager.roleIdentityStore are again initialized with seam's built-in JpaIdentityStore. This happens in IdentityManager.initIdentityStore() where Component.getInstance(JpaIdentityStore.class, true); returns org.jboss.seam.security.management.JpaIdentityStore
Can anyone help me!? How can I extend JpaIdentityStore and just override it's lookupUser method and make the login module use it? And how Component.getInstance(JpaIdentityStore.class); can return an instance of my subclass?
Thank you