r1193 - in trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security: credentials dao
Author: glandais Date: 2008-02-22 16:47:13 +0000 (Fri, 22 Feb 2008) New Revision: 1193 Added: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoActor.java trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoActorImpl.java trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoPermission.java trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoPermissionImpl.java Removed: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurity.java trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurityImpl.java Modified: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/credentials/CredentialManager.java trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/credentials/CredentialManagerImpl.java Log: Split of security DAO : actors and permissions (preparing for list filtering) Modified: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/credentials/CredentialManager.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/credentials/CredentialManager.java 2008-02-22 16:46:35 UTC (rev 1192) +++ trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/credentials/CredentialManager.java 2008-02-22 16:47:13 UTC (rev 1193) @@ -17,9 +17,12 @@ * ##% */ package fr.cemagref.simexplorer.is.security.credentials; +import java.util.List; + import javax.ejb.Local; import fr.cemagref.simexplorer.is.security.entities.Permission; +import fr.cemagref.simexplorer.is.security.entities.User; /** * The Interface CredentialManager. @@ -38,12 +41,37 @@ public Permission getPermission(String token, String businessId); /** + * Gets the permissions. + * + * @param uuid the uuid + * + * @return the permissions + */ + public Permission[] getPermissions(String uuid); + + /** * Save element. * * @param token the token * @param businessId the business id - * */ public void saveElement(String token, String businessId); + /** + * Sets the permissions. + * + * @param uuid the uuid + * @param permissions the permissions + */ + public void setPermissions(String uuid, Permission[] permissions); + + /** + * Gets the permissions owned by. + * + * @param user the user + * + * @return the permissions owned by + */ + public List<Permission> getPermissionsOwnedBy(User user); + } Modified: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/credentials/CredentialManagerImpl.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/credentials/CredentialManagerImpl.java 2008-02-22 16:46:35 UTC (rev 1192) +++ trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/credentials/CredentialManagerImpl.java 2008-02-22 16:47:13 UTC (rev 1193) @@ -25,7 +25,8 @@ import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; -import fr.cemagref.simexplorer.is.security.dao.DaoSecurity; +import fr.cemagref.simexplorer.is.security.dao.DaoActor; +import fr.cemagref.simexplorer.is.security.dao.DaoPermission; import fr.cemagref.simexplorer.is.security.entities.Actor; import fr.cemagref.simexplorer.is.security.entities.Group; import fr.cemagref.simexplorer.is.security.entities.Permission; @@ -41,8 +42,12 @@ /** The dao. */ @EJB - private DaoSecurity dao; + private DaoActor daoActor; + /** The dao. */ + @EJB + private DaoPermission daoPermission; + /** * Gets the relative level. * @@ -73,7 +78,7 @@ public Permission getPermission(String token, String businessId) { Permission p = null; - User user = dao.getLoggedUser(token); + User user = daoActor.getLoggedUser(token); if (user.isSuperAdmin()) { p = new Permission(); @@ -81,7 +86,7 @@ p.setBusinessId(businessId); p.setOwner(true); } else { - List<Permission> permissions = dao.getPermissions(businessId); + List<Permission> permissions = daoPermission.getPermissions(businessId); if (permissions.size() > 0) { int minLevel = 0; @@ -131,18 +136,44 @@ * @see fr.cemagref.simexplorer.is.security.credentials.CredentialManager#saveElement(java.lang.String, java.lang.String) */ public void saveElement(String token, String businessId) { - User user = dao.getLoggedUser(token); + User user = daoActor.getLoggedUser(token); Permission p = getPermission(token, businessId); if ((p == null) || (!user.getId().equals(p.getActor().getId()))) { p = new Permission(); - p.setActor(dao.getLoggedUser(token)); + p.setActor(user); p.setBusinessId(businessId); p.setOwner(true); - dao.savePermission(p); + daoPermission.savePermission(p); } else { p.setOwner(true); - dao.updatePermission(p); + daoPermission.updatePermission(p); } } + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.credentials.CredentialManager#getPermissions(java.lang.String) + */ + @Override + public Permission[] getPermissions(String uuid) { + List<Permission> permissions = daoPermission.getPermissions(uuid); + return permissions.toArray(new Permission[permissions.size()]); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.credentials.CredentialManager#setPermissions(java.lang.String, fr.cemagref.simexplorer.is.security.entities.Permission[]) + */ + @Override + public void setPermissions(String uuid, Permission[] permissions) { + daoPermission.setPermissions(uuid, permissions); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.credentials.CredentialManager#getPermissionsOwnedBy(fr.cemagref.simexplorer.is.security.entities.User) + */ + @Override + public List<Permission> getPermissionsOwnedBy(User user) { + List<Permission> permissionsOwnedBy = daoPermission.getPermissionsOwnedBy(user); + return permissionsOwnedBy; + } + } Copied: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoActor.java (from rev 1191, trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurity.java) =================================================================== --- trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoActor.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoActor.java 2008-02-22 16:47:13 UTC (rev 1193) @@ -0,0 +1,201 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* +* 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 fr.cemagref.simexplorer.is.security.dao; + +import java.util.List; + +import fr.cemagref.simexplorer.is.security.entities.Actor; +import fr.cemagref.simexplorer.is.security.entities.Group; +import fr.cemagref.simexplorer.is.security.entities.LoginAction; +import fr.cemagref.simexplorer.is.security.entities.Permission; +import fr.cemagref.simexplorer.is.security.entities.User; + +/** + * The Interface DaoActor. + */ +public interface DaoActor { + + /** + * Save user. + * + * @param user the user + */ + public void saveUser(User user); + + /** + * Gets the user. + * + * @param id the id + * + * @return the user + */ + public User getUser(Integer id); + + /** + * Gets the user. + * + * @param login the login + * + * @return the user + */ + public User getUser(String login); + + /** + * Update user. + * + * @param user the user + * + * @return the user + */ + public User updateUser(User user); + + /** + * Delete user. + * + * @param id the id + */ + public void deleteUser(Integer id); + + /** + * Save group. + * + * @param group the group + */ + public void saveGroup(Group group); + + /** + * Gets the group. + * + * @param id the id + * + * @return the group + */ + public Group getGroup(Integer id); + + /** + * Gets the group. + * + * @param name the name + * + * @return the group + */ + public Group getGroup(String name); + + /** + * Update group. + * + * @param group the group + * + * @return the group + */ + public Group updateGroup(Group group); + + /** + * Delete group. + * + * @param id the id + */ + public void deleteGroup(Integer id); + + /** + * Gets the logged user. + * + * @param token the token + * + * @return the logged user + */ + public User getLoggedUser(String token); + + /** + * Login user. + * + * @param login the login + * @param passwordHash the password hash + * + * @return the user + */ + public User loginUser(String login, String passwordHash); + + /** + * Save token. + * + * @param loginAction the login action + */ + public void saveToken(LoginAction loginAction); + + /** + * Delete old tokens associated to user. + * + * @param login Login of user + * @param before ms before now + */ + public void deleteTokens(String login, long before); + + /** + * Gets the groups. + * + * @return the groups + */ + public List<Group> getGroups(); + + /** + * Gets the groups owned by. + * + * @param user the user + * + * @return the groups owned by + */ + public List<Group> getGroupsOwnedBy(User user); + + /** + * Gets the users. + * + * @return the users + */ + public List<User> getUsers(); + + /** + * Gets the users of group. + * + * @param group the group + * + * @return the users of group + */ + public List<User> getUsersOfGroup(Group group); + + /** + * Gets the groups of user. + * + * @param user the user + * + * @return the groups of user + */ + public List<Group> getGroupsOfUser(User user); + + /** + * Gets the groups of group. + * + * @param group the group + * + * @return the groups of group + */ + public List<Group> getGroupsOfGroup(Group group); + + + +} Copied: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoActorImpl.java (from rev 1191, trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurityImpl.java) =================================================================== --- trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoActorImpl.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoActorImpl.java 2008-02-22 16:47:13 UTC (rev 1193) @@ -0,0 +1,261 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* +* 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 fr.cemagref.simexplorer.is.security.dao; + +import java.util.Date; +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import org.codelutin.util.CollectionUtil; + +import fr.cemagref.simexplorer.is.security.entities.Actor; +import fr.cemagref.simexplorer.is.security.entities.Group; +import fr.cemagref.simexplorer.is.security.entities.LoginAction; +import fr.cemagref.simexplorer.is.security.entities.Permission; +import fr.cemagref.simexplorer.is.security.entities.User; + +/** + * The Class DaoActorImpl. + */ + at Stateless(name = "DaoActor") +public class DaoActorImpl implements DaoActor { + + /** The em. */ + @PersistenceContext(unitName = "simexploreris-security") + private EntityManager em; + + /** + * Gets the single result. + * + * @param query the query + * + * @return the single result + */ + private Object getSingleResult(Query query) { + Object o; + try { + o = query.getSingleResult(); + } catch (NoResultException e) { + o = null; + } + return o; + } + + /** + * Find. + * + * @param clazz the clazz + * @param arg the arg + * + * @return the object + */ + private Object find(Class<?> clazz, Object arg) { + Object o; + try { + o = em.find(clazz, arg); + } catch (NoResultException e) { + o = null; + } + return o; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#deleteGroup(java.lang.Integer) + */ + public void deleteGroup(Integer id) { + Group g = getGroup(id); + g.setVisible(false); + em.merge(g); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#deleteUser(java.lang.Integer) + */ + public void deleteUser(Integer id) { + User u = getUser(id); + u.setVisible(false); + em.merge(u); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getGroup(java.lang.Integer) + */ + public Group getGroup(Integer id) { + Group g = (Group) find(Group.class, id); + return g; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getGroup(java.lang.String) + */ + public Group getGroup(String name) { + Group g = (Group) getSingleResult(em.createQuery("select g from Group g where g.name=:name").setParameter( + "name", name)); + return g; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getUser(java.lang.Integer) + */ + public User getUser(Integer id) { + User u = (User) find(User.class, id); + return u; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#saveGroup(fr.cemagref.simexplorer.is.security.entities.Group) + */ + public void saveGroup(Group group) { + em.persist(group); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#saveUser(fr.cemagref.simexplorer.is.security.entities.User) + */ + public void saveUser(User user) { + em.persist(user); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#updateGroup(fr.cemagref.simexplorer.is.security.entities.Group) + */ + public Group updateGroup(Group group) { + return em.merge(group); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#updateUser(fr.cemagref.simexplorer.is.security.entities.User) + */ + public User updateUser(User user) { + return em.merge(user); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getLoggedUser(java.lang.String) + */ + public User getLoggedUser(String token) { + User loggedUser = null; + LoginAction loginAction = (LoginAction) getSingleResult(em.createQuery( + "select la from LoginAction la where la.token=:token").setParameter("token", token)); + if (loginAction != null) { + loggedUser = loginAction.getLoggedUser(); + loginAction.setTime(new Date()); + em.merge(loginAction); + } + return loggedUser; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#loginUser(java.lang.String, java.lang.String) + */ + public User loginUser(String login, String passwordHash) { + User user = (User) getSingleResult(em.createQuery( + "select u from User u where u.login=:login and u.passwordHash=:passwordHash").setParameter("login", + login).setParameter("passwordHash", passwordHash)); + return user; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#saveToken(fr.cemagref.simexplorer.is.security.entities.LoginAction) + */ + public void saveToken(LoginAction loginAction) { + em.persist(loginAction); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#deleteTokens(java.lang.String, long) + */ + public void deleteTokens(String login, long before) { + Date now = new Date(); + Date date = new Date(now.getTime() - before); + em.createQuery("delete from LoginAction la where la.loggedUser.login = :login and la.time < :time") + .setParameter("time", date).setParameter("login", login).executeUpdate(); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getUser(java.lang.String) + */ + public User getUser(String login) { + User user = (User) getSingleResult(em.createQuery("select u from User u where u.login=:login").setParameter( + "login", login)); + return user; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getGroups() + */ + public List<Group> getGroups() { + List<Group> groups = CollectionUtil.toGenericList(em.createQuery("select g from Group g").getResultList(), + Group.class); + return groups; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getGroupsOwnedBy(fr.cemagref.simexplorer.is.security.entities.User) + */ + public List<Group> getGroupsOwnedBy(User user) { + List<Group> groups = CollectionUtil.toGenericList(em.createQuery("select g from Group g where g.owner=:owner") + .setParameter("owner", user).getResultList(), Group.class); + return groups; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getUsers() + */ + public List<User> getUsers() { + List<User> users = CollectionUtil.toGenericList(em.createQuery("select u from User u").getResultList(), + User.class); + return users; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getUsersOfGroup(fr.cemagref.simexplorer.is.security.entities.Group) + */ + public List<User> getUsersOfGroup(Group group) { + List<User> users = CollectionUtil.toGenericList(em.createQuery( + "select distinct u from User u where :group member of u.groups").setParameter("group", group) + .getResultList(), User.class); + return users; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getGroupsOfUser(fr.cemagref.simexplorer.is.security.entities.User) + */ + public List<Group> getGroupsOfUser(User user) { + List<Group> groups = CollectionUtil.toGenericList(em.createQuery( + "select distinct u.groups from User u where u = :user").setParameter("user", user).getResultList(), + Group.class); + return groups; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getGroupsOfGroup(fr.cemagref.simexplorer.is.security.entities.Group) + */ + public List<Group> getGroupsOfGroup(Group group) { + List<Group> groups = CollectionUtil.toGenericList(em.createQuery( + "select distinct g.groups from Group g where g = :group").setParameter("group", group).getResultList(), + Group.class); + return groups; + } + +} Added: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoPermission.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoPermission.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoPermission.java 2008-02-22 16:47:13 UTC (rev 1193) @@ -0,0 +1,77 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* +* 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 fr.cemagref.simexplorer.is.security.dao; + +import java.util.List; + +import fr.cemagref.simexplorer.is.security.entities.Actor; +import fr.cemagref.simexplorer.is.security.entities.Permission; + +public interface DaoPermission { + + /** + * Gets the permissions. + * + * @param businessId the business id + * + * @return the permissions + */ + public List<Permission> getPermissions(String businessId); + + /** + * Gets the permissions. + * + * @param businessId the business id + * @param actor the actor + * + * @return the permissions + */ + public List<Permission> getPermissions(Actor actor, String businessId); + + /** + * Gets the permissions. + * + * @param actor the actor + * + * @return the permissions + */ + public List<Permission> getPermissionsOwnedBy(Actor actor); + + /** + * Save permission. + * + * @param p the p + */ + public void savePermission(Permission p); + + /** + * Update permission. + * + * @param p the p + */ + public void updatePermission(Permission p); + + /** + * Sets the permissions. + * + * @param uuid the uuid + * @param permissions the permissions + */ + public void setPermissions(String uuid, Permission[] permissions); + +} Added: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoPermissionImpl.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoPermissionImpl.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoPermissionImpl.java 2008-02-22 16:47:13 UTC (rev 1193) @@ -0,0 +1,101 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* +* 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 fr.cemagref.simexplorer.is.security.dao; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.codelutin.util.CollectionUtil; + +import fr.cemagref.simexplorer.is.security.entities.Actor; +import fr.cemagref.simexplorer.is.security.entities.Permission; + +/** + * The Class DaoPermissionImpl. + */ + at Stateless(name = "DaoPermission") +public class DaoPermissionImpl implements DaoPermission { + + /** The em. */ + @PersistenceContext(unitName = "simexploreris-security") + private EntityManager em; + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getPermissions(java.lang.String) + */ + @Override + public List<Permission> getPermissions(String businessId) { + List<Permission> permissions = CollectionUtil.toGenericList(em.createQuery( + "select p from Permission p where p.businessId=:businessId").setParameter("businessId", businessId) + .getResultList(), Permission.class); + return permissions; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#getPermissions(fr.cemagref.simexplorer.is.security.entities.Actor, java.lang.String) + */ + @Override + public List<Permission> getPermissions(Actor actor, String businessId) { + List<Permission> permissions = CollectionUtil.toGenericList(em.createQuery( + "select p from Permission p where p.businessId=:businessId and p.actor=:actor").setParameter( + "businessId", businessId).setParameter("actor", actor).getResultList(), Permission.class); + return permissions; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoPermission#getPermissionsOwnedBy(fr.cemagref.simexplorer.is.security.entities.Actor) + */ + @Override + public List<Permission> getPermissionsOwnedBy(Actor actor) { + List<Permission> permissions = CollectionUtil.toGenericList(em.createQuery( + "select p from Permission p where p.actor=:actor and p.owner = true").setParameter("actor", actor) + .getResultList(), Permission.class); + return permissions; + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#savePermission(fr.cemagref.simexplorer.is.security.entities.Permission) + */ + @Override + public void savePermission(Permission p) { + em.persist(p); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoActor#setPermissions(java.lang.String, fr.cemagref.simexplorer.is.security.entities.Permission[]) + */ + @Override + public void setPermissions(String uuid, Permission[] permissions) { + em.createQuery("delete from Permission p where p.businessId=:businessId").setParameter("businessId", uuid) + .executeUpdate(); + for (Permission permission : permissions) { + em.persist(permission); + } + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.security.dao.DaoPermission#updatePermission(fr.cemagref.simexplorer.is.security.entities.Permission) + */ + @Override + public void updatePermission(Permission p) { + em.merge(p); + } +} Deleted: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurity.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurity.java 2008-02-22 16:46:35 UTC (rev 1192) +++ trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurity.java 2008-02-22 16:47:13 UTC (rev 1193) @@ -1,249 +0,0 @@ -/* -* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais -* -* 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 fr.cemagref.simexplorer.is.security.dao; - -import java.util.List; - -import fr.cemagref.simexplorer.is.security.entities.Actor; -import fr.cemagref.simexplorer.is.security.entities.Group; -import fr.cemagref.simexplorer.is.security.entities.LoginAction; -import fr.cemagref.simexplorer.is.security.entities.Permission; -import fr.cemagref.simexplorer.is.security.entities.User; - -/** - * The Interface DaoSecurity. - */ -public interface DaoSecurity { - - /** - * Save user. - * - * @param user the user - */ - public void saveUser(User user); - - /** - * Gets the user. - * - * @param id the id - * - * @return the user - */ - public User getUser(Integer id); - - /** - * Gets the user. - * - * @param login the login - * - * @return the user - */ - public User getUser(String login); - - /** - * Update user. - * - * @param user the user - * - * @return the user - */ - public User updateUser(User user); - - /** - * Delete user. - * - * @param id the id - */ - public void deleteUser(Integer id); - - /** - * Save group. - * - * @param group the group - */ - public void saveGroup(Group group); - - /** - * Gets the group. - * - * @param id the id - * - * @return the group - */ - public Group getGroup(Integer id); - - /** - * Gets the group. - * - * @param name the name - * - * @return the group - */ - public Group getGroup(String name); - - /** - * Update group. - * - * @param group the group - * - * @return the group - */ - public Group updateGroup(Group group); - - /** - * Delete group. - * - * @param id the id - */ - public void deleteGroup(Integer id); - - /** - * Gets the logged user. - * - * @param token the token - * - * @return the logged user - */ - public User getLoggedUser(String token); - - /** - * Login user. - * - * @param login the login - * @param passwordHash the password hash - * - * @return the user - */ - public User loginUser(String login, String passwordHash); - - /** - * Save token. - * - * @param loginAction the login action - */ - public void saveToken(LoginAction loginAction); - - /** - * Delete old tokens associated to user. - * - * @param login Login of user - * @param before ms before now - */ - public void deleteTokens(String login, long before); - - /** - * Gets the groups. - * - * @return the groups - */ - public List<Group> getGroups(); - - /** - * Gets the groups owned by. - * - * @param user the user - * - * @return the groups owned by - */ - public List<Group> getGroupsOwnedBy(User user); - - /** - * Gets the users. - * - * @return the users - */ - public List<User> getUsers(); - - /** - * Gets the users of group. - * - * @param group the group - * - * @return the users of group - */ - public List<User> getUsersOfGroup(Group group); - - /** - * Gets the groups of user. - * - * @param user the user - * - * @return the groups of user - */ - public List<Group> getGroupsOfUser(User user); - - /** - * Gets the groups of group. - * - * @param group the group - * - * @return the groups of group - */ - public List<Group> getGroupsOfGroup(Group group); - - /** - * Gets the permissions. - * - * @param businessId the business id - * - * @return the permissions - */ - public List<Permission> getPermissions(String businessId); - - /** - * Gets the permissions. - * - * @param businessId the business id - * @param actor the actor - * - * @return the permissions - */ - public List<Permission> getPermissions(Actor actor, String businessId); - - /** - * Gets the permissions. - * - * @param actor the actor - * - * @return the permissions - */ - public List<Permission> getPermissionsOwnedBy(Actor actor); - - /** - * Save permission. - * - * @param p the p - */ - public void savePermission(Permission p); - - /** - * Update permission. - * - * @param p the p - */ - public void updatePermission(Permission p); - - /** - * Sets the permissions. - * - * @param uuid the uuid - * @param permissions the permissions - */ - public void setPermissions(String uuid, Permission[] permissions); - -} Deleted: trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurityImpl.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurityImpl.java 2008-02-22 16:46:35 UTC (rev 1192) +++ trunk/simexplorer-is/simexplorer-is-security/src/java/fr/cemagref/simexplorer/is/security/dao/DaoSecurityImpl.java 2008-02-22 16:47:13 UTC (rev 1193) @@ -1,312 +0,0 @@ -/* -* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais -* -* 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 fr.cemagref.simexplorer.is.security.dao; - -import java.util.Date; -import java.util.List; - -import javax.ejb.Stateless; -import javax.persistence.EntityManager; -import javax.persistence.NoResultException; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; - -import org.codelutin.util.CollectionUtil; - -import fr.cemagref.simexplorer.is.security.entities.Actor; -import fr.cemagref.simexplorer.is.security.entities.Group; -import fr.cemagref.simexplorer.is.security.entities.LoginAction; -import fr.cemagref.simexplorer.is.security.entities.Permission; -import fr.cemagref.simexplorer.is.security.entities.User; - -/** - * The Class DaoSecurityImpl. - */ - at Stateless(name = "DaoSecurity") -public class DaoSecurityImpl implements DaoSecurity { - - /** The em. */ - @PersistenceContext(unitName = "simexploreris-security") - private EntityManager em; - - /** - * Gets the single result. - * - * @param query the query - * - * @return the single result - */ - private Object getSingleResult(Query query) { - Object o; - try { - o = query.getSingleResult(); - } catch (NoResultException e) { - o = null; - } - return o; - } - - /** - * Find. - * - * @param clazz the clazz - * @param arg the arg - * - * @return the object - */ - private Object find(Class<?> clazz, Object arg) { - Object o; - try { - o = em.find(clazz, arg); - } catch (NoResultException e) { - o = null; - } - return o; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#deleteGroup(java.lang.Integer) - */ - public void deleteGroup(Integer id) { - Group g = getGroup(id); - g.setVisible(false); - em.merge(g); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#deleteUser(java.lang.Integer) - */ - public void deleteUser(Integer id) { - User u = getUser(id); - u.setVisible(false); - em.merge(u); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getGroup(java.lang.Integer) - */ - public Group getGroup(Integer id) { - Group g = (Group) find(Group.class, id); - return g; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getGroup(java.lang.String) - */ - public Group getGroup(String name) { - Group g = (Group) getSingleResult(em.createQuery("select g from Group g where g.name=:name").setParameter( - "name", name)); - return g; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getUser(java.lang.Integer) - */ - public User getUser(Integer id) { - User u = (User) find(User.class, id); - return u; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#saveGroup(fr.cemagref.simexplorer.is.security.entities.Group) - */ - public void saveGroup(Group group) { - em.persist(group); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#saveUser(fr.cemagref.simexplorer.is.security.entities.User) - */ - public void saveUser(User user) { - em.persist(user); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#updateGroup(fr.cemagref.simexplorer.is.security.entities.Group) - */ - public Group updateGroup(Group group) { - return em.merge(group); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#updateUser(fr.cemagref.simexplorer.is.security.entities.User) - */ - public User updateUser(User user) { - return em.merge(user); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getLoggedUser(java.lang.String) - */ - public User getLoggedUser(String token) { - User loggedUser = null; - LoginAction loginAction = (LoginAction) getSingleResult(em.createQuery( - "select la from LoginAction la where la.token=:token").setParameter("token", token)); - if (loginAction != null) { - loggedUser = loginAction.getLoggedUser(); - loginAction.setTime(new Date()); - em.merge(loginAction); - } - return loggedUser; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#loginUser(java.lang.String, java.lang.String) - */ - public User loginUser(String login, String passwordHash) { - User user = (User) getSingleResult(em.createQuery( - "select u from User u where u.login=:login and u.passwordHash=:passwordHash").setParameter("login", - login).setParameter("passwordHash", passwordHash)); - return user; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#saveToken(fr.cemagref.simexplorer.is.security.entities.LoginAction) - */ - public void saveToken(LoginAction loginAction) { - em.persist(loginAction); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#deleteTokens(java.lang.String, long) - */ - public void deleteTokens(String login, long before) { - Date now = new Date(); - Date date = new Date(now.getTime() - before); - em.createQuery("delete from LoginAction la where la.loggedUser.login = :login and la.time < :time") - .setParameter("time", date).setParameter("login", login).executeUpdate(); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getUser(java.lang.String) - */ - public User getUser(String login) { - User user = (User) getSingleResult(em.createQuery("select u from User u where u.login=:login").setParameter( - "login", login)); - return user; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getGroups() - */ - public List<Group> getGroups() { - List<Group> groups = CollectionUtil.toGenericList(em.createQuery("select g from Group g").getResultList(), - Group.class); - return groups; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getGroupsOwnedBy(fr.cemagref.simexplorer.is.security.entities.User) - */ - public List<Group> getGroupsOwnedBy(User user) { - List<Group> groups = CollectionUtil.toGenericList(em.createQuery("select g from Group g where g.owner=:owner") - .setParameter("owner", user).getResultList(), Group.class); - return groups; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getUsers() - */ - public List<User> getUsers() { - List<User> users = CollectionUtil.toGenericList(em.createQuery("select u from User u").getResultList(), - User.class); - return users; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getUsersOfGroup(fr.cemagref.simexplorer.is.security.entities.Group) - */ - public List<User> getUsersOfGroup(Group group) { - List<User> users = CollectionUtil.toGenericList(em.createQuery( - "select distinct u from User u where :group member of u.groups").setParameter("group", group) - .getResultList(), User.class); - return users; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getGroupsOfUser(fr.cemagref.simexplorer.is.security.entities.User) - */ - public List<Group> getGroupsOfUser(User user) { - List<Group> groups = CollectionUtil.toGenericList(em.createQuery( - "select distinct u.groups from User u where u = :user").setParameter("user", user).getResultList(), - Group.class); - return groups; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getGroupsOfGroup(fr.cemagref.simexplorer.is.security.entities.Group) - */ - public List<Group> getGroupsOfGroup(Group group) { - List<Group> groups = CollectionUtil.toGenericList(em.createQuery( - "select distinct g.groups from Group g where g = :group").setParameter("group", group).getResultList(), - Group.class); - return groups; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getPermissions(java.lang.String) - */ - public List<Permission> getPermissions(String businessId) { - List<Permission> permissions = CollectionUtil.toGenericList(em.createQuery( - "select p from Permission p where p.businessId=:businessId").setParameter("businessId", businessId) - .getResultList(), Permission.class); - return permissions; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#getPermissions(fr.cemagref.simexplorer.is.security.entities.Actor, java.lang.String) - */ - public List<Permission> getPermissions(Actor actor, String businessId) { - List<Permission> permissions = CollectionUtil.toGenericList(em.createQuery( - "select p from Permission p where p.businessId=:businessId and p.actor=:actor").setParameter( - "businessId", businessId).setParameter("actor", actor).getResultList(), Permission.class); - return permissions; - } - - @Override - public List<Permission> getPermissionsOwnedBy(Actor actor) { - List<Permission> permissions = CollectionUtil.toGenericList(em.createQuery( - "select p from Permission p where p.actor=:actor and p.owner = true").setParameter("actor", actor) - .getResultList(), Permission.class); - return permissions; - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#savePermission(fr.cemagref.simexplorer.is.security.entities.Permission) - */ - public void savePermission(Permission p) { - em.persist(p); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.security.dao.DaoSecurity#setPermissions(java.lang.String, fr.cemagref.simexplorer.is.security.entities.Permission[]) - */ - public void setPermissions(String uuid, Permission[] permissions) { - em.createQuery("delete from Permission p where p.businessId=:businessId").setParameter("businessId", uuid) - .executeUpdate(); - for (Permission permission : permissions) { - em.persist(permission); - } - } - - @Override - public void updatePermission(Permission p) { - em.merge(p); - } - -}
participants (1)
-
glandais@users.labs.libre-entreprise.org