|
51 | 51 | */ |
52 | 52 | public class JdbcAclService implements AclService { |
53 | 53 |
|
54 | | - protected static final Log log = LogFactory.getLog(JdbcAclService.class); |
55 | | - |
56 | | - private static final String DEFAULT_SELECT_ACL_CLASS_COLUMNS = "class.class as class"; |
57 | | - |
58 | | - private static final String DEFAULT_SELECT_ACL_CLASS_COLUMNS_WITH_ID_TYPE = DEFAULT_SELECT_ACL_CLASS_COLUMNS |
59 | | - + ", class.class_id_type as class_id_type"; |
60 | | - |
61 | | - private static final String DEFAULT_SELECT_ACL_WITH_PARENT_SQL = "select obj.object_id_identity as obj_id, " |
62 | | - + DEFAULT_SELECT_ACL_CLASS_COLUMNS |
63 | | - + " from acl_object_identity obj, acl_object_identity parent, acl_class class " |
64 | | - + "where obj.parent_object = parent.id and obj.object_id_class = class.id " |
65 | | - + "and parent.object_id_identity = ? and parent.object_id_class = (" |
66 | | - + "select id FROM acl_class where acl_class.class = ?)"; |
67 | | - |
68 | | - private static final String DEFAULT_SELECT_ACL_WITH_PARENT_SQL_WITH_CLASS_ID_TYPE = "select obj.object_id_identity as obj_id, " |
69 | | - + DEFAULT_SELECT_ACL_CLASS_COLUMNS_WITH_ID_TYPE |
70 | | - + " from acl_object_identity obj, acl_object_identity parent, acl_class class " |
71 | | - + "where obj.parent_object = parent.id and obj.object_id_class = class.id " |
72 | | - + "and parent.object_id_identity = ? and parent.object_id_class = (" |
73 | | - + "select id FROM acl_class where acl_class.class = ?)"; |
74 | | - |
75 | | - protected final JdbcOperations jdbcOperations; |
76 | | - |
77 | | - private final LookupStrategy lookupStrategy; |
78 | | - |
79 | | - private boolean aclClassIdSupported; |
80 | | - |
81 | | - private String findChildrenSql = DEFAULT_SELECT_ACL_WITH_PARENT_SQL; |
82 | | - |
83 | | - private AclClassIdUtils aclClassIdUtils; |
84 | | - private ObjectIdentityGenerator objectIdentityGenerator; |
85 | | - |
86 | | - public JdbcAclService(DataSource dataSource, LookupStrategy lookupStrategy) { |
87 | | - this(new JdbcTemplate(dataSource), lookupStrategy); |
88 | | - } |
89 | | - |
90 | | - public JdbcAclService(JdbcOperations jdbcOperations, LookupStrategy lookupStrategy) { |
91 | | - Assert.notNull(jdbcOperations, "JdbcOperations required"); |
92 | | - Assert.notNull(lookupStrategy, "LookupStrategy required"); |
93 | | - this.jdbcOperations = jdbcOperations; |
94 | | - this.lookupStrategy = lookupStrategy; |
95 | | - this.objectIdentityGenerator = new ObjectIdentityRetrievalStrategyImpl(); |
96 | | - this.aclClassIdUtils = new AclClassIdUtils(); |
97 | | - } |
98 | | - |
99 | | - @Override |
100 | | - public List<ObjectIdentity> findChildren(ObjectIdentity parentIdentity) { |
101 | | - Object[] args = {parentIdentity.getIdentifier().toString(), parentIdentity.getType()}; |
102 | | - List<ObjectIdentity> objects = this.jdbcOperations.query(this.findChildrenSql, args, |
103 | | - (rs, rowNum) -> mapObjectIdentityRow(rs)); |
104 | | - return (!objects.isEmpty()) ? objects : null; |
105 | | - } |
106 | | - |
107 | | - private ObjectIdentity mapObjectIdentityRow(ResultSet rs) throws SQLException { |
108 | | - String javaType = rs.getString("class"); |
109 | | - Serializable identifier = (Serializable) rs.getObject("obj_id"); |
110 | | - identifier = this.aclClassIdUtils.identifierFrom(identifier, rs); |
111 | | - return objectIdentityGenerator.createObjectIdentity(identifier, javaType); |
112 | | - } |
113 | | - |
114 | | - @Override |
115 | | - public Acl readAclById(ObjectIdentity object, List<Sid> sids) throws NotFoundException { |
116 | | - Map<ObjectIdentity, Acl> map = readAclsById(Collections.singletonList(object), sids); |
117 | | - Assert.isTrue(map.containsKey(object), |
118 | | - () -> "There should have been an Acl entry for ObjectIdentity " + object); |
119 | | - return map.get(object); |
120 | | - } |
121 | | - |
122 | | - @Override |
123 | | - public Acl readAclById(ObjectIdentity object) throws NotFoundException { |
124 | | - return readAclById(object, null); |
125 | | - } |
126 | | - |
127 | | - @Override |
128 | | - public Map<ObjectIdentity, Acl> readAclsById(List<ObjectIdentity> objects) throws NotFoundException { |
129 | | - return readAclsById(objects, null); |
130 | | - } |
131 | | - |
132 | | - @Override |
133 | | - public Map<ObjectIdentity, Acl> readAclsById(List<ObjectIdentity> objects, List<Sid> sids) |
134 | | - throws NotFoundException { |
135 | | - Map<ObjectIdentity, Acl> result = this.lookupStrategy.readAclsById(objects, sids); |
136 | | - // Check every requested object identity was found (throw NotFoundException if |
137 | | - // needed) |
138 | | - for (ObjectIdentity oid : objects) { |
139 | | - if (!result.containsKey(oid)) { |
140 | | - throw new NotFoundException("Unable to find ACL information for object identity '" + oid + "'"); |
141 | | - } |
142 | | - } |
143 | | - return result; |
144 | | - } |
145 | | - |
146 | | - /** |
147 | | - * Allows customization of the SQL query used to find child object identities. |
148 | | - * |
149 | | - * @param findChildrenSql |
150 | | - */ |
151 | | - public void setFindChildrenQuery(String findChildrenSql) { |
152 | | - this.findChildrenSql = findChildrenSql; |
153 | | - } |
154 | | - |
155 | | - public void setAclClassIdSupported(boolean aclClassIdSupported) { |
156 | | - this.aclClassIdSupported = aclClassIdSupported; |
157 | | - if (aclClassIdSupported) { |
158 | | - // Change the default children select if it hasn't been overridden |
159 | | - if (this.findChildrenSql.equals(DEFAULT_SELECT_ACL_WITH_PARENT_SQL)) { |
160 | | - this.findChildrenSql = DEFAULT_SELECT_ACL_WITH_PARENT_SQL_WITH_CLASS_ID_TYPE; |
161 | | - } else { |
162 | | - log.debug("Find children statement has already been overridden, so not overridding the default"); |
163 | | - } |
164 | | - } |
165 | | - } |
166 | | - |
167 | | - public void setConversionService(ConversionService conversionService) { |
168 | | - this.aclClassIdUtils = new AclClassIdUtils(conversionService); |
169 | | - } |
170 | | - |
171 | | - public void setObjectIdentityGenerator(ObjectIdentityGenerator objectIdentityGenerator) { |
172 | | - Assert.notNull(objectIdentityGenerator,"The provided strategy has to be not null!"); |
173 | | - this.objectIdentityGenerator = objectIdentityGenerator; |
174 | | - } |
175 | | - |
176 | | - protected boolean isAclClassIdSupported() { |
177 | | - return this.aclClassIdSupported; |
178 | | - } |
| 54 | + protected static final Log log = LogFactory.getLog(JdbcAclService.class); |
| 55 | + |
| 56 | + private static final String DEFAULT_SELECT_ACL_CLASS_COLUMNS = "class.class as class"; |
| 57 | + |
| 58 | + private static final String DEFAULT_SELECT_ACL_CLASS_COLUMNS_WITH_ID_TYPE = DEFAULT_SELECT_ACL_CLASS_COLUMNS |
| 59 | + + ", class.class_id_type as class_id_type"; |
| 60 | + |
| 61 | + private static final String DEFAULT_SELECT_ACL_WITH_PARENT_SQL = "select obj.object_id_identity as obj_id, " |
| 62 | + + DEFAULT_SELECT_ACL_CLASS_COLUMNS |
| 63 | + + " from acl_object_identity obj, acl_object_identity parent, acl_class class " |
| 64 | + + "where obj.parent_object = parent.id and obj.object_id_class = class.id " |
| 65 | + + "and parent.object_id_identity = ? and parent.object_id_class = (" |
| 66 | + + "select id FROM acl_class where acl_class.class = ?)"; |
| 67 | + |
| 68 | + private static final String DEFAULT_SELECT_ACL_WITH_PARENT_SQL_WITH_CLASS_ID_TYPE = "select obj.object_id_identity as obj_id, " |
| 69 | + + DEFAULT_SELECT_ACL_CLASS_COLUMNS_WITH_ID_TYPE |
| 70 | + + " from acl_object_identity obj, acl_object_identity parent, acl_class class " |
| 71 | + + "where obj.parent_object = parent.id and obj.object_id_class = class.id " |
| 72 | + + "and parent.object_id_identity = ? and parent.object_id_class = (" |
| 73 | + + "select id FROM acl_class where acl_class.class = ?)"; |
| 74 | + |
| 75 | + protected final JdbcOperations jdbcOperations; |
| 76 | + |
| 77 | + private final LookupStrategy lookupStrategy; |
| 78 | + |
| 79 | + private boolean aclClassIdSupported; |
| 80 | + |
| 81 | + private String findChildrenSql = DEFAULT_SELECT_ACL_WITH_PARENT_SQL; |
| 82 | + |
| 83 | + private AclClassIdUtils aclClassIdUtils; |
| 84 | + |
| 85 | + private ObjectIdentityGenerator objectIdentityGenerator; |
| 86 | + |
| 87 | + public JdbcAclService(DataSource dataSource, LookupStrategy lookupStrategy) { |
| 88 | + this(new JdbcTemplate(dataSource), lookupStrategy); |
| 89 | + } |
| 90 | + |
| 91 | + public JdbcAclService(JdbcOperations jdbcOperations, LookupStrategy lookupStrategy) { |
| 92 | + Assert.notNull(jdbcOperations, "JdbcOperations required"); |
| 93 | + Assert.notNull(lookupStrategy, "LookupStrategy required"); |
| 94 | + this.jdbcOperations = jdbcOperations; |
| 95 | + this.lookupStrategy = lookupStrategy; |
| 96 | + this.aclClassIdUtils = new AclClassIdUtils(); |
| 97 | + this.objectIdentityGenerator = new ObjectIdentityRetrievalStrategyImpl(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public List<ObjectIdentity> findChildren(ObjectIdentity parentIdentity) { |
| 102 | + Object[] args = { parentIdentity.getIdentifier().toString(), parentIdentity.getType() }; |
| 103 | + List<ObjectIdentity> objects = this.jdbcOperations.query(this.findChildrenSql, args, |
| 104 | + (rs, rowNum) -> mapObjectIdentityRow(rs)); |
| 105 | + return (!objects.isEmpty()) ? objects : null; |
| 106 | + } |
| 107 | + |
| 108 | + private ObjectIdentity mapObjectIdentityRow(ResultSet rs) throws SQLException { |
| 109 | + String javaType = rs.getString("class"); |
| 110 | + Serializable identifier = (Serializable) rs.getObject("obj_id"); |
| 111 | + identifier = this.aclClassIdUtils.identifierFrom(identifier, rs); |
| 112 | + return this.objectIdentityGenerator.createObjectIdentity(identifier, javaType); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public Acl readAclById(ObjectIdentity object, List<Sid> sids) throws NotFoundException { |
| 117 | + Map<ObjectIdentity, Acl> map = readAclsById(Collections.singletonList(object), sids); |
| 118 | + Assert.isTrue(map.containsKey(object), |
| 119 | + () -> "There should have been an Acl entry for ObjectIdentity " + object); |
| 120 | + return map.get(object); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Acl readAclById(ObjectIdentity object) throws NotFoundException { |
| 125 | + return readAclById(object, null); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Map<ObjectIdentity, Acl> readAclsById(List<ObjectIdentity> objects) throws NotFoundException { |
| 130 | + return readAclsById(objects, null); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Map<ObjectIdentity, Acl> readAclsById(List<ObjectIdentity> objects, List<Sid> sids) |
| 135 | + throws NotFoundException { |
| 136 | + Map<ObjectIdentity, Acl> result = this.lookupStrategy.readAclsById(objects, sids); |
| 137 | + // Check every requested object identity was found (throw NotFoundException if |
| 138 | + // needed) |
| 139 | + for (ObjectIdentity oid : objects) { |
| 140 | + if (!result.containsKey(oid)) { |
| 141 | + throw new NotFoundException("Unable to find ACL information for object identity '" + oid + "'"); |
| 142 | + } |
| 143 | + } |
| 144 | + return result; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Allows customization of the SQL query used to find child object identities. |
| 149 | + * @param findChildrenSql |
| 150 | + */ |
| 151 | + public void setFindChildrenQuery(String findChildrenSql) { |
| 152 | + this.findChildrenSql = findChildrenSql; |
| 153 | + } |
| 154 | + |
| 155 | + public void setAclClassIdSupported(boolean aclClassIdSupported) { |
| 156 | + this.aclClassIdSupported = aclClassIdSupported; |
| 157 | + if (aclClassIdSupported) { |
| 158 | + // Change the default children select if it hasn't been overridden |
| 159 | + if (this.findChildrenSql.equals(DEFAULT_SELECT_ACL_WITH_PARENT_SQL)) { |
| 160 | + this.findChildrenSql = DEFAULT_SELECT_ACL_WITH_PARENT_SQL_WITH_CLASS_ID_TYPE; |
| 161 | + } |
| 162 | + else { |
| 163 | + log.debug("Find children statement has already been overridden, so not overridding the default"); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public void setConversionService(ConversionService conversionService) { |
| 169 | + this.aclClassIdUtils = new AclClassIdUtils(conversionService); |
| 170 | + } |
| 171 | + |
| 172 | + public void setObjectIdentityGenerator(ObjectIdentityGenerator objectIdentityGenerator) { |
| 173 | + Assert.notNull(objectIdentityGenerator, "objectIdentityGenerator cannot be null"); |
| 174 | + this.objectIdentityGenerator = objectIdentityGenerator; |
| 175 | + } |
| 176 | + |
| 177 | + protected boolean isAclClassIdSupported() { |
| 178 | + return this.aclClassIdSupported; |
| 179 | + } |
179 | 180 |
|
180 | 181 | } |
0 commit comments