1 package org.apache.fulcrum.security.torque;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.sql.Connection;
22 import java.util.List;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.fulcrum.security.entity.Permission;
27 import org.apache.fulcrum.security.spi.AbstractPermissionManager;
28 import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
29 import org.apache.fulcrum.security.util.DataBackendException;
30 import org.apache.fulcrum.security.util.EntityExistsException;
31 import org.apache.fulcrum.security.util.PermissionSet;
32 import org.apache.fulcrum.security.util.UnknownEntityException;
33 import org.apache.torque.NoRowsException;
34 import org.apache.torque.TooManyRowsException;
35 import org.apache.torque.TorqueException;
36 import org.apache.torque.util.Transaction;
37
38
39
40
41
42
43
44 public abstract class TorqueAbstractPermissionManager extends AbstractPermissionManager {
45
46
47 private static final long serialVersionUID = 194503017446833485L;
48
49
50
51
52
53
54 @Override
55 public void configure(Configuration conf) throws ConfigurationException {
56 super.configure(conf);
57
58 }
59
60
61
62
63
64
65
66
67 protected abstract <T extends Permission> List<T> doSelectAllPermissions(Connection con) throws TorqueException;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 protected abstract <T extends Permission> T doSelectByName(String name, Connection con)
83 throws NoRowsException, TooManyRowsException, TorqueException;
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 protected abstract <T extends Permission> T doSelectById(Integer id, Connection con)
99 throws NoRowsException, TooManyRowsException, TorqueException;
100
101
102
103
104
105
106
107
108
109
110 @Override
111 public synchronized void renamePermission(Permission permission, String name)
112 throws DataBackendException, UnknownEntityException {
113 if (checkExists(permission)) {
114 permission.setName(name);
115
116 try {
117 TorqueAbstractSecurityEntity p = (TorqueAbstractSecurityEntity) permission;
118 p.setNew(false);
119 p.save();
120 } catch (Exception e) {
121 throw new DataBackendException("Renaming Permission '" + permission.getName() + "' failed", e);
122 }
123 } else {
124 throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
125 }
126 }
127
128
129
130
131
132
133
134
135
136 @Override
137 public synchronized void removePermission(Permission permission)
138 throws DataBackendException, UnknownEntityException {
139 if (checkExists(permission)) {
140 try {
141
142 ((TorqueAbstractSecurityEntity) permission).delete();
143 } catch (TorqueException e) {
144 throw new DataBackendException("Removing Permission '" + permission.getName() + "' failed", e);
145 }
146 } else {
147 throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
148 }
149 }
150
151
152
153
154
155
156
157
158
159 @Override
160 protected synchronized <T extends Permission> T persistNewPermission(T permission) throws DataBackendException {
161 try {
162 ((TorqueAbstractSecurityEntity) permission).save();
163 } catch (Exception e) {
164 throw new DataBackendException("Adding Permission '" + permission.getName() + "' failed", e);
165 }
166
167 return permission;
168 }
169
170
171
172
173
174
175
176
177 @Override
178 public PermissionSet getAllPermissions() throws DataBackendException {
179 PermissionSet permissionSet = new PermissionSet();
180 Connection con = null;
181
182 try {
183 con = Transaction.begin();
184
185 List<Permission> permissions = doSelectAllPermissions(con);
186
187 for (Permission p : permissions) {
188
189 ((TorqueAbstractSecurityEntity) p).retrieveAttachedObjects(con, false);
190 permissionSet.add(p);
191 }
192
193 Transaction.commit(con);
194 con = null;
195 } catch (TorqueException e) {
196 throw new DataBackendException("Error retrieving permission information", e);
197 } finally {
198 if (con != null) {
199 Transaction.safeRollback(con);
200 }
201 }
202
203 return permissionSet;
204 }
205
206
207
208
209
210
211
212
213
214 @Override
215 public boolean checkExists(String permissionName) throws DataBackendException {
216 boolean exists = false;
217
218 Connection con = null;
219
220 try {
221 con = Transaction.begin();
222
223 doSelectByName(permissionName, con);
224
225 Transaction.commit(con);
226 con = null;
227
228 exists = true;
229 } catch (NoRowsException e) {
230 exists = false;
231 } catch (TooManyRowsException e) {
232 throw new DataBackendException("Multiple permissions with same name '" + permissionName + "'");
233 } catch (TorqueException e) {
234 throw new DataBackendException("Error retrieving permission information", e);
235 } finally {
236 if (con != null) {
237 Transaction.safeRollback(con);
238 }
239 }
240
241 return exists;
242 }
243
244
245
246
247
248
249
250
251
252
253 @Override
254 public <T extends Permission> T getPermissionById(Object id) throws DataBackendException, UnknownEntityException {
255 T permission;
256
257 if (id != null && id instanceof Integer) {
258 Connection con = null;
259
260 try {
261 con = Transaction.begin();
262
263 permission = doSelectById((Integer) id, con);
264
265
266 ((TorqueAbstractSecurityEntity) permission).retrieveAttachedObjects(con, false);
267
268 Transaction.commit(con);
269 con = null;
270 } catch (NoRowsException e) {
271 throw new UnknownEntityException("Permission with id '" + id + "' does not exist.", e);
272 } catch (TorqueException e) {
273 throw new DataBackendException("Error retrieving permission information", e);
274 } finally {
275 if (con != null) {
276 Transaction.safeRollback(con);
277 }
278 }
279 } else {
280 throw new UnknownEntityException("Invalid permission id '" + id + "'");
281 }
282
283 return permission;
284 }
285
286
287
288
289
290
291
292
293
294
295 @Override
296 public <T extends Permission> T getPermissionByName(String name)
297 throws DataBackendException, UnknownEntityException {
298 T permission = null;
299 Connection con = null;
300
301 try {
302 con = Transaction.begin();
303
304 permission = doSelectByName(name, con);
305
306
307 ((TorqueAbstractSecurityEntity) permission).retrieveAttachedObjects(con, false);
308
309 Transaction.commit(con);
310 con = null;
311 } catch (NoRowsException e) {
312 throw new UnknownEntityException("Could not find permission " + name);
313 } catch (TooManyRowsException e) {
314 throw new DataBackendException("Multiple Permissions with same name '" + name + "'");
315 } catch (TorqueException e) {
316 throw new DataBackendException("Error retrieving permission information", e);
317 } finally {
318 if (con != null) {
319 Transaction.safeRollback(con);
320 }
321 }
322
323 return permission;
324 }
325 }