[seam-commits] Seam SVN: r7682 - in branches/Seam_2_0: src/main/org/jboss/seam/persistence and 1 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Mon Mar 24 20:57:15 EDT 2008
Author: shane.bryzak at jboss.com
Date: 2008-03-24 20:57:14 -0400 (Mon, 24 Mar 2008)
New Revision: 7682
Added:
branches/Seam_2_0/src/main/org/jboss/seam/security/EntityPermissionChecker.java
Modified:
branches/Seam_2_0/build/core.pom.xml
branches/Seam_2_0/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java
branches/Seam_2_0/src/main/org/jboss/seam/persistence/PersistenceProvider.java
branches/Seam_2_0/src/main/org/jboss/seam/security/EntitySecurityListener.java
branches/Seam_2_0/src/main/org/jboss/seam/security/HibernateSecurityInterceptor.java
branches/Seam_2_0/src/main/org/jboss/seam/security/Identity.java
Log:
JBSEAM-2029
Modified: branches/Seam_2_0/build/core.pom.xml
===================================================================
--- branches/Seam_2_0/build/core.pom.xml 2008-03-24 21:03:57 UTC (rev 7681)
+++ branches/Seam_2_0/build/core.pom.xml 2008-03-25 00:57:14 UTC (rev 7682)
@@ -42,7 +42,6 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
- <scope>runtime</scope>
<optional>true</optional>
</dependency>
Modified: branches/Seam_2_0/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java 2008-03-24 21:03:57 UTC (rev 7681)
+++ branches/Seam_2_0/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java 2008-03-25 00:57:14 UTC (rev 7682)
@@ -4,6 +4,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
@@ -15,6 +16,11 @@
import org.hibernate.Session;
import org.hibernate.StaleStateException;
import org.hibernate.TransientObjectException;
+import org.hibernate.ejb.event.Callback;
+import org.hibernate.ejb.event.EJB3PostLoadEventListener;
+import org.hibernate.ejb.event.EntityCallbackHandler;
+import org.hibernate.engine.SessionImplementor;
+import org.hibernate.event.PostLoadEventListener;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.VersionType;
import org.jboss.seam.ScopeType;
@@ -322,30 +328,99 @@
}
}
- /*@Override
- public Method getPostLoadMethod(Class beanClass)
+ /**
+ * A nasty hack until we get a nicer method in Hibernate to use instead
+ *
+ * TODO fix this once Hibernate exposes an API method to return the callback method/s for a
+ * given bean class
+ *
+ * @param entityManager
+ * @return
+ */
+ private EntityCallbackHandler getCallbackHandler(EntityManager entityManager)
{
+ PostLoadEventListener[] listeners = ((SessionImplementor) getSession(entityManager))
+ .getListeners().getPostLoadEventListeners();
+
+ for (PostLoadEventListener listener : listeners)
+ {
+ if (listener instanceof EJB3PostLoadEventListener)
+ {
+ try
+ {
+ Field callbackHandlerField = EJB3PostLoadEventListener.class.getField("callbackHandler");
+ return (EntityCallbackHandler) callbackHandlerField.get(listener);
+ }
+ catch (Exception ex)
+ {
+ throw new RuntimeException(ex);
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * More nastiness
+ *
+ * @param handler
+ * @param fieldName
+ * @return
+ */
+ private Callback[] getCallbacks(EntityCallbackHandler handler, String fieldName, Class beanClass)
+ {
+ try
+ {
+ Field f = EntityCallbackHandler.class.getField(fieldName);
+ HashMap<Class,Callback[]> callbacks = (HashMap<Class,Callback[]>) f.get(handler);
+ return callbacks.get(beanClass);
+ }
+ catch (Exception ex)
+ {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ private Method getCallbackMethod(EntityManager entityManager, Class beanClass, String callbackFieldName)
+ {
+ Callback[] callbacks = getCallbacks(getCallbackHandler(entityManager), callbackFieldName, beanClass);
+
+ if (callbacks != null)
+ {
+ for (Callback cb : callbacks)
+ {
+ return cb.getCallbackMethod();
+ }
+ }
+
return null;
}
@Override
- public Method getPrePersistMethod(Class beanClass)
+ public Method getPostLoadMethod(Class beanClass, EntityManager entityManager)
{
- return null;
+ return getCallbackMethod(entityManager, beanClass, "postLoads");
}
+
@Override
- public Method getPreUpdateMethod(Class beanClass)
+ public Method getPrePersistMethod(Class beanClass, EntityManager entityManager)
{
- return null;
+ return getCallbackMethod(entityManager, beanClass, "preCreates");
}
@Override
- public Method getPreRemoveMethod(Class beanClass)
+ public Method getPreUpdateMethod(Class beanClass, EntityManager entityManager)
{
- return null;
- }*/
+ return getCallbackMethod(entityManager, beanClass, "preUpdates");
+ }
+ @Override
+ public Method getPreRemoveMethod(Class beanClass, EntityManager entityManager)
+ {
+ return getCallbackMethod(entityManager, beanClass, "preRemoves");
+ }
+
private Session getSession(EntityManager entityManager)
{
Object delegate = entityManager.getDelegate();
Modified: branches/Seam_2_0/src/main/org/jboss/seam/persistence/PersistenceProvider.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/persistence/PersistenceProvider.java 2008-03-24 21:03:57 UTC (rev 7681)
+++ branches/Seam_2_0/src/main/org/jboss/seam/persistence/PersistenceProvider.java 2008-03-25 00:57:14 UTC (rev 7682)
@@ -151,22 +151,22 @@
return Entity.forClass(bean.getClass()).getBeanClass();
}
- public Method getPostLoadMethod(Class beanClass)
+ public Method getPostLoadMethod(Class beanClass, EntityManager entityManager)
{
return Entity.forClass(beanClass).getPostLoadMethod();
}
- public Method getPrePersistMethod(Class beanClass)
+ public Method getPrePersistMethod(Class beanClass, EntityManager entityManager)
{
return Entity.forClass(beanClass).getPrePersistMethod();
}
- public Method getPreUpdateMethod(Class beanClass)
+ public Method getPreUpdateMethod(Class beanClass, EntityManager entityManager)
{
return Entity.forClass(beanClass).getPreUpdateMethod();
}
- public Method getPreRemoveMethod(Class beanClass)
+ public Method getPreRemoveMethod(Class beanClass, EntityManager entityManager)
{
return Entity.forClass(beanClass).getPreRemoveMethod();
}
Added: branches/Seam_2_0/src/main/org/jboss/seam/security/EntityPermissionChecker.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/security/EntityPermissionChecker.java (rev 0)
+++ branches/Seam_2_0/src/main/org/jboss/seam/security/EntityPermissionChecker.java 2008-03-25 00:57:14 UTC (rev 7682)
@@ -0,0 +1,126 @@
+package org.jboss.seam.security;
+
+import static org.jboss.seam.ScopeType.APPLICATION;
+import static org.jboss.seam.annotations.Install.BUILT_IN;
+
+import java.lang.reflect.Method;
+
+import javax.persistence.EntityManager;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.Seam;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Startup;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.annotations.security.Restrict;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.persistence.PersistenceProvider;
+import org.jboss.seam.util.Strings;
+
+/**
+ * Entity permission checks
+ *
+ * @author Shane Bryzak
+ */
+ at Name("org.jboss.seam.security.entityPermissionChecker")
+ at Scope(APPLICATION)
+ at Install(precedence = BUILT_IN)
+ at BypassInterceptors
+ at Startup
+public class EntityPermissionChecker
+{
+ private String entityManagerName = "entityManager";
+
+ private EntityManager getEntityManager()
+ {
+ return (EntityManager) Component.getInstance(entityManagerName);
+ }
+
+ public String getEntityManagerName()
+ {
+ return entityManagerName;
+ }
+
+ public void setEntityManagerName(String name)
+ {
+ this.entityManagerName = name;
+ }
+
+ public static EntityPermissionChecker instance()
+ {
+ if ( !Contexts.isApplicationContextActive() )
+ {
+ throw new IllegalStateException("No active application context");
+ }
+
+ EntityPermissionChecker instance = (EntityPermissionChecker) Component.getInstance(
+ EntityPermissionChecker.class, ScopeType.APPLICATION);
+
+ if (instance == null)
+ {
+ throw new IllegalStateException("No EntityPermissionChecker could be created");
+ }
+
+ return instance;
+ }
+
+ public void checkEntityPermission(Object entity, EntityAction action)
+ {
+ if (!Identity.isSecurityEnabled()) return;
+
+ Identity identity = Identity.instance();
+
+ identity.isLoggedIn(true);
+
+ PersistenceProvider provider = PersistenceProvider.instance();
+ Class beanClass = provider.getBeanClass(entity);
+
+ if (beanClass != null)
+ {
+ String name = Seam.getComponentName(entity.getClass());
+ if (name == null) name = beanClass.getName();
+
+ Method m = null;
+ switch (action)
+ {
+ case READ:
+ m = provider.getPostLoadMethod(beanClass, getEntityManager());
+ break;
+ case INSERT:
+ m = provider.getPrePersistMethod(beanClass, getEntityManager());
+ break;
+ case UPDATE:
+ m = provider.getPreUpdateMethod(beanClass, getEntityManager());
+ break;
+ case DELETE:
+ m = provider.getPreRemoveMethod(beanClass, getEntityManager());
+ }
+
+ Restrict restrict = null;
+
+ if (m != null && m.isAnnotationPresent(Restrict.class))
+ {
+ restrict = m.getAnnotation(Restrict.class);
+ }
+ else if (entity.getClass().isAnnotationPresent(Restrict.class))
+ {
+ restrict = entity.getClass().getAnnotation(Restrict.class);
+ }
+
+ if (restrict != null)
+ {
+ if (Strings.isEmpty(restrict.value()))
+ {
+ identity.checkPermission(name, action.toString(), entity);
+ }
+ else
+ {
+ identity.checkRestriction(restrict.value());
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: branches/Seam_2_0/src/main/org/jboss/seam/security/EntitySecurityListener.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/security/EntitySecurityListener.java 2008-03-24 21:03:57 UTC (rev 7681)
+++ branches/Seam_2_0/src/main/org/jboss/seam/security/EntitySecurityListener.java 2008-03-25 00:57:14 UTC (rev 7682)
@@ -21,36 +21,24 @@
@PostLoad
public void postLoad(Object entity)
{
- if (Identity.isSecurityEnabled())
- {
- Identity.instance().checkEntityPermission(entity, READ);
- }
+ EntityPermissionChecker.instance().checkEntityPermission(entity, READ);
}
@PrePersist
public void prePersist(Object entity)
{
- if (Identity.isSecurityEnabled())
- {
- Identity.instance().checkEntityPermission(entity, INSERT);
- }
+ EntityPermissionChecker.instance().checkEntityPermission(entity, INSERT);
}
@PreUpdate
public void preUpdate(Object entity)
{
- if (Identity.isSecurityEnabled())
- {
- Identity.instance().checkEntityPermission(entity, UPDATE);
- }
+ EntityPermissionChecker.instance().checkEntityPermission(entity, UPDATE);
}
@PreRemove
public void preRemove(Object entity)
{
- if (Identity.isSecurityEnabled())
- {
- Identity.instance().checkEntityPermission(entity, DELETE);
- }
+ EntityPermissionChecker.instance().checkEntityPermission(entity, DELETE);
}
}
Modified: branches/Seam_2_0/src/main/org/jboss/seam/security/HibernateSecurityInterceptor.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/security/HibernateSecurityInterceptor.java 2008-03-24 21:03:57 UTC (rev 7681)
+++ branches/Seam_2_0/src/main/org/jboss/seam/security/HibernateSecurityInterceptor.java 2008-03-25 00:57:14 UTC (rev 7682)
@@ -30,10 +30,8 @@
public boolean onLoad(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
{
- if (Identity.isSecurityEnabled())
- {
- Identity.instance().checkEntityPermission(entity, READ);
- }
+ EntityPermissionChecker.instance().checkEntityPermission(entity, READ);
+
return wrappedInterceptor != null ?
wrappedInterceptor.onLoad(entity, id, state, propertyNames, types) :
false;
@@ -43,10 +41,8 @@
public void onDelete(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
{
- if (Identity.isSecurityEnabled())
- {
- Identity.instance().checkEntityPermission(entity, DELETE);
- }
+ EntityPermissionChecker.instance().checkEntityPermission(entity, DELETE);
+
if (wrappedInterceptor != null)
wrappedInterceptor.onDelete(entity, id, state, propertyNames, types);
}
@@ -55,10 +51,8 @@
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
Object[] previousState, String[] propertyNames, Type[] types)
{
- if (Identity.isSecurityEnabled())
- {
- Identity.instance().checkEntityPermission(entity, UPDATE);
- }
+ EntityPermissionChecker.instance().checkEntityPermission(entity, UPDATE);
+
return wrappedInterceptor != null ?
wrappedInterceptor.onFlushDirty(entity, id, currentState,
previousState, propertyNames, types) : false;
@@ -68,10 +62,8 @@
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
{
- if (Identity.isSecurityEnabled())
- {
- Identity.instance().checkEntityPermission(entity, INSERT);
- }
+ EntityPermissionChecker.instance().checkEntityPermission(entity, INSERT);
+
return wrappedInterceptor != null ?
wrappedInterceptor.onSave(entity, id, state, propertyNames, types) :
false;
Modified: branches/Seam_2_0/src/main/org/jboss/seam/security/Identity.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/security/Identity.java 2008-03-24 21:03:57 UTC (rev 7681)
+++ branches/Seam_2_0/src/main/org/jboss/seam/security/Identity.java 2008-03-25 00:57:14 UTC (rev 7682)
@@ -626,58 +626,5 @@
principal = savedPrincipal;
subject = savedSubject;
}
- }
-
- public void checkEntityPermission(Object entity, EntityAction action)
- {
- isLoggedIn(true);
-
- PersistenceProvider provider = PersistenceProvider.instance();
- Class beanClass = provider.getBeanClass(entity);
-
- if (beanClass != null)
- {
- String name = Seam.getComponentName(entity.getClass());
- if (name == null) name = beanClass.getName();
-
- Method m = null;
- switch (action)
- {
- case READ:
- m = provider.getPostLoadMethod(beanClass);
- break;
- case INSERT:
- m = provider.getPrePersistMethod(beanClass);
- break;
- case UPDATE:
- m = provider.getPreUpdateMethod(beanClass);
- break;
- case DELETE:
- m = provider.getPreRemoveMethod(beanClass);
- }
-
- Restrict restrict = null;
-
- if (m != null && m.isAnnotationPresent(Restrict.class))
- {
- restrict = m.getAnnotation(Restrict.class);
- }
- else if (entity.getClass().isAnnotationPresent(Restrict.class))
- {
- restrict = entity.getClass().getAnnotation(Restrict.class);
- }
-
- if (restrict != null)
- {
- if (Strings.isEmpty(restrict.value()))
- {
- checkPermission(name, action.toString(), entity);
- }
- else
- {
- checkRestriction(restrict.value());
- }
- }
- }
- }
+ }
}
More information about the seam-commits
mailing list