Parcourir la source

Use new BasicAclExtendedDao for DAO CRUD operations instead of ContactDao.

Ben Alex il y a 21 ans
Parent
commit
f286c34312

+ 0 - 32
samples/contacts/src/main/java/sample/contact/ContactDao.java

@@ -31,45 +31,13 @@ public interface ContactDao {
 
     public void create(Contact contact);
 
-    /**
-     * Creates an acl_object_identity for the specified Contact.
-     *
-     * @param contact to create an entry for
-     *
-     * @return the acl_object_identity identifier
-     */
-    public Integer createAclObjectIdentity(Contact contact);
-
-    /**
-     * Given an acl_object_identitiy identifier, grant the specified recipient
-     * read access to the object identified.
-     *
-     * @param aclObjectIdentity to assign the read permission against
-     * @param recipient receiving the permission
-     * @param permission to assign
-     */
-    public void createPermission(Integer aclObjectIdentity, String recipient,
-        int permission);
-
     public void delete(Integer contactId);
 
-    public void deletePermission(Integer aclObjectIdentity, String recipient);
-
     public List findAll();
 
     public List findAllPrincipals();
 
     public List findAllRoles();
 
-    /**
-     * Obtains the acl_object_identity for the specified Contact.
-     *
-     * @param contact to locate an acl_object_identity for
-     *
-     * @return the acl_object_identity identifier or <code>null</code> if not
-     *         found
-     */
-    public Integer lookupAclObjectIdentity(Contact contact);
-
     public void update(Contact contact);
 }

+ 0 - 36
samples/contacts/src/main/java/sample/contact/ContactDaoSpring.java

@@ -15,8 +15,6 @@
 
 package sample.contact;
 
-import net.sf.acegisecurity.acl.basic.SimpleAclEntry;
-
 import org.springframework.jdbc.core.SqlParameter;
 import org.springframework.jdbc.core.support.JdbcDaoSupport;
 import org.springframework.jdbc.object.MappingSqlQuery;
@@ -40,15 +38,11 @@ import javax.sql.DataSource;
 public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
     //~ Instance fields ========================================================
 
-    private AclObjectIdentityByObjectIdentityQuery aclObjectIdentityByObjectIdentityQuery;
-    private AclObjectIdentityInsert aclObjectIdentityInsert;
     private ContactDelete contactDelete;
     private ContactInsert contactInsert;
     private ContactUpdate contactUpdate;
     private ContactsAllQuery contactsAllQuery;
     private ContactsByIdQuery contactsByIdQuery;
-    private PermissionDelete permissionDelete;
-    private PermissionInsert permissionInsert;
     private PrincipalsAllQuery principalsAllQuery;
     private RolesAllQuery rolesAllQuery;
 
@@ -68,25 +62,10 @@ public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
         contactInsert.insert(contact);
     }
 
-    public Integer createAclObjectIdentity(Contact contact) {
-        return new Integer(aclObjectIdentityInsert.insert(makeObjectIdentity(
-                    contact), null, SimpleAclEntry.class.getName()));
-    }
-
-    public void createPermission(Integer aclObjectIdentity, String recipient,
-        int permission) {
-        permissionInsert.insert(aclObjectIdentity, recipient,
-            new Integer(permission));
-    }
-
     public void delete(Integer contactId) {
         contactDelete.delete(contactId);
     }
 
-    public void deletePermission(Integer aclObjectIdentity, String recipient) {
-        permissionDelete.delete(aclObjectIdentity, recipient);
-    }
-
     public List findAll() {
         return contactsAllQuery.execute();
     }
@@ -99,17 +78,6 @@ public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
         return rolesAllQuery.execute();
     }
 
-    public Integer lookupAclObjectIdentity(Contact contact) {
-        List list = aclObjectIdentityByObjectIdentityQuery.execute(makeObjectIdentity(
-                    contact));
-
-        if (list.size() == 0) {
-            return null;
-        } else {
-            return (Integer) list.get(0);
-        }
-    }
-
     public void update(Contact contact) {
         contactUpdate.update(contact);
     }
@@ -118,14 +86,10 @@ public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
         contactInsert = new ContactInsert(getDataSource());
         contactUpdate = new ContactUpdate(getDataSource());
         contactDelete = new ContactDelete(getDataSource());
-        aclObjectIdentityInsert = new AclObjectIdentityInsert(getDataSource());
-        permissionInsert = new PermissionInsert(getDataSource());
-        permissionDelete = new PermissionDelete(getDataSource());
         contactsAllQuery = new ContactsAllQuery(getDataSource());
         principalsAllQuery = new PrincipalsAllQuery(getDataSource());
         rolesAllQuery = new RolesAllQuery(getDataSource());
         contactsByIdQuery = new ContactsByIdQuery(getDataSource());
