AppFuseAcegiACLAddDAO |
|
Your trail: |
Step III: Adding new DAOs for ACL
Add new Interfaces:
BasicAclObjectIdentityDAO
- Create a new file called BasicAclObjectIdentityDAO.java
- Add the following content:
package org.appfuse.dao;
import org.appfuse.model.BasicAclObjectIdentity;
public interface BasicAclObjectIdentityDAO extends Dao
{
/**
* Saves a object identity to persistent storage
* @param basicAclObjectIdentity Identity to save
*/
public void saveBasicAclObjectIdentity(BasicAclObjectIdentity basicAclObjectIdentity);
/**
* Removes an object identity from persistent storage
* @param id ID of hte identity to remove
*/
public void deleteBasicAclObjectIdentity(Long id);
/**
* Loads an object identity with the id <code>id</code> from persistent storage
* @param id ID of the identity to load
* @return An object identity with the id <code>id</code>
*/
public BasicAclObjectIdentity getBasicAclObjectIdentity(Long id);
/**
* Loads an object identity by a string representing the Object (e.g. CLASSNAME:PRIMARY_KEY)
* @param obejctIdentity The identity (e.g. CLASSNAME:PRIMARY_KEY) of the object
* @return An object identity
*/
public BasicAclObjectIdentity getBasicAclObjectIdentity(String obejctIdentity);
}
|
BasicAclPermissionDAO
- Create a file called BasicAclPermissionDAO.java
- Add the following code to the new file:
package org.appfuse.dao;
import java.util.List;
import java.util.Set;
import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.acl.basic.BasicAclDao;
import org.appfuse.model.BasicAclPermission;
public interface BasicAclPermissionDAO extends Dao, BasicAclDao
{
/**
* Saves a permission to the persistent storage
* @param basicACLPermission Permission to save
*/
public void saveBasicAclPermission(BasicAclPermission basicACLPermission);
/**
* Loads a permission from persistent storage by id
* @param id ID of the permission to load
* @return A permission with the id id
*/
public BasicAclPermission getBasicAclPermission(Long id);
/**
* Removes a permission by the id
* @param id Id of the permission to delete
*/
public void deleteBasicAclPermission(Long id);
/**
* Loads all permissions an user represented by the authentication <code>auth</code> has on an object represented by the <code>objectIdentity</code>
* @param objectIdentity String representing an unique instance of a class
* @param auth Authentication represetning the user
* @return List of permissions a user has on a specific object
*/
public Set getBasicAclPermissions(String objectIdentity, Authentication auth);
/**
* Returns a list of permissions an user (and only the user) with the username <code>username</code> has on an object represented by the <code>objectIdentity</code>
* @param objectIdentity String representing an unique instance of a class
* @param username Username of the user
* @return List of permissions a user has directly on an object
*/
public List getBasicAclPermissions(String objectIdentity, String username);
/**
* Returns a list of permissions the Authorities have on an object represented by the <code>objectIdentity</code>
* @param objectIdentity String representing an unique instance of a class
* @param authorities Array of authorities
* @return List of permissions
*/
public List getBasicAclPermissions(String objectIdentity, GrantedAuthority[] authorities);
/**
* Returns all Permissions granted on an object represented by <code>objectIdentity</code>
* @param objectIdentity String representing an unique instance of a class
* @return All Permissions granted on an object represented by <code>objectIdentity</code>
*/
public List getBasicAclPermissions(String objectIdentity);
/**
* Returns all permissions granted for an user with the username <code>username</code>
* @param username Username of the user
* @return List of permissions
*/
public List getBasicAclPermissionsByUsername(String username);
}
|
Add the Hibernate implementations:
- Create the corresponding Hibernate implementations and add the follwing content to the files:
BasicAclObjectIdentityDAOHibernate
package org.appfuse.dao.hibernate;
import org.appfuse.dao.BasicAclObjectIdentityDAO;
import org.appfuse.model.BasicAclObjectIdentity;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.springframework.orm.ObjectRetrievalFailureException;
public class BasicAclObjectIdentityDAOHibernate extends BaseDaoHibernate implements BasicAclObjectIdentityDAO
{
/**
* @see org.appfuse.dao.BasicAclObjectIdentityDAO#saveBasicAclObjectIdentity(org.appfuse.model.BasicAclObjectIdentity)
*/
public void saveBasicAclObjectIdentity(BasicAclObjectIdentity basicAclObjectIdentity)
{
getHibernateTemplate().saveOrUpdate(basicAclObjectIdentity);
}
/**
* @see org.appfuse.dao.BasicAclObjectIdentityDAO#deleteBasicAclObjectIdentity(java.lang.Long)
*/
public void deleteBasicAclObjectIdentity(Long id)
{
getHibernateTemplate().delete(getBasicAclObjectIdentity(id));
}
/**
* @see org.appfuse.dao.BasicAclObjectIdentityDAO#getBasicAclObjectIdentity(java.lang.Long)
*/
public BasicAclObjectIdentity getBasicAclObjectIdentity(Long id)
{
BasicAclObjectIdentity basicAclObjectIdentity = (BasicAclObjectIdentity) getHibernateTemplate().get(BasicAclObjectIdentity.class,id);
if (basicAclObjectIdentity == null)
{
log.warn("uh oh, basicAclObjectIdentity '" + id + "' not found...");
throw new ObjectRetrievalFailureException(BasicAclObjectIdentity.class,id);
}
return basicAclObjectIdentity;
}
/**
* @see org.appfuse.dao.BasicAclObjectIdentityDAO#getBasicAclObjectIdentity(java.lang.String)
*/
public BasicAclObjectIdentity getBasicAclObjectIdentity(
String objectIdentity)
{
Query query = getSession().createQuery("select acl from BasicAclObjectIdentity as acl where acl.objectIdentity=:identity");
query.setParameter("identity", objectIdentity, Hibernate.STRING);
Object result = query.uniqueResult();
if (result == null)
{
throw new ObjectRetrievalFailureException(BasicAclObjectIdentity.class,objectIdentity);
}
return (BasicAclObjectIdentity) result;
}
}
|
BasicAclPermissionDAOHibernate
package org.appfuse.dao.hibernate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.acl.basic.AclObjectIdentity;
import org.acegisecurity.acl.basic.BasicAclEntry;
import org.acegisecurity.userdetails.UserDetails;
import org.appfuse.dao.BasicAclPermissionDAO;
import org.appfuse.model.BasicAclObjectIdentity;
import org.appfuse.model.BasicAclPermission;
import org.hibernate.Hibernate;
import org.hibernate.Query;
public class BasicAclPermissionDAOHibernate extends BaseDaoHibernate implements BasicAclPermissionDAO {
/**
* @see org.appfuse.dao.BasicAclPermissionDAO#saveBasicAclPermission(org.appfuse.model.BasicAclPermission)
*/
public void saveBasicAclPermission(BasicAclPermission basicACLPermission)
{
getHibernateTemplate().saveOrUpdate(basicACLPermission);
}
/**
* @see org.appfuse.dao.BasicAclPermissionDAO#getBasicAclPermission(java.lang.Long)
*/
public BasicAclPermission getBasicAclPermission(Long id)
{
return (BasicAclPermission)getHibernateTemplate().get(BasicAclPermission.class,id);
}
/**
* @see org.appfuse.dao.BasicAclPermissionDAO#deleteBasicAclPermission(java.lang.Long)
*/
public void deleteBasicAclPermission(Long id)
{
getHibernateTemplate().delete(getBasicAclPermission(id));
}
/**
* @see org.appfuse.dao.BasicAclPermissionDAO#getBasicAclPermissions(java.lang.String, org.acegisecurity.Authentication)
*/
public Set getBasicAclPermissions(String objectIdentity, Authentication auth)
{
Set toReturn = new HashSet();
String username = "";
if (auth.getPrincipal() instanceof String)
{
username = (String) auth.getPrincipal();
}
else if(auth.getPrincipal() instanceof UserDetails)
{
username = ((UserDetails)auth.getPrincipal()).getUsername();
}
List userpermissions = getBasicAclPermissions(objectIdentity,username);
log.debug("Found ["+userpermissions.size()+"] permissions for user ["+username+"]");
toReturn.addAll(userpermissions);
List authorityPermissions = getBasicAclPermissions(objectIdentity,auth.getAuthorities());
toReturn.addAll(authorityPermissions);
return toReturn;
}
/**
* @see org.appfuse.dao.BasicAclPermissionDAO#getBasicAclPermissions(java.lang.String, java.lang.String)
*/
public List getBasicAclPermissions(String objectIdentity, String username)
{
String queryString = "from BasicAclPermission as p WHERE p.basicAclObjectIdentity.objectIdentity=:identity AND p.objRecipient=:username";
Query query = getSession().createQuery(queryString);
query.setParameter("identity",objectIdentity,Hibernate.STRING);
query.setParameter("username", username,Hibernate.STRING);
List usernameResults = query.list();
return usernameResults;
}
/**
* @see org.appfuse.dao.BasicAclPermissionDAO#getBasicAclPermissions(java.lang.String, org.acegisecurity.GrantedAuthority[])
*/
public List getBasicAclPermissions(String objectIdentity, GrantedAuthority[] authorities)
{
String queryString = "from BasicAclPermission as p WHERE p.basicAclObjectIdentity.objectIdentity=:identity AND p.objRecipient=:rolename";
Set result = new HashSet();
for (int i = 0; i < authorities.length; i++)
{
GrantedAuthority authority = authorities[i];
Query query = getSession().createQuery(queryString);
query.setParameter("identity", objectIdentity, Hibernate.STRING);
query.setParameter("rolename", authority.getAuthority(), Hibernate.STRING);
List authorityResults = query.list();
result.addAll(authorityResults);
}
List toReturn = new ArrayList();
toReturn.addAll(result);
return toReturn;
}
/**
* @see org.appfuse.dao.BasicAclPermissionDAO#getBasicAclPermissions(java.lang.String)
*/
public List getBasicAclPermissions(String objectIdentity)
{
String queryString = "from BasicAclPermission as p where p.basicAclObjectIdentity.objectIdentity=:identity";
Query query = getSession().createQuery(queryString);
query.setParameter("identity",objectIdentity,Hibernate.STRING);
List results = query.list();
return results;
}
/**
* @see org.appfuse.dao.BasicAclPermissionDAO#getBasicAclPermissionsByUsername(java.lang.String)
*/
public List getBasicAclPermissionsByUsername(String username) {
String queryString = "select permission from BasicAclPermission as permission where permission.objRecipient=:username";
Query query = getSession().createQuery(queryString);
query.setParameter("username",username,Hibernate.STRING);
List results = query.list();
if(log.isDebugEnabled())
{
log.debug("Found ["+results.size()+"] BasicACLPermissions");
}
return results;
}
/**
* @see org.acegisecurity.acl.basic.BasicAclDao#getAcls(org.acegisecurity.acl.basic.AclObjectIdentity)
*/
public BasicAclEntry[] getAcls(AclObjectIdentity aclObjectIdentity)
{
if (aclObjectIdentity instanceof BasicAclObjectIdentity)
{
BasicAclObjectIdentity oid = (BasicAclObjectIdentity) aclObjectIdentity;
List entries = getBasicAclPermissions(oid.toString());
return (BasicAclEntry[]) entries.toArray(new BasicAclEntry[]{});
}
return null;
}
}
|
Add the new Hibernate implementations to the applicationContext-hibernate.xml
- Open the file applicationContext-hibernate.xml and add the followign part:
<!-- BasicAclObjectIdentityDAO: Hibernate implementation -->
<bean id="basicAclObjectIdentityDAO" class="org.appfuse.dao.hibernate.BasicAclObjectIdentityDAOHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- BasicAclPermissionDAO: Hibernate implementation -->
<bean id="basicAclPermissionDAO" class="org.appfuse.dao.hibernate.BasicAclPermissionDAOHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Next Step:
Step IV. Add new Managers
|