01: package sample.dms.secured;
02:
03: import java.sql.ResultSet;
04: import java.sql.SQLException;
05:
06: import org.acegisecurity.acls.MutableAcl;
07: import org.acegisecurity.acls.MutableAclService;
08: import org.acegisecurity.acls.domain.BasePermission;
09: import org.acegisecurity.acls.objectidentity.ObjectIdentity;
10: import org.acegisecurity.acls.objectidentity.ObjectIdentityImpl;
11: import org.acegisecurity.acls.sid.PrincipalSid;
12: import org.acegisecurity.context.SecurityContextHolder;
13: import org.springframework.jdbc.core.RowMapper;
14: import org.springframework.util.Assert;
15:
16: import sample.dms.AbstractElement;
17: import sample.dms.DocumentDaoImpl;
18:
19: /**
20: * Adds extra {@link SecureDocumentDao} methods.
21: *
22: * @author Ben Alex
23: * @version $Id: SecureDocumentDaoImpl.java 1784 2007-02-24 21:00:24Z luke_t $
24: *
25: */
26: public class SecureDocumentDaoImpl extends DocumentDaoImpl implements
27: SecureDocumentDao {
28:
29: private static final String SELECT_FROM_USERS = "SELECT USERNAME FROM USERS ORDER BY USERNAME";
30: private MutableAclService mutableAclService;
31:
32: public SecureDocumentDaoImpl(MutableAclService mutableAclService) {
33: Assert.notNull(mutableAclService, "MutableAclService required");
34: this .mutableAclService = mutableAclService;
35: }
36:
37: public String[] getUsers() {
38: return (String[]) getJdbcTemplate().query(SELECT_FROM_USERS,
39: new RowMapper() {
40: public Object mapRow(ResultSet rs, int rowNumber)
41: throws SQLException {
42: return rs.getString("USERNAME");
43: }
44: }).toArray(new String[] {});
45: }
46:
47: public void create(AbstractElement element) {
48: super .create(element);
49:
50: // Create an ACL identity for this element
51: ObjectIdentity identity = new ObjectIdentityImpl(element);
52: MutableAcl acl = mutableAclService.createAcl(identity);
53:
54: // If the AbstractElement has a parent, go and retrieve its identity (it should already exist)
55: if (element.getParent() != null) {
56: ObjectIdentity parentIdentity = new ObjectIdentityImpl(
57: element.getParent());
58: MutableAcl aclParent = (MutableAcl) mutableAclService
59: .readAclById(parentIdentity);
60: acl.setParent(aclParent);
61: }
62: acl.insertAce(null, BasePermission.ADMINISTRATION,
63: new PrincipalSid(SecurityContextHolder.getContext()
64: .getAuthentication()), true);
65:
66: mutableAclService.updateAcl(acl);
67: }
68: }
|