1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.springframework.security.acls.jdbc;
16
17 import org.springframework.security.acls.Acl;
18 import org.springframework.security.acls.AclService;
19 import org.springframework.security.acls.NotFoundException;
20 import org.springframework.security.acls.objectidentity.ObjectIdentity;
21 import org.springframework.security.acls.objectidentity.ObjectIdentityImpl;
22 import org.springframework.security.acls.sid.Sid;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.springframework.jdbc.core.JdbcTemplate;
28 import org.springframework.jdbc.core.RowMapper;
29
30 import org.springframework.util.Assert;
31 import org.springframework.util.StringUtils;
32
33 import java.sql.ResultSet;
34 import java.sql.SQLException;
35
36 import java.util.List;
37 import java.util.Map;
38
39 import javax.sql.DataSource;
40
41
42
43
44
45
46
47
48
49
50
51
52 public class JdbcAclService implements AclService {
53
54
55 protected static final Log log = LogFactory.getLog(JdbcAclService.class);
56 private static final String selectAclObjectWithParent = "select obj.object_id_identity as obj_id, class.class as class "
57 + "from acl_object_identity obj, acl_object_identity parent, acl_class class "
58 + "where obj.parent_object = parent.id and obj.object_id_class = class.id "
59 + "and parent.object_id_identity = ? and parent.object_id_class = ("
60 + "select id FROM acl_class where acl_class.class = ?)";
61
62
63
64 protected JdbcTemplate jdbcTemplate;
65 private LookupStrategy lookupStrategy;
66
67
68
69 public JdbcAclService(DataSource dataSource, LookupStrategy lookupStrategy) {
70 Assert.notNull(dataSource, "DataSource required");
71 Assert.notNull(lookupStrategy, "LookupStrategy required");
72 this.jdbcTemplate = new JdbcTemplate(dataSource);
73 this.lookupStrategy = lookupStrategy;
74 }
75
76
77
78 public ObjectIdentity[] findChildren(ObjectIdentity parentIdentity) {
79 Object[] args = {parentIdentity.getIdentifier(), parentIdentity.getJavaType().getName()};
80 List objects = jdbcTemplate.query(selectAclObjectWithParent, args,
81 new RowMapper() {
82 public Object mapRow(ResultSet rs, int rowNum)
83 throws SQLException {
84 String javaType = rs.getString("class");
85 Long identifier = new Long(rs.getLong("obj_id"));
86
87 return new ObjectIdentityImpl(javaType, identifier);
88 }
89 });
90
91 if (objects.size() == 0) {
92 return null;
93 }
94
95 return (ObjectIdentityImpl[]) objects.toArray(new ObjectIdentityImpl[objects.size()]);
96 }
97
98 public Acl readAclById(ObjectIdentity object, Sid[] sids) throws NotFoundException {
99 Map map = readAclsById(new ObjectIdentity[] {object}, sids);
100 Assert.isTrue(map.containsKey(object), "There should have been an Acl entry for ObjectIdentity " + object);
101
102 return (Acl) map.get(object);
103 }
104
105 public Acl readAclById(ObjectIdentity object) throws NotFoundException {
106 return readAclById(object, null);
107 }
108
109 public Map readAclsById(ObjectIdentity[] objects) throws NotFoundException {
110 return readAclsById(objects, null);
111 }
112
113 public Map readAclsById(ObjectIdentity[] objects, Sid[] sids) throws NotFoundException {
114 Map result = lookupStrategy.readAclsById(objects, sids);
115
116
117 for (int i = 0; i < objects.length; i++) {
118 if (!result.containsKey(objects[i])) {
119 throw new NotFoundException("Unable to find ACL information for object identity '"
120 + objects[i].toString() + "'");
121 }
122 }
123
124 return result;
125 }
126 }