|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.acl.basic.jdbc; |
|
17 |
| |
|
18 |
| import org.acegisecurity.acl.basic.AclObjectIdentity; |
|
19 |
| import org.acegisecurity.acl.basic.BasicAclDao; |
|
20 |
| import org.acegisecurity.acl.basic.BasicAclEntry; |
|
21 |
| import org.acegisecurity.acl.basic.NamedEntityObjectIdentity; |
|
22 |
| |
|
23 |
| import org.apache.commons.logging.Log; |
|
24 |
| import org.apache.commons.logging.LogFactory; |
|
25 |
| |
|
26 |
| import org.springframework.context.ApplicationContextException; |
|
27 |
| |
|
28 |
| import org.springframework.jdbc.core.SqlParameter; |
|
29 |
| import org.springframework.jdbc.core.support.JdbcDaoSupport; |
|
30 |
| import org.springframework.jdbc.object.MappingSqlQuery; |
|
31 |
| |
|
32 |
| import org.springframework.util.Assert; |
|
33 |
| |
|
34 |
| import java.sql.ResultSet; |
|
35 |
| import java.sql.SQLException; |
|
36 |
| import java.sql.Types; |
|
37 |
| |
|
38 |
| import java.util.List; |
|
39 |
| import java.util.Vector; |
|
40 |
| |
|
41 |
| import javax.sql.DataSource; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| public class JdbcDaoImpl extends JdbcDaoSupport implements BasicAclDao { |
|
58 |
| |
|
59 |
| |
|
60 |
| public static final String RECIPIENT_USED_FOR_INHERITENCE_MARKER = "___INHERITENCE_MARKER_ONLY___"; |
|
61 |
| public static final String DEF_ACLS_BY_OBJECT_IDENTITY_QUERY = "SELECT RECIPIENT, MASK FROM acl_permission WHERE acl_object_identity = ?"; |
|
62 |
| public static final String DEF_OBJECT_PROPERTIES_QUERY = "SELECT CHILD.ID, CHILD.OBJECT_IDENTITY, CHILD.ACL_CLASS, PARENT.OBJECT_IDENTITY as PARENT_OBJECT_IDENTITY FROM acl_object_identity as CHILD LEFT OUTER JOIN acl_object_identity as PARENT ON CHILD.parent_object=PARENT.id WHERE CHILD.object_identity = ?"; |
|
63 |
| private static final Log logger = LogFactory.getLog(JdbcDaoImpl.class); |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| protected MappingSqlQuery aclsByObjectIdentity; |
|
68 |
| protected MappingSqlQuery objectProperties; |
|
69 |
| private String aclsByObjectIdentityQuery; |
|
70 |
| private String objectPropertiesQuery; |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
26
| public JdbcDaoImpl() {
|
|
75 |
26
| aclsByObjectIdentityQuery = DEF_ACLS_BY_OBJECT_IDENTITY_QUERY;
|
|
76 |
26
| objectPropertiesQuery = DEF_OBJECT_PROPERTIES_QUERY;
|
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
62
| protected String convertAclObjectIdentityToString(
|
|
90 |
| AclObjectIdentity aclObjectIdentity) { |
|
91 |
| |
|
92 |
62
| Assert.isInstanceOf(NamedEntityObjectIdentity.class, aclObjectIdentity,
|
|
93 |
| "Only aclObjectIdentity of type NamedEntityObjectIdentity supported (was passed: " |
|
94 |
| + aclObjectIdentity + ")"); |
|
95 |
| |
|
96 |
60
| NamedEntityObjectIdentity neoi = (NamedEntityObjectIdentity) aclObjectIdentity;
|
|
97 |
| |
|
98 |
| |
|
99 |
60
| return neoi.getClassname() + ":" + neoi.getId();
|
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
25
| private BasicAclEntry createBasicAclEntry(
|
|
127 |
| AclDetailsHolder propertiesInformation, AclDetailsHolder aclInformation) { |
|
128 |
25
| BasicAclEntry entry;
|
|
129 |
| |
|
130 |
25
| try {
|
|
131 |
25
| entry = (BasicAclEntry) propertiesInformation.getAclClass()
|
|
132 |
| .newInstance(); |
|
133 |
| } catch (InstantiationException ie) { |
|
134 |
0
| throw new IllegalArgumentException(ie.getMessage());
|
|
135 |
| } catch (IllegalAccessException iae) { |
|
136 |
0
| throw new IllegalArgumentException(iae.getMessage());
|
|
137 |
| } |
|
138 |
| |
|
139 |
25
| entry.setAclObjectIdentity(propertiesInformation.getAclObjectIdentity());
|
|
140 |
25
| entry.setAclObjectParentIdentity(propertiesInformation
|
|
141 |
| .getAclObjectParentIdentity()); |
|
142 |
| |
|
143 |
25
| if (aclInformation == null) {
|
|
144 |
| |
|
145 |
2
| entry.setMask(0);
|
|
146 |
2
| entry.setRecipient(RECIPIENT_USED_FOR_INHERITENCE_MARKER);
|
|
147 |
| } else { |
|
148 |
| |
|
149 |
23
| entry.setMask(aclInformation.getMask());
|
|
150 |
23
| entry.setRecipient(aclInformation.getRecipient());
|
|
151 |
| } |
|
152 |
| |
|
153 |
25
| return entry;
|
|
154 |
| } |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
25
| public BasicAclEntry[] getAcls(AclObjectIdentity aclObjectIdentity) {
|
|
185 |
25
| String aclObjectIdentityString;
|
|
186 |
| |
|
187 |
25
| try {
|
|
188 |
25
| aclObjectIdentityString = convertAclObjectIdentityToString(aclObjectIdentity);
|
|
189 |
| } catch (IllegalArgumentException unsupported) { |
|
190 |
1
| return null;
|
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
24
| List objects = objectProperties.execute(aclObjectIdentityString);
|
|
195 |
| |
|
196 |
23
| if (objects.size() == 0) {
|
|
197 |
| |
|
198 |
4
| return null;
|
|
199 |
| } |
|
200 |
| |
|
201 |
| |
|
202 |
19
| AclDetailsHolder propertiesInformation = (AclDetailsHolder) objects
|
|
203 |
| .get(0); |
|
204 |
| |
|
205 |
| |
|
206 |
19
| List acls = aclsByObjectIdentity.execute(propertiesInformation
|
|
207 |
| .getForeignKeyId()); |
|
208 |
| |
|
209 |
19
| if (acls.size() == 0) {
|
|
210 |
| |
|
211 |
2
| return new BasicAclEntry[] {createBasicAclEntry(propertiesInformation,
|
|
212 |
| null)}; |
|
213 |
| } else { |
|
214 |
| |
|
215 |
17
| AclDetailsHolder[] aclHolders = (AclDetailsHolder[]) acls
|
|
216 |
| .toArray(new AclDetailsHolder[] {}); |
|
217 |
17
| List toReturnAcls = new Vector();
|
|
218 |
| |
|
219 |
17
| for (int i = 0; i < aclHolders.length; i++) {
|
|
220 |
23
| toReturnAcls.add(createBasicAclEntry(
|
|
221 |
| propertiesInformation, aclHolders[i])); |
|
222 |
| } |
|
223 |
| |
|
224 |
17
| return (BasicAclEntry[]) toReturnAcls.toArray(new BasicAclEntry[] {});
|
|
225 |
| } |
|
226 |
| } |
|
227 |
| |
|
228 |
3
| public MappingSqlQuery getAclsByObjectIdentity() {
|
|
229 |
3
| return aclsByObjectIdentity;
|
|
230 |
| } |
|
231 |
| |
|
232 |
3
| public String getAclsByObjectIdentityQuery() {
|
|
233 |
3
| return aclsByObjectIdentityQuery;
|
|
234 |
| } |
|
235 |
| |
|
236 |
1
| public String getObjectPropertiesQuery() {
|
|
237 |
1
| return objectPropertiesQuery;
|
|
238 |
| } |
|
239 |
| |
|
240 |
25
| protected void initDao() throws ApplicationContextException {
|
|
241 |
25
| initMappingSqlQueries();
|
|
242 |
| } |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
25
| protected void initMappingSqlQueries() {
|
|
249 |
25
| setAclsByObjectIdentity(new AclsByObjectIdentityMapping(
|
|
250 |
| getDataSource())); |
|
251 |
25
| setObjectProperties(new ObjectPropertiesMapping(
|
|
252 |
| getDataSource())); |
|
253 |
| } |
|
254 |
| |
|
255 |
27
| public void setAclsByObjectIdentity(
|
|
256 |
| MappingSqlQuery aclsByObjectIdentityQuery) { |
|
257 |
27
| this.aclsByObjectIdentity = aclsByObjectIdentityQuery;
|
|
258 |
| } |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
2
| public void setAclsByObjectIdentityQuery(String queryString) {
|
|
271 |
2
| aclsByObjectIdentityQuery = queryString;
|
|
272 |
| } |
|
273 |
| |
|
274 |
25
| public void setObjectProperties(
|
|
275 |
| MappingSqlQuery objectPropertiesQuery) { |
|
276 |
25
| this.objectProperties = objectPropertiesQuery;
|
|
277 |
| } |
|
278 |
| |
|
279 |
1
| public void setObjectPropertiesQuery(String queryString) {
|
|
280 |
1
| objectPropertiesQuery = queryString;
|
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| protected final class AclDetailsHolder { |
|
303 |
| private AclObjectIdentity aclObjectIdentity; |
|
304 |
| private AclObjectIdentity aclObjectParentIdentity; |
|
305 |
| private Class aclClass; |
|
306 |
| private Object recipient; |
|
307 |
| private int mask; |
|
308 |
| private long foreignKeyId; |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
25
| public AclDetailsHolder(Object recipient, int mask) {
|
|
318 |
25
| this.recipient = recipient;
|
|
319 |
25
| this.mask = mask;
|
|
320 |
| } |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
47
| public AclDetailsHolder(long foreignKeyId,
|
|
339 |
| AclObjectIdentity aclObjectIdentity, |
|
340 |
| AclObjectIdentity aclObjectParentIdentity, |
|
341 |
| Class aclClass) { |
|
342 |
47
| this.foreignKeyId = foreignKeyId;
|
|
343 |
47
| this.aclObjectIdentity = aclObjectIdentity;
|
|
344 |
47
| this.aclObjectParentIdentity = aclObjectParentIdentity;
|
|
345 |
47
| this.aclClass = aclClass;
|
|
346 |
| } |
|
347 |
| |
|
348 |
25
| public Class getAclClass() {
|
|
349 |
25
| return aclClass;
|
|
350 |
| } |
|
351 |
| |
|
352 |
25
| public AclObjectIdentity getAclObjectIdentity() {
|
|
353 |
25
| return aclObjectIdentity;
|
|
354 |
| } |
|
355 |
| |
|
356 |
25
| public AclObjectIdentity getAclObjectParentIdentity() {
|
|
357 |
25
| return aclObjectParentIdentity;
|
|
358 |
| } |
|
359 |
| |
|
360 |
53
| public long getForeignKeyId() {
|
|
361 |
53
| return foreignKeyId;
|
|
362 |
| } |
|
363 |
| |
|
364 |
23
| public int getMask() {
|
|
365 |
23
| return mask;
|
|
366 |
| } |
|
367 |
| |
|
368 |
25
| public Object getRecipient() {
|
|
369 |
25
| return recipient;
|
|
370 |
| } |
|
371 |
| } |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| protected class AclsByObjectIdentityMapping |
|
392 |
| extends MappingSqlQuery { |
|
393 |
25
| protected AclsByObjectIdentityMapping(DataSource ds) {
|
|
394 |
25
| super(ds, aclsByObjectIdentityQuery);
|
|
395 |
25
| declareParameter(new SqlParameter(Types.BIGINT));
|
|
396 |
25
| compile();
|
|
397 |
| } |
|
398 |
| |
|
399 |
25
| protected Object mapRow(ResultSet rs, int rownum)
|
|
400 |
| throws SQLException { |
|
401 |
25
| String recipient = rs.getString(1);
|
|
402 |
25
| int mask = rs.getInt(2);
|
|
403 |
25
| Assert.hasText(recipient, "recipient required");
|
|
404 |
| |
|
405 |
25
| return new AclDetailsHolder(recipient, mask);
|
|
406 |
| } |
|
407 |
| } |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| protected class ObjectPropertiesMapping extends MappingSqlQuery { |
|
429 |
25
| protected ObjectPropertiesMapping(DataSource ds) {
|
|
430 |
25
| super(ds, objectPropertiesQuery);
|
|
431 |
25
| declareParameter(new SqlParameter(Types.VARCHAR));
|
|
432 |
25
| compile();
|
|
433 |
| } |
|
434 |
| |
|
435 |
94
| private AclObjectIdentity buildIdentity(String identity) {
|
|
436 |
94
| if (identity == null) {
|
|
437 |
| |
|
438 |
17
| return null;
|
|
439 |
| } |
|
440 |
| |
|
441 |
77
| int delim = identity.lastIndexOf(":");
|
|
442 |
77
| String classname = identity.substring(0, delim);
|
|
443 |
77
| String id = identity.substring(delim + 1);
|
|
444 |
| |
|
445 |
77
| return new NamedEntityObjectIdentity(classname, id);
|
|
446 |
| } |
|
447 |
| |
|
448 |
48
| protected Object mapRow(ResultSet rs, int rownum)
|
|
449 |
| throws SQLException { |
|
450 |
48
| long id = rs.getLong(1);
|
|
451 |
48
| String objectIdentity = rs.getString(2);
|
|
452 |
48
| String aclClass = rs.getString(3);
|
|
453 |
48
| String parentObjectIdentity = rs.getString(4);
|
|
454 |
48
| Assert.hasText(objectIdentity,
|
|
455 |
| "required DEF_OBJECT_PROPERTIES_QUERY value (objectIdentity) returned null or empty"); |
|
456 |
48
| Assert.hasText(aclClass,
|
|
457 |
| "required DEF_OBJECT_PROPERTIES_QUERY value (aclClass) returned null or empty"); |
|
458 |
| |
|
459 |
48
| Class aclClazz;
|
|
460 |
| |
|
461 |
48
| try {
|
|
462 |
48
| aclClazz = this.getClass().getClassLoader()
|
|
463 |
| .loadClass(aclClass); |
|
464 |
| } catch (ClassNotFoundException cnf) { |
|
465 |
1
| throw new IllegalArgumentException(cnf.getMessage());
|
|
466 |
| } |
|
467 |
| |
|
468 |
47
| return new AclDetailsHolder(id,
|
|
469 |
| buildIdentity(objectIdentity), |
|
470 |
| buildIdentity(parentObjectIdentity), aclClazz); |
|
471 |
| } |
|
472 |
| } |
|
473 |
| } |