1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that manages database access of xml access
|
4
|
* information.
|
5
|
* Copyright: 2009 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Michael Daigle
|
8
|
*
|
9
|
* '$Author: daigle $'
|
10
|
* '$Date: 2009-03-23 13:56:56 -0800 (Mon, 23 Mar 2009) $'
|
11
|
* '$Revision: 4854 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat.accesscontrol;
|
29
|
|
30
|
import java.sql.PreparedStatement;
|
31
|
import java.sql.ResultSet;
|
32
|
import java.sql.SQLException;
|
33
|
import java.util.Vector;
|
34
|
|
35
|
import org.apache.log4j.Logger;
|
36
|
|
37
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
38
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
39
|
import edu.ucsb.nceas.metacat.shared.AccessException;
|
40
|
import edu.ucsb.nceas.metacat.shared.BaseAccess;
|
41
|
|
42
|
public class XMLAccessAccess extends BaseAccess {
|
43
|
|
44
|
private Logger logMetacat = Logger.getLogger(XMLAccessAccess.class);
|
45
|
|
46
|
// Constructor
|
47
|
public XMLAccessAccess() throws AccessException {}
|
48
|
|
49
|
/**
|
50
|
* Get all xml access for a document
|
51
|
*
|
52
|
* @param docId
|
53
|
* the id of the document
|
54
|
* @return an xml access DAO list
|
55
|
*/
|
56
|
public Vector<XMLAccessDAO> getXMLAccessForDoc(String docId) throws AccessException {
|
57
|
|
58
|
Vector<XMLAccessDAO> xmlAccessList = new Vector<XMLAccessDAO>();
|
59
|
|
60
|
if (docId == null) {
|
61
|
throw new AccessException("XMLAccessAccess.getXMLAccessForDoc - doc id " +
|
62
|
"must be specified when selecting xml_access record");
|
63
|
}
|
64
|
|
65
|
// first get the xml access from the db and put it into a DAO list
|
66
|
PreparedStatement pstmt = null;
|
67
|
DBConnection conn = null;
|
68
|
int serialNumber = -1;
|
69
|
try {
|
70
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForDoc");
|
71
|
serialNumber = conn.getCheckOutSerialNumber();
|
72
|
|
73
|
String sql = "SELECT * FROM xml_access WHERE docid = ?";
|
74
|
pstmt = conn.prepareStatement(sql);
|
75
|
|
76
|
pstmt.setString(1, docId);
|
77
|
|
78
|
String sqlReport = "XMLAccessAccess.getXMLAccessForDoc - SQL: " + sql;
|
79
|
sqlReport += " [" + docId + "]";
|
80
|
|
81
|
logMetacat.info(sqlReport);
|
82
|
|
83
|
pstmt.execute();
|
84
|
|
85
|
ResultSet resultSet = pstmt.getResultSet();
|
86
|
while (resultSet.next()) {
|
87
|
XMLAccessDAO xmlAccessDAO = populateDAO(resultSet);
|
88
|
xmlAccessList.add(xmlAccessDAO);
|
89
|
}
|
90
|
|
91
|
// make sure permission orders do not conflict in the database
|
92
|
validateDocXMLAccessList(xmlAccessList);
|
93
|
|
94
|
return xmlAccessList;
|
95
|
|
96
|
} catch (SQLException sqle) {
|
97
|
throw new AccessException("XMLAccessAccess.getXMLAccessForDoc - SQL error when getting access " +
|
98
|
" for doc id: " + docId + " : " + sqle.getMessage());
|
99
|
} catch (PermOrderException poe) {
|
100
|
String errorStr = "XMLAccessAccess.getXMLAccessForDoc - Permission order error when getting " +
|
101
|
"access record for doc id: " + docId + " : " + poe.getMessage();
|
102
|
logMetacat.error(errorStr);
|
103
|
throw new AccessException(errorStr);
|
104
|
} finally {
|
105
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Get all xml access for a principal for a certain document
|
111
|
*
|
112
|
* @param docId
|
113
|
* the id of the document
|
114
|
* @param principalName
|
115
|
* the credentials of the principal in the database
|
116
|
* @return an xml access DAO list
|
117
|
*/
|
118
|
public Vector<XMLAccessDAO> getXMLAccessForPrincipal(String docId, String principalName)
|
119
|
throws AccessException {
|
120
|
|
121
|
Vector<XMLAccessDAO> xmlAccessList = new Vector<XMLAccessDAO>();
|
122
|
|
123
|
if (docId == null) {
|
124
|
throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - doc id " +
|
125
|
"must be specified when selecting xml_access record");
|
126
|
}
|
127
|
if (principalName == null) {
|
128
|
throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - doc id " +
|
129
|
"must be specified when selecting xml_access record");
|
130
|
}
|
131
|
|
132
|
// first get the xml access for this principal from the db and put it into a DAO list
|
133
|
PreparedStatement pstmt = null;
|
134
|
DBConnection conn = null;
|
135
|
int serialNumber = -1;
|
136
|
try {
|
137
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForPrincipal");
|
138
|
serialNumber = conn.getCheckOutSerialNumber();
|
139
|
|
140
|
String sql = "SELECT * FROM xml_access WHERE docid = ? AND principal_name = ?";
|
141
|
pstmt = conn.prepareStatement(sql);
|
142
|
|
143
|
pstmt.setString(1, docId);
|
144
|
pstmt.setString(2, principalName);
|
145
|
|
146
|
String sqlReport = "XMLAccessAccess.getXMLAccessForPrincipal - SQL: " + sql;
|
147
|
sqlReport += " [" + docId + "," + principalName + "]";
|
148
|
|
149
|
logMetacat.info(sqlReport);
|
150
|
|
151
|
pstmt.execute();
|
152
|
|
153
|
ResultSet resultSet = pstmt.getResultSet();
|
154
|
while (resultSet.next()) {
|
155
|
XMLAccessDAO xmlAccessDAO = populateDAO(resultSet);
|
156
|
xmlAccessList.add(xmlAccessDAO);
|
157
|
}
|
158
|
|
159
|
// make sure permission orders do not conflict in the database
|
160
|
validatePrincipalXMLAccessList(xmlAccessList);
|
161
|
|
162
|
return xmlAccessList;
|
163
|
|
164
|
} catch (SQLException sqle) {
|
165
|
throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - SQL error when getting access " +
|
166
|
" for doc id: " + docId + ", principal: " + principalName + " : " + sqle.getMessage());
|
167
|
} catch (PermOrderException poe) {
|
168
|
String errorStr = "XMLAccessAccess.getXMLAccessForPrincipal - Permission order error when getting " +
|
169
|
"access record for doc id: " + docId + ", principal: " + principalName + " : " + poe.getMessage();
|
170
|
logMetacat.error(errorStr);
|
171
|
throw new AccessException(errorStr);
|
172
|
} finally {
|
173
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
174
|
}
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* Get all xml access for a principal/permType/permOrder for a certain document
|
179
|
*
|
180
|
* @param docId
|
181
|
* the id of the document
|
182
|
* @param principalName
|
183
|
* the credentials of the principal in the database
|
184
|
* @return an xml access DAO list
|
185
|
*/
|
186
|
public Vector<XMLAccessDAO> getXMLAccessForPrincipal(String docId, String principalName, String permType, String permOrder)
|
187
|
throws AccessException {
|
188
|
|
189
|
Vector<XMLAccessDAO> xmlAccessList = new Vector<XMLAccessDAO>();
|
190
|
|
191
|
if (docId == null) {
|
192
|
throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - doc id " +
|
193
|
"must be specified when selecting xml_access record");
|
194
|
}
|
195
|
if (principalName == null) {
|
196
|
throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - doc id " +
|
197
|
"must be specified when selecting xml_access record");
|
198
|
}
|
199
|
if (permType == null) {
|
200
|
throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - permission type " +
|
201
|
"must be specified when selecting xml_access record");
|
202
|
}
|
203
|
if (permOrder == null) {
|
204
|
throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - permission order " +
|
205
|
"must be specified when selecting xml_access record");
|
206
|
}
|
207
|
|
208
|
// first get the xml access for this principal from the db and put it into a DAO list
|
209
|
PreparedStatement pstmt = null;
|
210
|
DBConnection conn = null;
|
211
|
int serialNumber = -1;
|
212
|
try {
|
213
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.getXMLAccessForPrincipal");
|
214
|
serialNumber = conn.getCheckOutSerialNumber();
|
215
|
|
216
|
String sql = "SELECT * FROM xml_access WHERE docid = ? AND principal_name = ? " +
|
217
|
"AND perm_type = ? AND perm_order = ?";
|
218
|
pstmt = conn.prepareStatement(sql);
|
219
|
|
220
|
pstmt.setString(1, docId);
|
221
|
pstmt.setString(2, principalName);
|
222
|
pstmt.setString(3, permType);
|
223
|
pstmt.setString(4, permOrder);
|
224
|
|
225
|
String sqlReport = "XMLAccessAccess.getXMLAccessForPrincipal - SQL: " + sql;
|
226
|
sqlReport += " [" + docId + "," + principalName + "," + permType + "," + permOrder + "]";
|
227
|
|
228
|
logMetacat.info(sqlReport);
|
229
|
|
230
|
pstmt.execute();
|
231
|
|
232
|
ResultSet resultSet = pstmt.getResultSet();
|
233
|
while (resultSet.next()) {
|
234
|
XMLAccessDAO xmlAccessDAO = populateDAO(resultSet);
|
235
|
xmlAccessList.add(xmlAccessDAO);
|
236
|
}
|
237
|
|
238
|
validatePrincipalXMLAccessList(xmlAccessList);
|
239
|
|
240
|
return xmlAccessList;
|
241
|
|
242
|
} catch (SQLException sqle) {
|
243
|
throw new AccessException("XMLAccessAccess.getXMLAccessForPrincipal - SQL error when getting access " +
|
244
|
" for doc id: " + docId + ", principal: " + principalName + " : " + sqle.getMessage());
|
245
|
} catch (PermOrderException poe) {
|
246
|
String errorStr = "XMLAccessAccess.getXMLAccessForPrincipal - Permission order error when getting " +
|
247
|
"access record for doc id: " + docId + ", principal: " + principalName + " : " + poe.getMessage();
|
248
|
logMetacat.error(errorStr);
|
249
|
throw new AccessException(errorStr);
|
250
|
} finally {
|
251
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
252
|
}
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Add permissions for a given principal on a given document. If the
|
257
|
* principal already exists, bitwise OR the permission to the existing
|
258
|
* permission and update.
|
259
|
*
|
260
|
* @param docId
|
261
|
* document id
|
262
|
* @param principalName
|
263
|
* principal credentials
|
264
|
* @param permission
|
265
|
* permission bitmap
|
266
|
* @param permType
|
267
|
* permission type
|
268
|
* @param permOrder
|
269
|
* permission order
|
270
|
*/
|
271
|
public void addXMLAccess(String docId, String principalName, Long permission, String permType,
|
272
|
String permOrder) throws AccessException, PermOrderException {
|
273
|
|
274
|
permOrderConflict(docId, permOrder);
|
275
|
|
276
|
Vector<XMLAccessDAO> xmlAccessList =
|
277
|
getXMLAccessForPrincipal(docId, principalName, permType, permOrder);
|
278
|
|
279
|
// if more than one record exists for this principal on this document with the same
|
280
|
// access type / access order combination, call cleanup to combine common access and then
|
281
|
// re-retrieve the access list.
|
282
|
if (xmlAccessList.size() == 0) {
|
283
|
insertXMLAccess(docId, principalName, permission, permType, permOrder);
|
284
|
return;
|
285
|
}
|
286
|
|
287
|
if (xmlAccessList.size() > 1) {
|
288
|
cleanupXMLAccessForPrincipal(xmlAccessList);
|
289
|
xmlAccessList = getXMLAccessForPrincipal(docId, principalName, permType, permOrder);
|
290
|
}
|
291
|
|
292
|
if (xmlAccessList.size() == 0) {
|
293
|
throw new AccessException("XMLAccessAccess.addXMLAccess - xml access list is empty when " +
|
294
|
"it shouldn't be for docid: " + docId + ", prinicpal name: " + principalName + ", perm type " +
|
295
|
permType + ", perm order: " + permOrder);
|
296
|
}
|
297
|
|
298
|
XMLAccessDAO xmlAccessDAO = xmlAccessList.get(0);
|
299
|
|
300
|
// if the permission on the xml access dao does not already contain the permission we are
|
301
|
//trying to add, update the access record with the existing permission bitwis OR-ed with our
|
302
|
// new permission
|
303
|
if ((xmlAccessDAO.getPermission() & permission) != permission) {
|
304
|
updateXMLAccessPermission(docId, principalName, xmlAccessDAO.getPermission() | permission);
|
305
|
}
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* Set permissions for a given document. This means first removing all access control for the
|
310
|
* document and then adding the given rules.
|
311
|
*
|
312
|
* @param docId
|
313
|
* document id
|
314
|
* @param xmlAccessList
|
315
|
* list of xml access dao objects that hold new access for the document
|
316
|
*/
|
317
|
public void replaceAccess(String docId, Vector<XMLAccessDAO> xmlAccessList) throws AccessException {
|
318
|
deleteXMLAccessForDoc(docId);
|
319
|
|
320
|
// if more than one record exists for this principal on this document with the same
|
321
|
// access type / access order combination, call cleanup to combine common access and then
|
322
|
// re-retrieve the access list.
|
323
|
for(XMLAccessDAO xmlAccessDAO : xmlAccessList) {
|
324
|
insertXMLAccess(docId, xmlAccessDAO.getPrincipalName(), xmlAccessDAO.getPermission(),
|
325
|
xmlAccessDAO.getPermType(), xmlAccessDAO.getPermOrder());
|
326
|
}
|
327
|
}
|
328
|
|
329
|
/**
|
330
|
* Insert an xml access record. It is assumed that the checks have already been made to
|
331
|
* make sure the principal does not already have an access record for this document. If
|
332
|
* one does already exist, that record should be updated and this insert not called.
|
333
|
*
|
334
|
* @param docId
|
335
|
* document id
|
336
|
* @param principal
|
337
|
* principal credentials
|
338
|
* @param permission
|
339
|
* permission bitmap
|
340
|
* @param permType
|
341
|
* permission type
|
342
|
* @param permOrder
|
343
|
* permission order
|
344
|
*/
|
345
|
private void insertXMLAccess(String docId, String principalName, Long permission, String permType,
|
346
|
String permOrder) throws AccessException {
|
347
|
if (docId == null) {
|
348
|
throw new AccessException("XMLAccessAccess.insertXMLAccess - docid is required when " +
|
349
|
"inserting XML access record");
|
350
|
}
|
351
|
if (principalName == null) {
|
352
|
throw new AccessException("XMLAccessAccess.insertXMLAccess - principal is required when " +
|
353
|
"inserting XML access record");
|
354
|
}
|
355
|
if (permission == null) {
|
356
|
throw new AccessException("XMLAccessAccess.insertXMLAccess - permission is required when " +
|
357
|
"inserting XML access record");
|
358
|
}
|
359
|
if (permType == null) {
|
360
|
throw new AccessException("XMLAccessAccess.insertXMLAccess - permType is required when " +
|
361
|
"inserting XML access record");
|
362
|
}
|
363
|
if (permOrder == null) {
|
364
|
permOrder = AccessControlInterface.ALLOWFIRST;
|
365
|
}
|
366
|
|
367
|
PreparedStatement pstmt = null;
|
368
|
DBConnection conn = null;
|
369
|
int serialNumber = -1;
|
370
|
try {
|
371
|
// check out DBConnection
|
372
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.insertXMLAccess");
|
373
|
serialNumber = conn.getCheckOutSerialNumber();
|
374
|
|
375
|
String sql = "INSERT INTO xml_access " +
|
376
|
"(docid, principal_name, permission, perm_type, perm_order ) " +
|
377
|
"VALUES (?,?,?,?,?)";
|
378
|
pstmt = conn.prepareStatement(sql);
|
379
|
|
380
|
// Bind the values to the query
|
381
|
pstmt.setString(1, docId);
|
382
|
pstmt.setString(2, principalName);
|
383
|
pstmt.setLong(3, permission);
|
384
|
pstmt.setString(4, permType);
|
385
|
pstmt.setString(5, permOrder);
|
386
|
|
387
|
String sqlReport = "XMLAccessAccess.insertXMLAccess - SQL: " + sql;
|
388
|
sqlReport += " [" + docId + "," + principalName + "," + permission + "," + permType + "," + permOrder + "]";
|
389
|
|
390
|
logMetacat.info(sqlReport);
|
391
|
|
392
|
pstmt.execute();
|
393
|
} catch (SQLException sqle) {
|
394
|
throw new AccessException("XMLAccessAccess.insertXMLAccess - SQL error when inserting"
|
395
|
+ "xml access permissions for doc id: " + docId + ", principal: " +
|
396
|
principalName + ":" + sqle.getMessage());
|
397
|
} finally {
|
398
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
399
|
}
|
400
|
}
|
401
|
|
402
|
/**
|
403
|
* Update existing xml access permissions in the db. The permission value should be the combined
|
404
|
* value of pre-existing permissions plus new permissions.
|
405
|
*
|
406
|
* @param docId
|
407
|
* document id
|
408
|
* @param principalName
|
409
|
* principal credentials
|
410
|
* @param permission
|
411
|
* permission bitmap
|
412
|
*/
|
413
|
private void updateXMLAccessPermission(String docId, String principalName, Long permission)
|
414
|
throws AccessException {
|
415
|
if (docId == null) {
|
416
|
throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - docid is required when " +
|
417
|
"updating XML access record");
|
418
|
}
|
419
|
if (principalName == null) {
|
420
|
throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - principal is required when " +
|
421
|
"updating XML access record");
|
422
|
}
|
423
|
if (permission == null) {
|
424
|
throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - permission is required when " +
|
425
|
"updating XML access record");
|
426
|
}
|
427
|
|
428
|
PreparedStatement pstmt = null;
|
429
|
DBConnection conn = null;
|
430
|
int serialNumber = -1;
|
431
|
try {
|
432
|
// check out DBConnection
|
433
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.updateXMLAccessPermission");
|
434
|
serialNumber = conn.getCheckOutSerialNumber();
|
435
|
|
436
|
String sql = "UPDATE xml_access SET permission = ?" +
|
437
|
"WHERE docid = ? AND principal_name = ?";
|
438
|
pstmt = conn.prepareStatement(sql);
|
439
|
|
440
|
// Bind the values to the query
|
441
|
pstmt.setLong(1, permission);
|
442
|
pstmt.setString(2, docId);
|
443
|
pstmt.setString(3, principalName);
|
444
|
|
445
|
String sqlReport = "XMLAccessAccess.updateXMLAccessPermission - SQL: " + sql;
|
446
|
sqlReport += " [" + permission + "," + docId + "," + principalName + "]";
|
447
|
|
448
|
logMetacat.info(sqlReport);
|
449
|
|
450
|
pstmt.execute();
|
451
|
} catch (SQLException sqle) {
|
452
|
throw new AccessException("XMLAccessAccess.updateXMLAccessPermission - SQL error when updating"
|
453
|
+ "xml access permissions for doc id: " + docId + ", principal: " +
|
454
|
principalName + ":" + sqle.getMessage());
|
455
|
} finally {
|
456
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
457
|
}
|
458
|
|
459
|
}
|
460
|
|
461
|
/**
|
462
|
* Remove xml access. This modifies the access in the database for a principal
|
463
|
* for a given document. If the provided permission is exactly the same as what
|
464
|
* the principal has, the record is deleted from the database.
|
465
|
*
|
466
|
* @param docId
|
467
|
* document id
|
468
|
* @param principalName
|
469
|
* principal credentials
|
470
|
*/
|
471
|
public void removeXMLAccessForPrincipal(String docId, String principalName, Long permission) throws AccessException {
|
472
|
if (docId == null) {
|
473
|
throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - docid is required when " +
|
474
|
"removing XML access");
|
475
|
}
|
476
|
if (principalName == null) {
|
477
|
throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - principal is required when " +
|
478
|
"deleting XML access");
|
479
|
}
|
480
|
if (permission == null) {
|
481
|
throw new AccessException("XMLAccessAccess.removeXMLAccessForPrincipal - permission is required when " +
|
482
|
"updating XML access");
|
483
|
}
|
484
|
|
485
|
Vector<XMLAccessDAO> xmlAccessList = getXMLAccessForPrincipal(docId, principalName);
|
486
|
if (xmlAccessList.size() == 0) {
|
487
|
logMetacat.warn("XMLAccessAccess.removeXMLAccessForPrincipal - attempting to remove access when no " +
|
488
|
"access record exists for docid: " + docId + ", principal: " + principalName);
|
489
|
} else {
|
490
|
long permissionMask = 0;
|
491
|
for (XMLAccessDAO xmlAccessDAO : xmlAccessList) {
|
492
|
permissionMask |= xmlAccessDAO.getPermission();
|
493
|
}
|
494
|
permissionMask |= permission;
|
495
|
|
496
|
// in this case, the only existing permissions are the ones we want to remove, so
|
497
|
// delete the record(s) for this principal on this document
|
498
|
if ((permissionMask & permission) == permission) {
|
499
|
deleteXMLAccessForPrincipal(docId, principalName);
|
500
|
}
|
501
|
|
502
|
if (xmlAccessList.size() > 1) {
|
503
|
|
504
|
} else {
|
505
|
updateXMLAccessPermission(docId, principalName, permission);
|
506
|
}
|
507
|
}
|
508
|
|
509
|
}
|
510
|
|
511
|
/**
|
512
|
* Delete xml access. This removes all access records from the database for a given document
|
513
|
*
|
514
|
* @param docId
|
515
|
* document id
|
516
|
*/
|
517
|
public void deleteXMLAccessForDoc(String docId) throws AccessException {
|
518
|
if (docId == null) {
|
519
|
throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - docid is required when " +
|
520
|
"deleting XML access record");
|
521
|
}
|
522
|
|
523
|
PreparedStatement pstmt = null;
|
524
|
DBConnection conn = null;
|
525
|
int serialNumber = -1;
|
526
|
try {
|
527
|
// check out DBConnection
|
528
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForDoc");
|
529
|
serialNumber = conn.getCheckOutSerialNumber();
|
530
|
|
531
|
String sql = "DELETE FROM xml_access WHERE docid = ?";
|
532
|
pstmt = conn.prepareStatement(sql);
|
533
|
|
534
|
// Bind the values to the query
|
535
|
pstmt.setString(1, docId);
|
536
|
|
537
|
String sqlReport = "XMLAccessAccess.deleteXMLAccessForDoc - SQL: " + sql;
|
538
|
sqlReport += " [" + docId + "]";
|
539
|
|
540
|
logMetacat.info(sqlReport);
|
541
|
|
542
|
pstmt.execute();
|
543
|
} catch (SQLException sqle) {
|
544
|
throw new AccessException("XMLAccessAccess.deleteXMLAccessForDoc - SQL error when deleting"
|
545
|
+ "xml access permissions for doc id: " + docId + ":" + sqle.getMessage());
|
546
|
} finally {
|
547
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
548
|
}
|
549
|
}
|
550
|
|
551
|
/**
|
552
|
* Delete xml access. This removes all access records from the database for a principal
|
553
|
* for a given document
|
554
|
*
|
555
|
* @param docId
|
556
|
* document id
|
557
|
* @param principal
|
558
|
* principal credentials
|
559
|
*/
|
560
|
private void deleteXMLAccessForPrincipal(String docId, String principalName) throws AccessException {
|
561
|
if (docId == null) {
|
562
|
throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - docid is required when " +
|
563
|
"deleting XML access record");
|
564
|
}
|
565
|
if (principalName == null) {
|
566
|
throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - principal is required when " +
|
567
|
"deleting XML access record");
|
568
|
}
|
569
|
|
570
|
PreparedStatement pstmt = null;
|
571
|
DBConnection conn = null;
|
572
|
int serialNumber = -1;
|
573
|
try {
|
574
|
// check out DBConnection
|
575
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForPrincipal");
|
576
|
serialNumber = conn.getCheckOutSerialNumber();
|
577
|
|
578
|
String sql = "DELETE FROM xml_access WHERE docid = ? AND principal_name = ?";
|
579
|
pstmt = conn.prepareStatement(sql);
|
580
|
|
581
|
// Bind the values to the query
|
582
|
pstmt.setString(1, docId);
|
583
|
pstmt.setString(2, principalName);
|
584
|
|
585
|
String sqlReport = "XMLAccessAccess.deleteXMLAccessForPrincipal - SQL: " + sql;
|
586
|
sqlReport += " [" + docId + "," + principalName + "]";
|
587
|
|
588
|
logMetacat.info(sqlReport);
|
589
|
|
590
|
pstmt.execute();
|
591
|
} catch (SQLException sqle) {
|
592
|
throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - SQL error when deleting"
|
593
|
+ "xml access permissions for doc id: " + docId + ", principal: " +
|
594
|
principalName + ":" + sqle.getMessage());
|
595
|
} finally {
|
596
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
597
|
}
|
598
|
}
|
599
|
|
600
|
/**
|
601
|
* Checks to see if there is a permission order conflict for a given document. Each
|
602
|
* document is only allowed to have a single permission order
|
603
|
*
|
604
|
* @param docId
|
605
|
* document id
|
606
|
* @param principal
|
607
|
* principal credentials
|
608
|
*/
|
609
|
private void permOrderConflict(String docId, String permOrder) throws AccessException, PermOrderException {
|
610
|
if (docId == null) {
|
611
|
throw new AccessException("XMLAccessAccess.permOrderConflict - docid is required when " +
|
612
|
"determining perm order conflict");
|
613
|
}
|
614
|
if (permOrder == null) {
|
615
|
throw new AccessException("XMLAccessAccess.permOrderConflict - perm order is required when " +
|
616
|
"determining perm order conflict");
|
617
|
}
|
618
|
|
619
|
PreparedStatement pstmt = null;
|
620
|
DBConnection conn = null;
|
621
|
int serialNumber = -1;
|
622
|
try {
|
623
|
// check out DBConnection
|
624
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.permOrderConflict");
|
625
|
serialNumber = conn.getCheckOutSerialNumber();
|
626
|
|
627
|
String sql = "SELECT * FROM xml_access WHERE docid = ? AND perm_order != ?";
|
628
|
pstmt = conn.prepareStatement(sql);
|
629
|
|
630
|
// Bind the values to the query
|
631
|
pstmt.setString(1, docId);
|
632
|
pstmt.setString(2, permOrder);
|
633
|
|
634
|
String sqlReport = "XMLAccessAccess.permOrderConflict - SQL: " + sql;
|
635
|
sqlReport += " [" + docId + "," + permOrder + "]";
|
636
|
|
637
|
logMetacat.info(sqlReport);
|
638
|
|
639
|
pstmt.execute();
|
640
|
|
641
|
ResultSet resultSet = pstmt.getResultSet();
|
642
|
if (resultSet.next()) {
|
643
|
throw new PermOrderException("XMLAccessAccess.addXMLAccess - cannot add permission " +
|
644
|
"record for doc id: " + docId + "with permOrder: " + permOrder + " due to permOrder conflict");
|
645
|
}
|
646
|
} catch (SQLException sqle) {
|
647
|
throw new AccessException("XMLAccessAccess.permOrderConflict - SQL error when checking"
|
648
|
+ "for perm order conflict on: " + docId + ":" + sqle.getMessage());
|
649
|
} finally {
|
650
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
651
|
}
|
652
|
|
653
|
}
|
654
|
|
655
|
/**
|
656
|
* Delete xml access. This removes all access records from the database for a principal
|
657
|
* for a given document, perm type and perm order
|
658
|
*
|
659
|
* @param docId
|
660
|
* document id
|
661
|
* @param principal
|
662
|
* principal credentials
|
663
|
*/
|
664
|
private void deleteXMLAccessForPrincipal(String docId, String principalName, String permType, String permOrder) throws AccessException {
|
665
|
if (docId == null) {
|
666
|
throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - docid is required when " +
|
667
|
"deleting XML access record");
|
668
|
}
|
669
|
if (principalName == null) {
|
670
|
throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - principal is required when " +
|
671
|
"deleting XML access record");
|
672
|
}
|
673
|
if (permType == null) {
|
674
|
throw new AccessException(
|
675
|
"XMLAccessAccess.deleteXMLAccessForPrincipal - perm type is required when "
|
676
|
+ "deleting XML access record");
|
677
|
}
|
678
|
if (permOrder == null) {
|
679
|
throw new AccessException(
|
680
|
"XMLAccessAccess.deleteXMLAccessForPrincipal - perm order is required when "
|
681
|
+ "deleting XML access record");
|
682
|
}
|
683
|
|
684
|
PreparedStatement pstmt = null;
|
685
|
DBConnection conn = null;
|
686
|
int serialNumber = -1;
|
687
|
try {
|
688
|
// check out DBConnection
|
689
|
conn = DBConnectionPool.getDBConnection("XMLAccessAccess.deleteXMLAccessForPrincipal");
|
690
|
serialNumber = conn.getCheckOutSerialNumber();
|
691
|
|
692
|
String sql = "DELETE FROM xml_access WHERE docid = ? AND principal_name = ? " +
|
693
|
"AND perm_type = ? AND perm_order = ?";
|
694
|
pstmt = conn.prepareStatement(sql);
|
695
|
|
696
|
// Bind the values to the query
|
697
|
pstmt.setString(1, docId);
|
698
|
pstmt.setString(2, principalName);
|
699
|
pstmt.setString(3, permType);
|
700
|
pstmt.setString(4, permOrder);
|
701
|
|
702
|
String sqlReport = "XMLAccessAccess.deleteXMLAccessForPrincipal - SQL: " + sql;
|
703
|
sqlReport += " [" + docId + "," + principalName + "," + permType + "," + permOrder + "]";
|
704
|
|
705
|
logMetacat.info(sqlReport);
|
706
|
|
707
|
pstmt.execute();
|
708
|
} catch (SQLException sqle) {
|
709
|
throw new AccessException("XMLAccessAccess.deleteXMLAccessForPrincipal - SQL error when deleting"
|
710
|
+ "xml access permissions for doc id: " + docId + ", principal: " +
|
711
|
principalName + ":" + sqle.getMessage());
|
712
|
} finally {
|
713
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
714
|
}
|
715
|
|
716
|
}
|
717
|
|
718
|
/**
|
719
|
* Make sure that only one record exists per principal/permType/document. If
|
720
|
* more than one record exists, delete the existing records, consolidate the
|
721
|
* permissions insert the new record.
|
722
|
*
|
723
|
* @param xmlAccessList the access dao list
|
724
|
*/
|
725
|
private void cleanupXMLAccessForPrincipal(Vector<XMLAccessDAO> xmlAccessList) throws AccessException{
|
726
|
|
727
|
int numAllowRecords = 0;
|
728
|
int numDenyRecords = 0;
|
729
|
long allowPermissionMask = 0;
|
730
|
long denyPermissionMask = 0;
|
731
|
String docId = null;
|
732
|
String principalName = null;
|
733
|
String permType = null;
|
734
|
String permOrder = null;
|
735
|
|
736
|
|
737
|
// iterate through the list of access dao objects and bttwise or the permissions. Most
|
738
|
// of this is just doing some error checking to make sure each record is valid.
|
739
|
for (XMLAccessDAO xmlAccessDAO : xmlAccessList) {
|
740
|
if (docId == null) {
|
741
|
docId = xmlAccessDAO.getDocId();
|
742
|
} else {
|
743
|
if (!docId.equals(xmlAccessDAO.getDocId())) {
|
744
|
throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " +
|
745
|
" Conflicting doc ids " + xmlAccessDAO.getDocId() +
|
746
|
" and " + docId);
|
747
|
}
|
748
|
}
|
749
|
if (principalName == null) {
|
750
|
principalName = xmlAccessDAO.getPrincipalName();
|
751
|
} else {
|
752
|
if (!principalName.equals(xmlAccessDAO.getPrincipalName())) {
|
753
|
throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " +
|
754
|
" Conflicting prinicpal names " + xmlAccessDAO.getPrincipalName() +
|
755
|
" and principalName " + principalName);
|
756
|
}
|
757
|
}
|
758
|
if (permType == null) {
|
759
|
permType = xmlAccessDAO.getPermType();
|
760
|
} else {
|
761
|
if (!permType.equals(xmlAccessDAO.getPermType())) {
|
762
|
throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " +
|
763
|
" Conflicting permission orders for document " + xmlAccessDAO.getDocId() +
|
764
|
"principalName " + principalName + ". Database intervention required ");
|
765
|
}
|
766
|
}
|
767
|
if (permOrder == null) {
|
768
|
permOrder = xmlAccessDAO.getPermOrder();
|
769
|
} else {
|
770
|
if (!permOrder.equals(xmlAccessDAO.getPermOrder())) {
|
771
|
throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " +
|
772
|
" Conflicting permission types for document " + xmlAccessDAO.getDocId() +
|
773
|
"principalName " + principalName + ". Database intervention required ");
|
774
|
}
|
775
|
}
|
776
|
if (permType == null) {
|
777
|
permType = xmlAccessDAO.getPermType();
|
778
|
} else {
|
779
|
if (!permType.equals(xmlAccessDAO.getPermType())) {
|
780
|
throw new AccessException("XMLAccessAccess.cleanupXMLAccessForPrincipal - " +
|
781
|
" Conflicting permission orders for document " + xmlAccessDAO.getDocId() +
|
782
|
"principalName " + principalName + ". Database intervention required ");
|
783
|
}
|
784
|
}
|
785
|
if (permType.equals(AccessControlInterface.ALLOW)) {
|
786
|
numAllowRecords++;
|
787
|
allowPermissionMask |= xmlAccessDAO.getPermission();
|
788
|
} else if (permType.equals(AccessControlInterface.DENY)) {
|
789
|
numDenyRecords++;
|
790
|
denyPermissionMask |= xmlAccessDAO.getPermission();
|
791
|
}
|
792
|
}
|
793
|
|
794
|
// if there was more than one allow record, remove all allow records for this user on this doc
|
795
|
// with this perm type and perm order then insert a single record
|
796
|
if (numAllowRecords > 1) {
|
797
|
deleteXMLAccessForPrincipal(docId, principalName, AccessControlInterface.ALLOW, permOrder);
|
798
|
insertXMLAccess(docId, principalName, allowPermissionMask, AccessControlInterface.ALLOW, permOrder);
|
799
|
}
|
800
|
// if there was more than one deny record, remove all deny records for this user on this doc
|
801
|
// with this perm type and perm order then insert a single record
|
802
|
if (numDenyRecords > 1) {
|
803
|
deleteXMLAccessForPrincipal(docId, principalName, AccessControlInterface.DENY, permOrder);
|
804
|
insertXMLAccess(docId, principalName, denyPermissionMask, AccessControlInterface.DENY, permOrder);
|
805
|
}
|
806
|
}
|
807
|
|
808
|
/**
|
809
|
* Make sure for a given list of access DAOs that only one perm order
|
810
|
* exists. It is assumed that all the DAOs are for the same doc
|
811
|
*
|
812
|
* @param xmlAccessList
|
813
|
* the access dao list
|
814
|
*/
|
815
|
private void validateDocXMLAccessList(Vector<XMLAccessDAO> xmlAccessList) throws PermOrderException {
|
816
|
String permOrder = null;
|
817
|
for(XMLAccessDAO xmlAccessDAO : xmlAccessList) {
|
818
|
if (permOrder == null) {
|
819
|
permOrder = xmlAccessDAO.getPermOrder();
|
820
|
} else {
|
821
|
if(!permOrder.equals(xmlAccessDAO.getPermOrder())) {
|
822
|
throw new PermOrderException("XMLAccessAccess.validateXMLAccessList - " +
|
823
|
" Conflicting permission orders for document " + xmlAccessDAO.getDocId() +
|
824
|
". Database intervention required ");
|
825
|
}
|
826
|
}
|
827
|
}
|
828
|
}
|
829
|
|
830
|
/**
|
831
|
* Check that only one permOrder exists for each principal.
|
832
|
* TODO add check that one of each permType exists as well
|
833
|
*
|
834
|
* @param xmlAccessList
|
835
|
* the access dao list
|
836
|
*/
|
837
|
private void validatePrincipalXMLAccessList(Vector<XMLAccessDAO> xmlAccessList)
|
838
|
throws PermOrderException {
|
839
|
|
840
|
boolean allowFirst = false;
|
841
|
boolean denyFirst = false;
|
842
|
String docId = null;
|
843
|
|
844
|
// These vectors will hold all combinations of access DAOs with different permission
|
845
|
// orders and permission types.
|
846
|
Vector<XMLAccessDAO> allowFirstAllows = new Vector<XMLAccessDAO>();
|
847
|
Vector<XMLAccessDAO> allowFirstDenys = new Vector<XMLAccessDAO>();
|
848
|
Vector<XMLAccessDAO> denyFirstAllows = new Vector<XMLAccessDAO>();
|
849
|
Vector<XMLAccessDAO> denyFirstDenys = new Vector<XMLAccessDAO>();
|
850
|
|
851
|
// sort the access dao records into the appropriate vector
|
852
|
for(XMLAccessDAO xmlAccessDAO : xmlAccessList) {
|
853
|
if (docId == null) {
|
854
|
docId = xmlAccessDAO.getDocId();
|
855
|
}
|
856
|
if (xmlAccessDAO.getPermOrder().equals(AccessControlInterface.ALLOWFIRST)) {
|
857
|
allowFirst = true;
|
858
|
if (xmlAccessDAO.getPermType().equals(AccessControlInterface.ALLOW)) {
|
859
|
allowFirstAllows.add(xmlAccessDAO);
|
860
|
} else if (xmlAccessDAO.getPermType().equals(AccessControlInterface.DENY)) {
|
861
|
allowFirstDenys.add(xmlAccessDAO);
|
862
|
} else {
|
863
|
throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " +
|
864
|
" Invalid permission type: " + xmlAccessDAO.getPermType() + " for document " +
|
865
|
xmlAccessDAO.getDocId() + ". Database intervention required ");
|
866
|
}
|
867
|
} else if (xmlAccessDAO.getPermOrder().equals(AccessControlInterface.DENYFIRST)) {
|
868
|
denyFirst = true;
|
869
|
if (xmlAccessDAO.getPermType().equals(AccessControlInterface.ALLOW)) {
|
870
|
denyFirstAllows.add(xmlAccessDAO);
|
871
|
} else if (xmlAccessDAO.getPermType().equals(AccessControlInterface.DENY)) {
|
872
|
denyFirstDenys.add(xmlAccessDAO);
|
873
|
} else {
|
874
|
throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " +
|
875
|
" Invalid permission type: " + xmlAccessDAO.getPermType() + " for document " +
|
876
|
xmlAccessDAO.getDocId() + ". Database intervention required ");
|
877
|
}
|
878
|
} else {
|
879
|
throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " +
|
880
|
" Invalid permission order: " + xmlAccessDAO.getPermOrder() + " for document " +
|
881
|
xmlAccessDAO.getDocId() + ". Database intervention required ");
|
882
|
}
|
883
|
}
|
884
|
|
885
|
// for a given user, there cannot be allowfirst and denyfirst records on the same
|
886
|
// document
|
887
|
if(allowFirst && denyFirst) {
|
888
|
throw new PermOrderException("XMLAccessAccess.validatePrincipalXMLAccessList - " +
|
889
|
" Conflicting permission orders for document " + docId +
|
890
|
". Database intervention required ");
|
891
|
}
|
892
|
}
|
893
|
|
894
|
/**
|
895
|
* Populate a job data object with the current row in a resultset
|
896
|
*
|
897
|
* @param resultSet
|
898
|
* the result set which is already pointing to the desired row.
|
899
|
* @return a scheduled job data object
|
900
|
*/
|
901
|
protected XMLAccessDAO populateDAO(ResultSet resultSet) throws SQLException {
|
902
|
|
903
|
XMLAccessDAO xmlAccessDAO = new XMLAccessDAO();
|
904
|
xmlAccessDAO.setDocId(resultSet.getString("docid"));
|
905
|
xmlAccessDAO.setAccessFileId(resultSet.getString("accessfileid"));
|
906
|
xmlAccessDAO.setPrincipalName(resultSet.getString("principal_name"));
|
907
|
xmlAccessDAO.setPermission(resultSet.getLong("permission"));
|
908
|
xmlAccessDAO.setPermType(resultSet.getString("perm_type"));
|
909
|
xmlAccessDAO.setPermOrder(resultSet.getString("perm_order"));
|
910
|
xmlAccessDAO.setBeginTime(resultSet.getDate("begin_time"));
|
911
|
xmlAccessDAO.setEndTime(resultSet.getDate("end_time"));
|
912
|
xmlAccessDAO.setTicketCount(resultSet.getLong("ticket_count"));
|
913
|
xmlAccessDAO.setSubTreeId(resultSet.getString("subtreeid"));
|
914
|
xmlAccessDAO.setStartNodeId(resultSet.getString("startnodeid"));
|
915
|
xmlAccessDAO.setEndNodeId(resultSet.getString("endnodeid"));
|
916
|
|
917
|
return xmlAccessDAO;
|
918
|
}
|
919
|
|
920
|
}
|