Index: topia2/src/java/org/codelutin/topia/security/entities/hibernate/TopiaHibernatePermissionManager.java diff -u /dev/null topia2/src/java/org/codelutin/topia/security/entities/hibernate/TopiaHibernatePermissionManager.java:1.1 --- /dev/null Fri Feb 24 00:48:20 2006 +++ topia2/src/java/org/codelutin/topia/security/entities/hibernate/TopiaHibernatePermissionManager.java Fri Feb 24 00:48:15 2006 @@ -0,0 +1,133 @@ +/* *##% +* 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. +*##%*/ + +/* * +* TopiaHibernatePermissionManager.java +* +* Created: 17 févr. 2006 +* +* @author Arnaud Thimel +* @version $Revision$ +* +* Mise a jour: $Date$ +* par : $Author$ +*/ + + +package org.codelutin.topia.security.entities.hibernate; + +import java.security.Principal; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.event.TopiaEntityEvent; +import org.codelutin.topia.event.TopiaEntityListener; +import org.codelutin.topia.framework.TopiaContextImplementor; +import org.codelutin.topia.persistence.TopiaDAO; +import org.codelutin.topia.security.entities.TopiaEntityPermission; +import org.codelutin.topia.security.entities.TopiaEntityPermissionImpl; +import org.codelutin.topia.security.entities.TopiaPermission; +import org.codelutin.topia.security.entities.TopiaPermissionManager; + +public class TopiaHibernatePermissionManager implements TopiaPermissionManager, + TopiaEntityListener { + + private TopiaDAO dao; + + private TopiaContextImplementor context; + + private Map permissionsCache; + + public TopiaHibernatePermissionManager(TopiaContext context) { + this.context = (TopiaContextImplementor)context; + context.addTopiaEntityListener(TopiaEntityPermission.class, this); + } + + private TopiaDAO getDAO() throws TopiaException { + if (dao == null) { + dao = context.getDAO(TopiaEntityPermission.class); + } + return dao; + } + + public Collection getAllPermissions() throws TopiaException { + return getCache().values(); + } + + public void entityCreated(TopiaEntityEvent event) { + for (Object obj : event.getTopiaEntities()) { + TopiaEntityPermission entityPerm = (TopiaEntityPermission)obj; + try { + getCache().put(entityPerm, new TopiaPermission(entityPerm)); + } catch (TopiaException te) { + te.printStackTrace(); + } + } + } + + public void entityUpdated(TopiaEntityEvent event) { + } + + public void entityDeleted(TopiaEntityEvent event) { + for (Object obj : event.getTopiaEntities()) { + TopiaEntityPermission entityPerm = (TopiaEntityPermission)obj; + try { + getCache().remove(entityPerm); + } catch (TopiaException te) { + te.printStackTrace(); + } + } + } + + public TopiaPermission create(String id, int actions, Set principals) throws TopiaException { + TopiaEntityPermissionImpl entityPerm = (TopiaEntityPermissionImpl)getDAO().create("id", id); + entityPerm.init(id, principals, actions); + getDAO().update(entityPerm); + TopiaPermission perm = getCache().get(entityPerm); + return perm; + } + + private Map getCache() throws TopiaException { + if (permissionsCache == null) { + List entityPerms = getDAO().findAll(); + permissionsCache = new HashMap(); + if (entityPerms != null) { + for (TopiaEntityPermission entityPerm : entityPerms) { + permissionsCache.put(entityPerm, new TopiaPermission(entityPerm)); + } + } + } + return permissionsCache; + } + + public void update(TopiaPermission perm) throws TopiaException { + getDAO().update(perm.getEntityPermission()); + } + + public void delete(TopiaPermission perm) throws TopiaException { + getDAO().delete(perm.getEntityPermission()); + } + +} //TopiaHibernatePermissionManager Index: topia2/src/java/org/codelutin/topia/security/entities/hibernate/TopiaHibernateUserManager.java diff -u /dev/null topia2/src/java/org/codelutin/topia/security/entities/hibernate/TopiaHibernateUserManager.java:1.1 --- /dev/null Fri Feb 24 00:48:20 2006 +++ topia2/src/java/org/codelutin/topia/security/entities/hibernate/TopiaHibernateUserManager.java Fri Feb 24 00:48:15 2006 @@ -0,0 +1,137 @@ +/* *##% +* 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. +*##%*/ + +/* * +* TopiaHibernateUserManager.java +* +* Created: 10 févr. 2006 +* +* @author Arnaud Thimel +* @version $Revision$ +* +* Mise a jour: $Date$ +* par : $Author$ +*/ + + +/** + * + */ +package org.codelutin.topia.security.entities.hibernate; + +import static org.codelutin.topia.security.TopiaSecurityUtil.hash; + +import java.security.Principal; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.framework.TopiaContextImplementor; +import org.codelutin.topia.persistence.TopiaDAO; +import org.codelutin.topia.security.entities.TopiaPrincipal; +import org.codelutin.topia.security.entities.TopiaUser; +import org.codelutin.topia.security.entities.TopiaUserManager; + +/** + * Implantation Hibernate du TopiaUserManager + */ +public class TopiaHibernateUserManager implements TopiaUserManager { + + private TopiaDAO dao; + + private TopiaContextImplementor context; + + public TopiaHibernateUserManager(TopiaContext context) { + this.context = (TopiaContextImplementor)context; + } + + private TopiaDAO getDAO() throws TopiaException { + if (dao == null) { + dao = context.getDAO(TopiaUser.class); + } + return dao; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.security.TopiaUserManager#create(java.lang.String, java.lang.String, java.lang.String) + */ + public TopiaUser create(String login, String email, String password) throws TopiaException { + TopiaUser user = getDAO().create("login", login, "email", email); + user.setPassword(null, password); + update(user); + return user; + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.security.TopiaUserManager#update(java.lang.String, java.lang.String, java.lang.String) + */ + public void update(TopiaUser user) throws TopiaException { + getDAO().update(user); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.security.TopiaUserManager#delete(java.lang.String) + */ + public void delete(TopiaUser user) throws TopiaException { + getDAO().delete(user); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.security.TopiaUserManager#checkPassword(java.lang.String, java.lang.String) + */ + public boolean checkPassword(TopiaUser user, String password) throws TopiaException { + if (user == null) { + return false; + } + return user.isCorrectPassord(password); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.security.TopiaUserManager#findUserByEmail(java.lang.String) + */ + public List findUserByEmail(String email) throws TopiaException { + return getDAO().findAllByProperty("email", email); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.security.TopiaUserManager#findUserByLogin(java.lang.String) + */ + public TopiaUser findUserByLogin(String login) throws TopiaException { + return getDAO().findByProperty("login", login); + } + + /* (non-Javadoc) + * @see org.codelutin.topia.framework.security.TopiaUserManager#authenticate(java.lang.String, java.lang.String) + */ + public Set authenticate(String login, String password) throws TopiaException { + TopiaUser user = getDAO().findByProperties("login", login, "password", hash(password)); + Set principals = null; + if (user == null) { + throw new TopiaException("Echec d'authenfication pour l'utilisateur : " + login); + } else { + principals = new HashSet(); + principals.add(new TopiaPrincipal(user.getLogin())); + } + return principals; + } + +} //TopiaHibernateUserManager