Skip to content

Commit

Permalink
Fixing SEAMSECURITY-132
Browse files Browse the repository at this point in the history
This was a problem with eclipselink (possibly other JPA impls as well)
where the entity given was the fully qualified name of the class instead
of the actual name of the entity / simple class name.

Also made the JpaIdentityStore class work if you specify a different
entity name besides the simple class name. All JPAQL queries should be
working correctly.
  • Loading branch information
LightGuard committed Dec 8, 2011
1 parent 20b45ab commit 5cb1233
Showing 1 changed file with 27 additions and 9 deletions.
Expand Up @@ -961,8 +961,11 @@ protected Object lookupIdentityType(String identityType, EntityManager em) {
// If there is no identity type table, just return the name
if (typeNameProp == null) return identityType;

final String identTypeEntityAnnotationValue = typeNameProp.getDeclaringClass().getAnnotation(Entity.class).name();
final String identTypeEntityName = ("".equals(identTypeEntityAnnotationValue) ? typeNameProp.getDeclaringClass().getSimpleName() : identTypeEntityAnnotationValue);

Object val = em.createQuery(
"select t from " + typeNameProp.getDeclaringClass().getSimpleName() +
"select t from " + identTypeEntityName +
" t where t." + typeNameProp.getName() +
" = :identityType")
.setParameter("identityType", identityType)
Expand Down Expand Up @@ -1154,8 +1157,11 @@ public EntityManager getEntityManager(IdentityStoreInvocationContext invocationC
public IdentityObject findIdentityObject(IdentityStoreInvocationContext invocationContext, String id)
throws IdentityException {
try {
final String identEntityAnnotationValue = identityClass.getAnnotation(Entity.class).name();
final String identEntityName = ("".equals(identEntityAnnotationValue) ? identityClass.getSimpleName() : identEntityAnnotationValue);

Object identity = getEntityManager(invocationContext).createQuery("select i from " +
identityClass.getName() + " i where i." +
identEntityName + " i where i." +
modelProperties.get(PROPERTY_IDENTITY_ID).getName() +
" = :id")
.setParameter("id", id)
Expand Down Expand Up @@ -1185,8 +1191,11 @@ public IdentityObject findIdentityObject(
lookupIdentityType(identityObjectType.getName(), getEntityManager(invocationContext)) :
identityObjectType.getName();

final String identEntityAnnotationValue = identityClass.getAnnotation(Entity.class).name();
final String identEntityName = ("".equals(identEntityAnnotationValue) ? identityClass.getSimpleName() : identEntityAnnotationValue);

Object identity = getEntityManager(invocationContext).createQuery("select i from " +
identityClass.getName() + " i where i." +
identEntityName + " i where i." +
modelProperties.get(PROPERTY_IDENTITY_NAME).getName() +
" = :name and i." + modelProperties.get(PROPERTY_IDENTITY_TYPE).getName() +
" = :type")
Expand Down Expand Up @@ -2078,25 +2087,34 @@ public Collection<IdentityObject> findIdentityObject(IdentityStoreInvocationCont
Object identType = modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME) ? lookupIdentityType(
identityType.getName(), getEntityManager(invocationCxt)) : identityType.getName();

final String identEntityAnnotationValue = identityClass.getAnnotation(Entity.class).name();
final String identEntityName = ("".equals(identEntityAnnotationValue) ? identityClass.getSimpleName() : identEntityAnnotationValue);

Object ident = getEntityManager(invocationCxt).createQuery(
"select i from " + identityClass.getName() + " i where i."
"select i from " + identEntityName + " i where i."
+ modelProperties.get(PROPERTY_IDENTITY_NAME).getName() + " = :name and i."
+ modelProperties.get(PROPERTY_IDENTITY_TYPE).getName() + " = :type")
.setParameter("name", identity.getName()).setParameter("type", identType).getSingleResult();

// FIXME: This won't work if they use the table name attribute on the annotation
String relEntityName = "";
if (modelProperties.get(PROPERTY_RELATIONSHIP_NAME) != null) {
final Class<?> relationshipClass = modelProperties.get(PROPERTY_RELATIONSHIP_NAME).getDeclaringClass();
final String relEntityAnnotationValue = relationshipClass.getAnnotation(Entity.class).name();
relEntityName = ("".equals(identEntityAnnotationValue) ? relationshipClass.getSimpleName() : relEntityAnnotationValue);
}

if (parent) {
if (relationshipType != null) {
queryString.append("select distinct ior." + modelProperties.get(PROPERTY_RELATIONSHIP_TO).getName() + " from "
+ modelProperties.get(PROPERTY_RELATIONSHIP_NAME).getDeclaringClass().getSimpleName() + " ior where ior."
+ relEntityName + " ior where ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_TO).getName() + "."
+ modelProperties.get(PROPERTY_RELATIONSHIP_NAME).getName() + " like :nameFilter and ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_TYPE).getName() + "."
+ modelProperties.get(PROPERTY_RELATIONSHIP_TYPE_NAME).getName() + " = :relType and ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_FROM).getName() + " = :identity");
} else {
queryString.append("select distinct ior. " + modelProperties.get(PROPERTY_RELATIONSHIP_TO).getName() + "from "
+ modelProperties.get(PROPERTY_RELATIONSHIP_NAME).getDeclaringClass().getSimpleName() + " ior where ior."
+ relEntityName + " ior where ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_TO).getName() + "."
+ modelProperties.get(PROPERTY_IDENTITY_NAME).getName() + " like :nameFilter and ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_FROM).getName() + " = :identity");
Expand All @@ -2109,7 +2127,7 @@ public Collection<IdentityObject> findIdentityObject(IdentityStoreInvocationCont
if (relationshipType != null) {
queryString.append("select distinct ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_FROM).getName() + " from "
+ modelProperties.get(PROPERTY_RELATIONSHIP_NAME).getDeclaringClass().getSimpleName() + " ior where ior."
+ relEntityName + " ior where ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_FROM).getName() + "."
+ modelProperties.get(PROPERTY_IDENTITY_NAME).getName() + " like :nameFilter and ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_TYPE).getName() + "."
Expand All @@ -2118,7 +2136,7 @@ public Collection<IdentityObject> findIdentityObject(IdentityStoreInvocationCont
} else {
queryString.append("select distinct ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_FROM).getName() + " from "
+ modelProperties.get(PROPERTY_RELATIONSHIP_NAME).getDeclaringClass().getSimpleName() + " ior where ior."
+ relEntityName + " ior where ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_FROM).getName() + "."
+ modelProperties.get(PROPERTY_IDENTITY_NAME).getName() + " like :nameFilter and ior."
+ modelProperties.get(PROPERTY_RELATIONSHIP_TO).getName() + " = :identity");
Expand Down

0 comments on commit 5cb1233

Please sign in to comment.