Index: topia-security/src/java/org/codelutin/topia/security/listener/EntityVetoable.java diff -u /dev/null topia-security/src/java/org/codelutin/topia/security/listener/EntityVetoable.java:1.1 --- /dev/null Tue Oct 24 12:01:05 2006 +++ topia-security/src/java/org/codelutin/topia/security/listener/EntityVetoable.java Tue Oct 24 12:01:00 2006 @@ -0,0 +1,136 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaSecurityVetoableListener.java +* +* Created: 10 févr. 2006 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +* +* Mise a jour: $Date: 2006/10/24 12:01:00 $ +* par : $Author: ruchaud $ +*/ + +package org.codelutin.topia.security.listener; + +import static org.codelutin.topia.security.util.TopiaSecurityUtil.CREATE; +import static org.codelutin.topia.security.util.TopiaSecurityUtil.DELETE; +import static org.codelutin.topia.security.util.TopiaSecurityUtil.LOAD; + +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.event.TopiaEntityEvent; +import org.codelutin.topia.event.TopiaEntityVetoable; +import org.codelutin.topia.persistence.TopiaEntity; +import org.codelutin.topia.security.TopiaSecurityServiceImpl; + +/** + * Listenner permettant de vérifier les autorisations pour la création ou la + * suppression d'une entité. + * @author ruchaud + */ +public class EntityVetoable implements TopiaEntityVetoable { + + private static Log log = LogFactory.getLog(EntityVetoable.class); + + private TopiaSecurityServiceImpl securityManager; + + public EntityVetoable(TopiaSecurityServiceImpl securityManager) { + this.securityManager = securityManager; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.event.TopiaVetoableEntityListener#createEntity(org.codelutin.topia.event.TopiaVetoableEntityEvent) + */ + public void create(TopiaEntityEvent event) { + Class clazz = event.getEntity().getClass(); + if (log.isDebugEnabled()) { + log.debug("[Security] create entity : " + clazz.getName()); + } + try { + securityManager.checkPermission(clazz, CREATE); + } catch (TopiaException te) { + throw new SecurityException("Access denied to entity creation", te); + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.event.TopiaVetoableEntityListener#deleteEntity(org.codelutin.topia.event.TopiaVetoableEntityEvent) + */ + public void delete(TopiaEntityEvent event) { + String topiaId = event.getEntity().getTopiaId(); + if (log.isDebugEnabled()) { + log.debug("[Security] delete entity : " + topiaId); + } + try { + securityManager.checkPermission(topiaId, DELETE); + } catch (TopiaException te) { + throw new SecurityException("Access denied to entity deletion", te); + } + } + + /* + * (non-Javadoc) + * @see org.codelutin.topia.event.TopiaVetoableEntityLoadListener#loadEntity(org.codelutin.topia.event.TopiaVetoableEntityLoadEvent) + */ + public void load(TopiaEntityEvent event) { + boolean authorized = true; + TopiaEntity entity = event.getEntity(); + String topiaId = entity.getTopiaId(); + Class clazz = entity.getClass(); + + Class[] interfaces = clazz.getInterfaces(); + List asList = Arrays.asList(interfaces); + if (!asList.contains(NoEntityVetoableRead.class)) { + if (log.isDebugEnabled()) { + log.debug("[Security] load entity : " + topiaId); + } + + /* Vérification dans le cache */ + boolean contain = securityManager.containEntitiesLoadingCache(topiaId); + + if(!contain) { + try { + securityManager.checkPermission(topiaId, LOAD); + + } catch (TopiaException te) { + authorized = false; + } + + /* Mise en cache */ + securityManager.putEntitiesLoadingCache(topiaId, authorized); + } + + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.event.TopiaVetoableEntityListener#updateEntity(org.codelutin.topia.event.TopiaVetoableEntityEvent) + */ + public void update(TopiaEntityEvent event) { + } + +} Index: topia-security/src/java/org/codelutin/topia/security/listener/PropertyVetoable.java diff -u /dev/null topia-security/src/java/org/codelutin/topia/security/listener/PropertyVetoable.java:1.1 --- /dev/null Tue Oct 24 12:01:05 2006 +++ topia-security/src/java/org/codelutin/topia/security/listener/PropertyVetoable.java Tue Oct 24 12:01:00 2006 @@ -0,0 +1,104 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaSecurityVetoableListener.java +* +* Created: 10 févr. 2006 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +* +* Mise a jour: $Date: 2006/10/24 12:01:00 $ +* par : $Author: ruchaud $ +*/ + +package org.codelutin.topia.security.listener; + +import java.util.Arrays; +import java.util.List; + +import org.codelutin.topia.event.TopiaEntityEvent; +import org.codelutin.topia.event.TopiaEntityListener; +import org.codelutin.topia.persistence.TopiaEntity; + +/** + * Ajout en cas de chargement ou de creation d'entités des listeners pour la + * sécurité sur leurs champs. + * @author ruchaud + */ +public class PropertyVetoable implements TopiaEntityListener { + + protected PropertyReadListener read; + protected PropertyWriteListener write; + + /** + * Contructeur avec comme paramètre les listeners à attacher au chargement ou + * à la création. + * @param readListener Listener en lecture d'un champ + * @param writeListener Listener en écriture d'un champ + */ + public PropertyVetoable(PropertyReadListener read, PropertyWriteListener write) { + super(); + this.read = read; + this.write = write; + } + + private void putVetoables(TopiaEntityEvent event) { + TopiaEntity entity = event.getEntity(); + Class[] interfaces = entity.getClass().getInterfaces(); + List asList = Arrays.asList(interfaces); + if (!asList.contains(NoEntityVetoableRead.class)) { + entity.addVetoableListener(read); + } + entity.addVetoableChangeListener(write); + } + + /* + * (non-Javadoc) + * @see org.codelutin.topia.event.TopiaEntityLoadListener#entityLoaded(org.codelutin.topia.event.TopiaEntityLoadEvent) + */ + public void load(TopiaEntityEvent event) { + putVetoables(event); + } + + /* + * (non-Javadoc) + * @see org.codelutin.topia.event.TopiaEntityListener#entityCreated(org.codelutin.topia.event.TopiaEntityEvent) + */ + public void create(TopiaEntityEvent event) { + putVetoables(event); + } + + /* + * (non-Javadoc) + * @see org.codelutin.topia.event.TopiaEntityListener#entityDeleted(org.codelutin.topia.event.TopiaEntityEvent) + */ + public void delete(TopiaEntityEvent event) { + } + + /* + * (non-Javadoc) + * @see org.codelutin.topia.event.TopiaEntityListener#entityUpdated(org.codelutin.topia.event.TopiaEntityEvent) + */ + public void update(TopiaEntityEvent event) { + } + +} //TopiaSecurityVetoableListener Index: topia-security/src/java/org/codelutin/topia/security/listener/NoEntityVetoableRead.java diff -u /dev/null topia-security/src/java/org/codelutin/topia/security/listener/NoEntityVetoableRead.java:1.1 --- /dev/null Tue Oct 24 12:01:05 2006 +++ topia-security/src/java/org/codelutin/topia/security/listener/NoEntityVetoableRead.java Tue Oct 24 12:01:00 2006 @@ -0,0 +1,29 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +package org.codelutin.topia.security.listener; + +/** + * Interface permettant à préciser que l'entité n'est pas soumis aux autorisations + * de chargement sur les entités. + * @author ruchaud + */ +public interface NoEntityVetoableRead { +}