-        aclObjectIdentityByObjectIdentityQuery = new AclObjectIdentityByObjectIdentityQuery(getDataSource());
     }
 
     private String makeObjectIdentity(Contact contact) {

+ 32 - 8
samples/contacts/src/main/java/sample/contact/ContactManagerBackend.java

@@ -15,6 +15,9 @@
 
 package sample.contact;
 
+import net.sf.acegisecurity.acl.basic.AclObjectIdentity;
+import net.sf.acegisecurity.acl.basic.BasicAclExtendedDao;
+import net.sf.acegisecurity.acl.basic.NamedEntityObjectIdentity;
 import net.sf.acegisecurity.acl.basic.SimpleAclEntry;
 import net.sf.acegisecurity.context.ContextHolder;
 import net.sf.acegisecurity.context.SecureContext;
@@ -34,6 +37,7 @@ import java.util.Random;
 public class ContactManagerBackend implements ContactManager, InitializingBean {
     //~ Instance fields ========================================================
 
+    private BasicAclExtendedDao basicAclExtendedDao;
     private ContactDao contactDao;
     private int counter = 100;
 
@@ -50,6 +54,14 @@ public class ContactManagerBackend implements ContactManager, InitializingBean {
         return list;
     }
 
+    public void setBasicAclExtendedDao(BasicAclExtendedDao basicAclExtendedDao) {
+        this.basicAclExtendedDao = basicAclExtendedDao;
+    }
+
+    public BasicAclExtendedDao getBasicAclExtendedDao() {
+        return basicAclExtendedDao;
+    }
+
     public Contact getById(Integer id) {
         return contactDao.getById(id);
     }
@@ -77,15 +89,21 @@ public class ContactManagerBackend implements ContactManager, InitializingBean {
 
     public void addPermission(Contact contact, String recipient,
         Integer permission) {
-        Integer aclObjectIdentity = contactDao.lookupAclObjectIdentity(contact);
-        contactDao.createPermission(aclObjectIdentity, recipient,
-            permission.intValue());
+        SimpleAclEntry simpleAclEntry = new SimpleAclEntry();
+        simpleAclEntry.setAclObjectIdentity(makeObjectIdentity(contact));
+        simpleAclEntry.setMask(permission.intValue());
+        simpleAclEntry.setRecipient(recipient);
+        basicAclExtendedDao.create(simpleAclEntry);
     }
 
     public void afterPropertiesSet() throws Exception {
         if (contactDao == null) {
             throw new IllegalArgumentException("contactDao required");
         }
+
+        if (basicAclExtendedDao == null) {
+            throw new IllegalArgumentException("basicAclExtendedDao required");
+        }
     }
 
     public void create(Contact contact) {
@@ -94,18 +112,19 @@ public class ContactManagerBackend implements ContactManager, InitializingBean {
         contactDao.create(contact);
 
         // Grant the current principal access to the contact 
-        Integer aclObjectIdentity = contactDao.createAclObjectIdentity(contact);
-        contactDao.createPermission(aclObjectIdentity, getUsername(),
-            SimpleAclEntry.ADMINISTRATION);
+        addPermission(contact, getUsername(),
+            new Integer(SimpleAclEntry.ADMINISTRATION));
     }
 
     public void delete(Contact contact) {
         contactDao.delete(contact.getId());
+
+        // Delete the ACL information as well
+        basicAclExtendedDao.delete(makeObjectIdentity(contact));
     }
 
     public void deletePermission(Contact contact, String recipient) {
-        Integer aclObjectIdentity = contactDao.lookupAclObjectIdentity(contact);
-        contactDao.deletePermission(aclObjectIdentity, recipient);
+        basicAclExtendedDao.delete(makeObjectIdentity(contact), recipient);
     }
 
     public void update(Contact contact) {
@@ -116,4 +135,9 @@ public class ContactManagerBackend implements ContactManager, InitializingBean {
         return ((SecureContext) ContextHolder.getContext()).getAuthentication()
                 .getPrincipal().toString();
     }
+
+    private AclObjectIdentity makeObjectIdentity(Contact contact) {
+        return new NamedEntityObjectIdentity(contact.getClass().getName(),
+            contact.getId().toString());
+    }
 }

+ 2 - 2
samples/contacts/src/main/webapp/common/WEB-INF/applicationContext-common-authorization.xml

@@ -78,10 +78,10 @@
    </bean>
 
    <bean id="basicAclProvider" class="net.sf.acegisecurity.acl.basic.BasicAclProvider">
-      <property name="basicAclDao"><ref local="basicAclDao"/></property>
+      <property name="basicAclDao"><ref local="basicAclExtendedDao"/></property>
    </bean>
 
-   <bean id="basicAclDao" class="net.sf.acegisecurity.acl.basic.jdbc.JdbcDaoImpl">
+   <bean id="basicAclExtendedDao" class="net.sf.acegisecurity.acl.basic.jdbc.JdbcExtendedDaoImpl">
       <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>
 

+ 1 - 0
samples/contacts/src/main/webapp/common/WEB-INF/applicationContext-common-business.xml

@@ -66,6 +66,7 @@
 
    <bean id="contactManagerTarget" class="sample.contact.ContactManagerBackend">
 	   <property name="contactDao"><ref local="contactDao"/></property>
+	   <property name="basicAclExtendedDao"><ref bean="basicAclExtendedDao"/></property>
    </bean>
 
 </beans>