AppFuseAcegiACLAddModelsAndHibernate |
|
Your trail: |
Step II: Add new models and DAOs
Add new abstract BaseObjectACLAware.java
- Add a file called BaseObjectACLAware.java to your model folder
- Add the following content to file (I assume that you don't have changed the package structure of the apfuse project)
package org.appfuse.model;
public abstract class BaseObjectACLAware extends BaseObject
{
/**
* This methods returns the unique key of the object (the primary key)
* @return Unique key of the object (the primary key)
*/
public abstract Object getUniqueKey();
}
|
Add new domain objects for ACL security
First we need to create 2 new classes to represent object identities and permissions od suers / roles on these object indentities
Object identity
- Add a new class called BasicAclObjectIdentity.java to your model folder.
package org.appfuse.model;
import java.util.HashSet;
import java.util.Set;
import org.acegisecurity.acl.basic.AclObjectIdentity;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* @hibernate.class table="acl_object_identity"
*/
public class BasicAclObjectIdentity extends BaseObject implements AclObjectIdentity
{
private Long id;
private String objectIdentity;
private String aclClass;
private BasicAclObjectIdentity parentObject;
private Set permissions = new HashSet();
public BasicAclObjectIdentity()
{
super();
}
public BasicAclObjectIdentity(String identity)
{
super();
objectIdentity = identity;
}
/**
* @hibernate.id column="id" generator-class="increment"
* unsaved-value="null"
*/
public Long getId()
{
return id;
}
/**
* @hibernate.property column="object_identity" length="60" not-null="true"
*
*/
public String getObjectIdentity()
{
return objectIdentity;
}
/**
* @hibernate.property length="50" column="acl_class" not-null="true"
*/
public String getAclClass()
{
return aclClass;
}
public void setId(Long id)
{
this.id = id;
}
public void setObjectIdentity(String objectIdentity)
{
this.objectIdentity = objectIdentity;
}
public void setAclClass(String aclClass)
{
this.aclClass = aclClass;
}
/**
* @hibernate.many-to-one column="parent_object"
* class="org.appfuse.model.BasicAclObjectIdentity"
* cascade="none"
*/
public BasicAclObjectIdentity getParentObject()
{
return parentObject;
}
/**
* @hibernate.set name="permissions" cascade="all" inverse="true"
* @hibernate.collection-key column="acl_object_identity"
* @hibernate.collection-one-to-many class="org.appfuse.model.BasicAclPermission"
*/
public Set getPermissions()
{
return permissions;
}
public void setParentObject(BasicAclObjectIdentity parentObject)
{
this.parentObject = parentObject;
}
public void setPermissions(Set permissions)
{
this.permissions = permissions;
}
/**
* @see java.lang.Object#equals(Object)
*/
public boolean equals(Object object)
{
BasicAclObjectIdentity rhs = (BasicAclObjectIdentity) object;
return new EqualsBuilder().append(this.getObjectIdentity(),
rhs.getObjectIdentity()).isEquals();
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
return new HashCodeBuilder(1330891477, -1591801705).append(this.getObjectIdentity())
.toHashCode();
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
return objectIdentity;
}
}
|
AclPermission
- Add a java file called BasicAclPermission.java to your model folder.
- Add the folowing content to this file:
package org.appfuse.model;
import org.acegisecurity.acl.basic.AclObjectIdentity;
import org.acegisecurity.acl.basic.BasicAclEntry;
import org.acegisecurity.acl.basic.SimpleAclEntry;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @hibernate.class table="acl_permission"
*/
public class BasicAclPermission extends BaseObject implements BasicAclEntry
{
private Long id;
private String objRecipient;
private Integer objMask;
private AclObjectIdentity basicAclObjectIdentity;
/**
* @hibernate.id column="id" generator-class="increment" unsaved-value="null"
*/
public Long getId()
{
return id;
}
/**
* @hibernate.property column="mask" not-null="true"
*/
public Integer getObjMask()
{
return objMask;
}
/**
* @hibernate.property length="100" not-null="true" column="recipient"
*/
public String getObjRecipient()
{
return objRecipient;
}
/**
* @hibernate.many-to-one name="basicAclObjectIdentity" column="acl_object_identity"
* class="org.appfuse.model.BasicAclObjectIdentity" not-null="true"
*/
public AclObjectIdentity getBasicAclObjectIdentity()
{
return basicAclObjectIdentity;
}
public void setId(Long id)
{
this.id = id;
}
public void setObjRecipient(String objRecipient)
{
this.objRecipient = objRecipient;
}
public void setObjMask(Integer objMask)
{
this.objMask = objMask;
}
public void setBasicAclObjectIdentity(
BasicAclObjectIdentity basicAclObjectIdentity)
{
this.basicAclObjectIdentity = basicAclObjectIdentity;
}
// implements BasicAclEntry
public void setAclObjectIdentity(AclObjectIdentity aclObjectIdentity)
{
this.basicAclObjectIdentity = aclObjectIdentity;
}
public AclObjectIdentity getAclObjectIdentity()
{
return this.basicAclObjectIdentity;
}
public void setAclObjectParentIdentity(
AclObjectIdentity aclObjectParentIdentity)
{
BasicAclObjectIdentity identity = (BasicAclObjectIdentity) aclObjectParentIdentity;
identity.setParentObject((BasicAclObjectIdentity) aclObjectParentIdentity);
}
public AclObjectIdentity getAclObjectParentIdentity()
{
return ((BasicAclObjectIdentity) basicAclObjectIdentity).getParentObject();
}
public void setMask(int mask)
{
this.objMask = new Integer(mask);
}
public int getMask()
{
return objMask.intValue();
}
public void setRecipient(Object recipient)
{
this.objRecipient = recipient.toString();
}
public Object getRecipient()
{
return objRecipient;
}
/**
* @see java.lang.Object#equals(Object)
*/
public boolean equals(Object object)
{
if (!(object instanceof BasicAclPermission))
{
return false;
}
BasicAclPermission rhs = (BasicAclPermission) object;
return new EqualsBuilder().append(this.objRecipient, rhs.objRecipient)
.append(this.objMask, rhs.objMask)
.append(this.id, rhs.id).isEquals();
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
return new HashCodeBuilder(-623867371, 1205873953).append(this.objRecipient)
.append(this.objMask)
.append(this.id)
.toHashCode();
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
return new ToStringBuilder(this).append("objMask", this.objMask)
.append("id", this.id)
.append("recipient", this.getRecipient())
.toString();
}
public boolean isPermitted(int mask)
{
SimpleAclEntry aclEntry = new SimpleAclEntry();
aclEntry.setMask(this.getMask());
return aclEntry.isPermitted(mask);
}
}
|
Add new models to applicationContext-hibernate.xml
- Open the applicationContext-hibernate.xml file and add the follwing to elements to mappingResources:
<value>org/appfuse/model/BasicAclObjectIdentity.hbm.xml</value>
<value>org/appfuse/model/BasicAclPermission.hbm.xml</value>
Add new content to the sample-data.xml
I assume that you have 1 person object in your DB. Enter the following part at the end of the sample-data.xml:
<table name='acl_object_identity'>
<column>id</column>
<column>object_identity</column>
<column>acl_class</column>
<column>parent_object</column>
<row>
<value>1</value>
<value>org.appfuse.model.Person</value>
<value>org.acegisecurity.acl.basic.SimpleAclEntry</value>
<null/>
</row>
<row>
<value>2</value>
<value>org.appfuse.model.Person:1</value>
<value>org.acegisecurity.acl.basic.SimpleAclEntry</value>
<null/>
</row>
</table>
<table name='acl_permission'>
<column>id</column>
<column>recipient</column>
<column>acl_object_identity</column>
<column>mask</column>
<row>
<value>1</value>
<value>admin</value>
<value>1</value>
<value>1</value>
</row>
<row>
<value>2</value>
<value>user</value>
<value>1</value>
<value>14</value>
</row>
<row>
<value>3</value>
<value>admin</value>
<value>2</value>
<value>1</value>
</row>
</table>
Setup the DB
Enhance the Person object
- Change the the Person.class. Person should now extend BaseObjectACLAware
public class Person extends BaseObjectACLAware
|
- And add the following method:
/**
* @see org.appfuse.model.BaseObjectACLAware#getUniqueKey()
*/
public Serializable getUniqueKey()
{
return getId();
}
|
Next section
Step III. Add new DAOs
|