Index: topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java diff -u topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.14 topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.15 --- topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java:1.14 Thu Feb 23 14:05:47 2006 +++ topia2/src/java/org/codelutin/topia/framework/TopiaContextImpl.java Fri Feb 24 00:51:04 2006 @@ -23,9 +23,9 @@ * * @author poussin * - * @version $Revision: 1.14 $ + * @version $Revision: 1.15 $ * - * Last update: $Date: 2006/02/23 14:05:47 $ by : $Author: bpoussin $ + * Last update: $Date: 2006/02/24 00:51:04 $ by : $Author: thimel $ */ package org.codelutin.topia.framework; @@ -65,6 +65,13 @@ import org.codelutin.topia.persistence.TopiaEntity; import org.codelutin.topia.persistence.TopiaEntityAbstract; import org.codelutin.topia.persistence.TopiaId; +import org.codelutin.topia.security.TopiaConfiguration; +import org.codelutin.topia.security.TopiaPolicy; +import org.codelutin.topia.security.TopiaSecurityVetoableListener; +import org.codelutin.topia.security.entities.TopiaPermissionManager; +import org.codelutin.topia.security.entities.TopiaUserManager; +import org.codelutin.topia.security.entities.hibernate.TopiaHibernatePermissionManager; +import org.codelutin.topia.security.entities.hibernate.TopiaHibernateUserManager; import org.codelutin.util.ArrayUtil; import org.codelutin.util.CategorisedListenerSet; import org.codelutin.util.ListenerSet; @@ -181,12 +188,30 @@ protected List transactionEvents = new LinkedList(); /** + * Type de la sécurité (topia, ldap, ...) + */ + protected String securityType = null; + + protected TopiaUserManager userManager; + protected TopiaPermissionManager permissionManager; + + protected TopiaSecurityVetoableListener securityListener; + + /** * constructeur utilisé par la factory pour creer les contexts initiaux * * @param config */ public TopiaContextImpl(Properties config) { this.config = config; + if (isSecurityEnabled()) { + securityListener = new TopiaSecurityVetoableListener(); + addVetoableListener(securityListener); + addVetoableLoadListener(securityListener); + TopiaPolicy policy = new TopiaPolicy(this); + policy.installPolicy(); + javax.security.auth.login.Configuration.setConfiguration(new TopiaConfiguration("topia", this)); + } } /** @@ -595,14 +620,15 @@ public TopiaContext beginTransaction() throws TopiaNotFoundException { TopiaContextImpl result = new TopiaContextImpl(this); addChildContext(result); - result.hibernate = getHibernateFactory().openSession(); + SessionFactory factory = getHibernateFactory(); + result.hibernate = factory.openSession(); // new TopiaInterceptor(result)); // on ne synchronise jamais les données avec la base tant que // l'utilisateur n'a pas fait de commit du context result.hibernate.setFlushMode(FlushMode.NEVER); // result.tx = result.hibernate.beginTransaction(); - + return result; } @@ -760,18 +786,20 @@ /* (non-Javadoc) * @see org.codelutin.topia.framework.TopiaContextImplementor#fireVetoableLoad(java.lang.Class, java.lang.Object) */ - public void fireVetoableLoad(Class entityClass, Object id) { + public boolean fireVetoableLoad(Class entityClass, Object id) { if (log.isDebugEnabled()) { log.debug("fireVetoableLoad: " + id); } try { + boolean result = true; TopiaVetoableEntityLoadEvent event = new TopiaVetoableEntityLoadEvent(this, entityClass, id); for(Iterator l=getVetoableLoadListeners().iterator(); l.hasNext();) { - l.next().loadEntity(event); + result &= l.next().loadEntity(event); } if (getParentContext() != null) { - getParentContext().fireVetoableLoad(entityClass, id); + result &= getParentContext().fireVetoableLoad(entityClass, id); } + return result; } catch (Exception eee) { throw new TopiaVetoException(eee); } @@ -849,7 +877,14 @@ TopiaEntityAbstract entityAbstract = (TopiaEntityAbstract) entity; //On ne rattache l'entité à un contexte que si elle n'en a pas if (entityAbstract.getTopiaContext() == null) { - entityAbstract.setTopiaContext(this); + try { + entityAbstract.setTopiaContext(this); + } catch (TopiaException te) { + if (log.isWarnEnabled()) { + log.warn("Impossible d'initialiser le TopiaContext" + + " sur cette entité : " + entityAbstract, te); + } + } } } try { @@ -859,7 +894,7 @@ l.next().entityLoaded(event); } } catch (Exception eee) { - if (log.isWarnEnabled()) { + if (log.isErrorEnabled()) { log.error("Can't fire event loaded for entity: " + entity, eee); } } @@ -876,7 +911,14 @@ TopiaEntityAbstract entityAbstract = (TopiaEntityAbstract) entity; //On ne rattache l'entité à un contexte que si elle n'en a pas if (entityAbstract.getTopiaContext() == null) { - entityAbstract.setTopiaContext(this); + try { + entityAbstract.setTopiaContext(this); + } catch (TopiaException te) { + if (log.isWarnEnabled()) { + log.warn("Impossible d'initialiser le TopiaContext" + + " sur cette entité : " + entityAbstract, te); + } + } } } try { @@ -1085,8 +1127,12 @@ TopiaContextImplementor context = getContext(rootContext, event .getSession()); if (context != null) { - context.fireVetoableLoad(event.getPersister().getMappedClass( + boolean loadAuthorised = true; + loadAuthorised &= context.fireVetoableLoad(event.getPersister().getMappedClass( EntityMode.POJO), event.getId()); + if (!loadAuthorised) { + throw new SecurityException("User is not authorised to load this entity"); + } } } @@ -1163,4 +1209,88 @@ } } -} + /** + * Initialise la variable securityType en fonction des + * propriétés du context. Si le type n'est pas connu, la variable reste à + * null. + */ + protected void initSecurityType() { + if (parentContext != null) { + securityType = parentContext.getSecurityType(); + } else { + Properties props = getConfig(); + if (props == null) { + securityType = null; + } else { + securityType = props.getProperty("topia.authentication"); + //Si le type n'est pas connu, on remet le securityType à null + if (!"topia".equalsIgnoreCase(securityType) && + !"ldap".equalsIgnoreCase(securityType)) { + log.warn("Type de sécurité inconnu : " + securityType); + securityType = null; + } + } + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.TopiaContextImplementor#isSecurityEnabled() + */ + public boolean isSecurityEnabled() { + return (getSecurityType() != null); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.TopiaContextImplementor#getSecurityType() + */ + public String getSecurityType() { + //Si le securityType est null, on retente la lecture des propriétés + if (securityType == null) { + initSecurityType(); + } + return securityType; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.TopiaContextImplementor#getUserManager() + */ + public TopiaUserManager getUserManager() { + if (userManager == null) { + //L'appel à isSecurityEnabled va forcer l'initialisation du securityType + if (isSecurityEnabled()) { + if ("topia".equalsIgnoreCase(securityType)) { + userManager = new TopiaHibernateUserManager(this); + } else if ("ldap".equalsIgnoreCase(securityType)) { +// userManager = new TopiaLDAPUserManager(this); + } else { + log.warn("Type de sécurité inconnu : " + securityType); + } + } else { + userManager = null; + } + } + return userManager; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.TopiaContextImplementor#getPermissionManager() + */ + public TopiaPermissionManager getPermissionManager() { + if (permissionManager == null) { + //L'appel à isSecurityEnabled va forcer l'initialisation du securityType + if (isSecurityEnabled()) { + if ("topia".equalsIgnoreCase(securityType)) { + permissionManager = new TopiaHibernatePermissionManager(this); + } else if ("ldap".equalsIgnoreCase(securityType)) { +// userManager = new TopiaLDAPUserManager(this); + } else { + log.warn("Type de sécurité inconnu : " + securityType); + } + } else { + permissionManager = null; + } + } + return permissionManager; + } + +} //TopiaContextImpl Index: topia2/src/java/org/codelutin/topia/framework/TopiaContextImplementor.java diff -u topia2/src/java/org/codelutin/topia/framework/TopiaContextImplementor.java:1.5 topia2/src/java/org/codelutin/topia/framework/TopiaContextImplementor.java:1.6 --- topia2/src/java/org/codelutin/topia/framework/TopiaContextImplementor.java:1.5 Fri Feb 10 14:01:25 2006 +++ topia2/src/java/org/codelutin/topia/framework/TopiaContextImplementor.java Fri Feb 24 00:51:04 2006 @@ -23,10 +23,10 @@ * Created: 3 janv. 2006 21:27:24 * * @author poussin - * @version $Revision: 1.5 $ + * @version $Revision: 1.6 $ * - * Last update: $Date: 2006/02/10 14:01:25 $ - * by : $Author: bpoussin $ + * Last update: $Date: 2006/02/24 00:51:04 $ + * by : $Author: thimel $ */ package org.codelutin.topia.framework; @@ -39,6 +39,8 @@ import org.codelutin.topia.TopiaNotFoundException; import org.codelutin.topia.persistence.TopiaDAO; import org.codelutin.topia.persistence.TopiaEntity; +import org.codelutin.topia.security.entities.TopiaPermissionManager; +import org.codelutin.topia.security.entities.TopiaUserManager; import org.codelutin.util.CategorisedListenerSet; import org.codelutin.util.ListenerSet; import org.hibernate.Session; @@ -130,7 +132,7 @@ * @param id identifiant de l'objet a charger * @throws Exception If one listener don't accepte this action */ - public void fireVetoableLoad(Class entityClass, Object id); + public boolean fireVetoableLoad(Class entityClass, Object id); /** * Appele avant de creer un objet, le listener peut lever une exception @@ -190,4 +192,26 @@ */ public void fireOnRollbacked(); -} + /** + * Indique si la sécurité est activée ou non + */ + public boolean isSecurityEnabled(); + + /** + * Renvoie le type de sécurité utilisé par le contexte + */ + public String getSecurityType(); + + /** + * Renvoie le TopiaUserManager indiqué dans les propriétés. Si le type de + * userManager n'est pas reconnu, renvoie null. + */ + public TopiaUserManager getUserManager(); + + /** + * Renvoie le TopiaPermissionManager indiqué dans les propriétés. Si le type + * de permissionManager n'est pas reconnu, renvoie null. + */ + public TopiaPermissionManager getPermissionManager(); + +} //TopiaContextImplementor