1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that loads eml-access.xml file containing ACL
|
4
|
* for a metadata document into relational DB
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Jivka Bojilova
|
8
|
*
|
9
|
* '$Author: tao $'
|
10
|
* '$Date: 2014-12-16 18:50:35 -0800 (Tue, 16 Dec 2014) $'
|
11
|
* '$Revision: 9024 $'
|
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.io.IOException;
|
31
|
import java.io.StringReader;
|
32
|
import java.sql.PreparedStatement;
|
33
|
import java.sql.ResultSet;
|
34
|
import java.sql.SQLException;
|
35
|
import java.util.Vector;
|
36
|
|
37
|
import org.apache.log4j.Logger;
|
38
|
import org.xml.sax.ContentHandler;
|
39
|
import org.xml.sax.ErrorHandler;
|
40
|
import org.xml.sax.InputSource;
|
41
|
import org.xml.sax.SAXException;
|
42
|
import org.xml.sax.XMLReader;
|
43
|
import org.xml.sax.helpers.XMLReaderFactory;
|
44
|
|
45
|
import edu.ucsb.nceas.metacat.DBUtil;
|
46
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
47
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
48
|
import edu.ucsb.nceas.metacat.McdbException;
|
49
|
import edu.ucsb.nceas.metacat.PermissionController;
|
50
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
51
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
52
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
53
|
import edu.ucsb.nceas.metacat.shared.AccessException;
|
54
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
55
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
56
|
import edu.ucsb.nceas.utilities.access.AccessControlInterface;
|
57
|
import edu.ucsb.nceas.utilities.access.DocInfoHandler;
|
58
|
import edu.ucsb.nceas.utilities.access.XMLAccessDAO;
|
59
|
|
60
|
|
61
|
/**
|
62
|
* A Class that loads eml-access.xml file containing ACL for a metadata
|
63
|
* document into relational DB. It extends DefaultHandler class to handle
|
64
|
* SAX parsing events when processing the XML stream.
|
65
|
*/
|
66
|
public class AccessControlForSingleFile implements AccessControlInterface
|
67
|
{
|
68
|
|
69
|
private String _guid;
|
70
|
|
71
|
private Logger logMetacat = Logger.getLogger(AccessControlForSingleFile.class);
|
72
|
|
73
|
/**
|
74
|
* Construct an instance of the AccessControlForSingleFile class. This
|
75
|
* instance will represent one file only.
|
76
|
*
|
77
|
* @param myAccessNumber
|
78
|
* the docid or docid with dev will be controlled
|
79
|
*/
|
80
|
public AccessControlForSingleFile(String accessionNumber)
|
81
|
throws AccessControlException {
|
82
|
|
83
|
// this is a local metacat id
|
84
|
String docid = DocumentUtil.getDocIdFromString(accessionNumber);
|
85
|
// get the revision
|
86
|
String revision = DocumentUtil.getRevisionStringFromString(accessionNumber);
|
87
|
int rev = -1;
|
88
|
if (revision != null) {
|
89
|
// we were given it
|
90
|
rev = Integer.valueOf(revision);
|
91
|
} else {
|
92
|
// look up the latest
|
93
|
try {
|
94
|
rev = DBUtil.getLatestRevisionInDocumentTable(docid);
|
95
|
} catch (SQLException e) {
|
96
|
AccessControlException ace = new AccessControlException(e.getMessage());
|
97
|
ace.initCause(e);
|
98
|
throw ace;
|
99
|
}
|
100
|
if (rev <= 0) {
|
101
|
// look in the revisions table
|
102
|
try {
|
103
|
rev = DBUtil.getMaxRevFromRevisionTable(docid);
|
104
|
} catch (SQLException e) {
|
105
|
AccessControlException ace = new AccessControlException(e.getMessage());
|
106
|
ace.initCause(e);
|
107
|
throw ace;
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
|
112
|
// find the guid for this docid.rev
|
113
|
try {
|
114
|
_guid = IdentifierManager.getInstance().getGUID(docid, rev);
|
115
|
} catch (McdbDocNotFoundException e) {
|
116
|
AccessControlException ace = new AccessControlException(e.getMessage());
|
117
|
ace.initCause(e);
|
118
|
throw ace;
|
119
|
}
|
120
|
|
121
|
// couldn't find it?
|
122
|
if (_guid == null || _guid.equals("")) {
|
123
|
throw new AccessControlException("Guid cannot be null");
|
124
|
}
|
125
|
|
126
|
logMetacat.debug("AccessControlForSingleFile() - docid: " + _guid);
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Insert a single access record into the database based on access DAO
|
131
|
* object
|
132
|
*
|
133
|
* @param xmlAccessDAO
|
134
|
* dao object holding info to insert
|
135
|
*/
|
136
|
public void insertPermissions(XMLAccessDAO xmlAccessDAO)
|
137
|
throws AccessControlException, PermOrderException{
|
138
|
insertPermissions(xmlAccessDAO.getPrincipalName(), xmlAccessDAO.getPermission(),
|
139
|
xmlAccessDAO.getPermType(), xmlAccessDAO.getPermOrder(), xmlAccessDAO.getAccessFileId(), xmlAccessDAO.getSubTreeId());
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* Insert a single access record into the database.
|
144
|
*
|
145
|
* @param principalName
|
146
|
* the principal credentials
|
147
|
* @param permission
|
148
|
* the permission
|
149
|
* @param permType
|
150
|
* the permission type
|
151
|
* @param permOrder
|
152
|
* the permission order
|
153
|
*/
|
154
|
public void insertPermissions(String principalName, Long permission, String permType, String permOrder, String accessFileId, String subTreeId)
|
155
|
throws AccessControlException, PermOrderException {
|
156
|
try {
|
157
|
// The addXMLAccess method will create the permission record if it does not exist.
|
158
|
// It will bitwise OR to permissions if the principal already has a record for this
|
159
|
// doc id.
|
160
|
XMLAccessAccess xmlAccessAccess = new XMLAccessAccess();
|
161
|
//System.out.println("permission in accessControlForSingleFile.insertPermissions: " + permission);
|
162
|
xmlAccessAccess.addXMLAccess(_guid, principalName, new Long(permission), permType, permOrder, accessFileId, subTreeId);
|
163
|
} catch (AccessException ae) {
|
164
|
throw new AccessControlException("AccessControlForSingleFile.insertPermissions - "
|
165
|
+ "DB access error when inserting permissions: " + ae.getMessage());
|
166
|
}
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Replace existing permissions with a given block of permissions for this
|
171
|
* document.
|
172
|
*
|
173
|
* @param accessBlock
|
174
|
* the xml access block. This is the same structure as that
|
175
|
* returned by the getdocumentinfo action in metacat.
|
176
|
*/
|
177
|
public void insertPermissions(String accessBlock) throws AccessControlException {
|
178
|
try {
|
179
|
// use DocInfoHandler to parse the access section into DAO objects
|
180
|
XMLReader parser = null;
|
181
|
DocInfoHandler docInfoHandler = new DocInfoHandler(_guid);
|
182
|
ContentHandler chandler = docInfoHandler;
|
183
|
|
184
|
// Get an instance of the parser
|
185
|
String parserName = PropertyService.getProperty("xml.saxparser");
|
186
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
187
|
|
188
|
// Turn off validation
|
189
|
parser.setFeature("http://xml.org/sax/features/validation", false);
|
190
|
parser.setContentHandler((ContentHandler)chandler);
|
191
|
parser.setErrorHandler((ErrorHandler)chandler);
|
192
|
|
193
|
parser.parse(new InputSource(new StringReader(accessBlock)));
|
194
|
|
195
|
XMLAccessAccess xmlAccessAccess = new XMLAccessAccess();
|
196
|
|
197
|
// replace all access on the document
|
198
|
Vector<XMLAccessDAO> accessControlList = docInfoHandler.getAccessControlList();
|
199
|
xmlAccessAccess.replaceAccess(_guid, accessControlList);
|
200
|
|
201
|
} catch (PropertyNotFoundException pnfe) {
|
202
|
throw new AccessControlException("AccessControlForSingleFile.insertPermissions - "
|
203
|
+ "property error when replacing permissions: " + pnfe.getMessage());
|
204
|
} catch (AccessException ae) {
|
205
|
throw new AccessControlException("AccessControlForSingleFile.insertPermissions - "
|
206
|
+ "DB access error when replacing permissions: " + ae.getMessage());
|
207
|
} catch (SAXException se) {
|
208
|
throw new AccessControlException("AccessControlForSingleFile.insertPermissions - "
|
209
|
+ "SAX error when replacing permissions: " + se.getMessage());
|
210
|
} catch(IOException ioe) {
|
211
|
throw new AccessControlException("AccessControlForSingleFile.insertPermissions - "
|
212
|
+ "I/O error when replacing permissions: " + ioe.getMessage());
|
213
|
}
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* Check if access control comination for
|
218
|
* docid/principal/permission/permorder/permtype already exists.
|
219
|
*
|
220
|
* @param xmlAccessDAO
|
221
|
* the dao object holding the access we want to check for.
|
222
|
* @return true if the Access Control for this file already exists in the DB
|
223
|
*/
|
224
|
public boolean accessControlExists(XMLAccessDAO xmlAccessDAO) throws AccessControlException {
|
225
|
boolean exists = false;
|
226
|
PreparedStatement pstmt = null;
|
227
|
DBConnection conn = null;
|
228
|
int serialNumber = -1;
|
229
|
try {
|
230
|
//check out DBConnection
|
231
|
conn=DBConnectionPool.getDBConnection
|
232
|
("AccessControlForSingleFiel.accessControlExists");
|
233
|
serialNumber=conn.getCheckOutSerialNumber();
|
234
|
pstmt = conn.prepareStatement(
|
235
|
"SELECT * FROM xml_access " +
|
236
|
"WHERE guid = ? " +
|
237
|
"AND principal_name = ? " +
|
238
|
"AND permission = ? " +
|
239
|
"AND perm_type = ? " +
|
240
|
"AND perm_order = ? ");
|
241
|
|
242
|
// Bind the values to the query
|
243
|
pstmt.setString(1, _guid);
|
244
|
pstmt.setString(2, xmlAccessDAO.getPrincipalName());
|
245
|
pstmt.setLong(3, xmlAccessDAO.getPermission());
|
246
|
pstmt.setString(4, xmlAccessDAO.getPermType());
|
247
|
pstmt.setString(5, xmlAccessDAO.getPermOrder());
|
248
|
|
249
|
pstmt.execute();
|
250
|
ResultSet rs = pstmt.getResultSet();
|
251
|
exists = rs.next();
|
252
|
|
253
|
} catch (SQLException sqle){
|
254
|
throw new AccessControlException("AccessControlForSingleFile.accessControlExists - SQL error when " +
|
255
|
"checking if access control exists: " + sqle.getMessage());
|
256
|
} finally {
|
257
|
try {
|
258
|
if(pstmt != null) {
|
259
|
pstmt.close();
|
260
|
}
|
261
|
} catch (SQLException sqle) {
|
262
|
logMetacat.error("AccessControlForSingleFile.accessControlExists - Could not close " +
|
263
|
"prepared statement: " +sqle.getMessage());
|
264
|
} finally {
|
265
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
266
|
}
|
267
|
}
|
268
|
|
269
|
return exists;
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* Get Access Control List information for document from db connetion. User
|
274
|
* or Group should have permissions for reading access control information
|
275
|
* for a document specified by
|
276
|
*
|
277
|
* @param user
|
278
|
* name of user connected to Metacat system
|
279
|
* @param groups
|
280
|
* names of user's groups to which user belongs
|
281
|
*/
|
282
|
public String getACL(String user, String[] groups)
|
283
|
throws AccessControlException {
|
284
|
StringBuffer output = new StringBuffer();
|
285
|
boolean hasPermission = false;
|
286
|
|
287
|
try {
|
288
|
hasPermission = isOwned(user);
|
289
|
if (!hasPermission) {
|
290
|
// get the docid for this guid
|
291
|
String docid = IdentifierManager.getInstance().getLocalId(_guid);
|
292
|
PermissionController controller = new PermissionController(docid);
|
293
|
hasPermission =
|
294
|
controller.hasPermission(user, groups, READSTRING);
|
295
|
}
|
296
|
|
297
|
// if the user has permissions, get the access dao list for this doc and return
|
298
|
// it as a string. Otherwise, get the string for an empty access dao list
|
299
|
// (which will return the access section with no allow or deny sections)
|
300
|
if (hasPermission) {
|
301
|
// Get a list of all access dao objects for this docid
|
302
|
XMLAccessAccess xmlAccessAccess = new XMLAccessAccess();
|
303
|
Vector<XMLAccessDAO> xmlAccessDAOList = xmlAccessAccess.getXMLAccessForDoc(_guid);
|
304
|
output.append(getAccessString(xmlAccessDAOList));
|
305
|
} else {
|
306
|
output.append(getAccessString(new Vector<XMLAccessDAO>()));
|
307
|
}
|
308
|
|
309
|
return output.toString();
|
310
|
|
311
|
} catch (SQLException sqle) {
|
312
|
throw new AccessControlException("AccessControlForSingleFile.getACL() - SQL error when " +
|
313
|
"getting ACL: " + sqle.getMessage());
|
314
|
} catch (AccessException ae) {
|
315
|
throw new AccessControlException("AccessControlForSingleFile.getACL() - DB access error when " +
|
316
|
"getting ACL: " + ae.getMessage());
|
317
|
} catch (McdbException mcdb) {
|
318
|
throw new AccessControlException("AccessControlForSingleFile.getACL() - MCDB error when " +
|
319
|
"getting ACL: " + mcdb.getMessage());
|
320
|
}
|
321
|
}
|
322
|
|
323
|
/**
|
324
|
* Get the access xml for all access on this docid
|
325
|
*
|
326
|
* @return string representation of access
|
327
|
*/
|
328
|
public String getAccessString() throws AccessControlException {
|
329
|
Vector<XMLAccessDAO> xmlAccessDAOList = null;
|
330
|
|
331
|
try {
|
332
|
// Get a list of all access dao objects for this docid
|
333
|
XMLAccessAccess xmlAccessAccess = new XMLAccessAccess();
|
334
|
xmlAccessDAOList = xmlAccessAccess.getXMLAccessForDoc(_guid);
|
335
|
} catch (AccessException ae) {
|
336
|
throw new AccessControlException("AccessControlForSingleFile.getAccessString() - DB access error when " +
|
337
|
"getting access string: " + ae.getMessage());
|
338
|
}
|
339
|
|
340
|
return getAccessString(xmlAccessDAOList);
|
341
|
}
|
342
|
|
343
|
/**
|
344
|
* Put together an xml representation of the objects in a given access dao list
|
345
|
* @param xmlAccessDAOList list of xml access DAO objects
|
346
|
* @return string representation of access
|
347
|
*/
|
348
|
public String getAccessString(Vector<XMLAccessDAO> xmlAccessDAOList) throws AccessControlException {
|
349
|
|
350
|
StringBuffer output = new StringBuffer();
|
351
|
StringBuffer tmpOutput = new StringBuffer();
|
352
|
StringBuffer allowOutput = new StringBuffer();
|
353
|
StringBuffer denyOutput = new StringBuffer();
|
354
|
|
355
|
String principal = null;
|
356
|
int permission = -1;
|
357
|
String permOrder = ALLOWFIRST;
|
358
|
String permType = null;
|
359
|
String accessfileid = null;
|
360
|
String subtreeid = null;
|
361
|
|
362
|
// We assume that all the records will have the same permission order, so we can just
|
363
|
// grab the perm order from the first one.
|
364
|
if (xmlAccessDAOList.size() > 0) {
|
365
|
permOrder = xmlAccessDAOList.get(0).getPermOrder();
|
366
|
accessfileid = xmlAccessDAOList.get(0).getAccessFileId();
|
367
|
subtreeid = xmlAccessDAOList.get(0).getSubTreeId();
|
368
|
}
|
369
|
|
370
|
// get the docid for this guid
|
371
|
String docid = _guid;
|
372
|
try {
|
373
|
docid = IdentifierManager.getInstance().getLocalId(_guid);
|
374
|
} catch (McdbDocNotFoundException e) {
|
375
|
logMetacat.warn("Could not lookup docid for guid, defaulting to guid: " + _guid, e);
|
376
|
} catch (SQLException e){
|
377
|
throw new AccessControlException("Couldn't identify the local id of the object with the specified identifier "+_guid+" since "+e.getMessage());
|
378
|
}
|
379
|
|
380
|
output.append("<access authSystem=\"knb\" order=\"" + permOrder + "\" id=\"" + docid + "\" scope=\"document\"");
|
381
|
if (accessfileid != null) {
|
382
|
output.append(" accessfileid=\"" + accessfileid + "\"");
|
383
|
}
|
384
|
if (subtreeid != null) {
|
385
|
output.append(" subtreeid=\"" + subtreeid + "\"");
|
386
|
}
|
387
|
|
388
|
output.append(">\n");
|
389
|
|
390
|
for (XMLAccessDAO xmlAccessDAO : xmlAccessDAOList) {
|
391
|
principal = xmlAccessDAO.getPrincipalName();
|
392
|
permission = xmlAccessDAO.getPermission().intValue();
|
393
|
//System.out.println("accessControlForSingleFile.getAccessString: permission is set to: " + permission);
|
394
|
permType = xmlAccessDAO.getPermType();
|
395
|
|
396
|
tmpOutput.append(" <" + permType + ">\n");
|
397
|
tmpOutput.append(" <principal>" + principal + "</principal>\n");
|
398
|
|
399
|
if ((permission & READ) == READ) {
|
400
|
tmpOutput.append(" <permission>read</permission>\n");
|
401
|
}
|
402
|
if ((permission & WRITE) == WRITE) {
|
403
|
tmpOutput.append(" <permission>write</permission>\n");
|
404
|
}
|
405
|
if ((permission & ALL) == ALL) {
|
406
|
tmpOutput.append(" <permission>all</permission>\n");
|
407
|
}
|
408
|
if ((permission & CHMOD) == CHMOD) {
|
409
|
tmpOutput.append(" <permission>chmod</permission>\n");
|
410
|
}
|
411
|
|
412
|
tmpOutput.append(" </" + permType + ">\n");
|
413
|
|
414
|
if (permType.equals(ALLOW)) {
|
415
|
allowOutput.append(tmpOutput);
|
416
|
} else if (permType.equals(DENY)) {
|
417
|
denyOutput.append(tmpOutput);
|
418
|
}
|
419
|
tmpOutput = new StringBuffer();
|
420
|
}
|
421
|
|
422
|
// This just orders the allow/deny sections based on the permOrder. Not
|
423
|
// required, but convenient later when parsing the output.
|
424
|
if (permOrder.equals(ALLOWFIRST)) {
|
425
|
output.append(allowOutput);
|
426
|
output.append(denyOutput);
|
427
|
} else if (permOrder.equals(DENYFIRST)) {
|
428
|
output.append(denyOutput);
|
429
|
output.append(allowOutput);
|
430
|
}
|
431
|
|
432
|
output.append("</access>");
|
433
|
|
434
|
return output.toString();
|
435
|
}
|
436
|
|
437
|
/**
|
438
|
* check if the docid represented in this class is owned by the user
|
439
|
*
|
440
|
* @param user
|
441
|
* the user credentials
|
442
|
* @return true if doc is owned by user, false otherwise
|
443
|
*/
|
444
|
private boolean isOwned(String user) throws SQLException {
|
445
|
PreparedStatement pstmt = null;
|
446
|
DBConnection conn = null;
|
447
|
int serialNumber = -1;
|
448
|
try {
|
449
|
// check out DBConnection
|
450
|
conn = DBConnectionPool.getDBConnection("AccessControlList.isOwned");
|
451
|
serialNumber = conn.getCheckOutSerialNumber();
|
452
|
String query =
|
453
|
"SELECT id.guid FROM xml_documents xd, identifier id "
|
454
|
+ "WHERE xd.docid = id.docid " +
|
455
|
"AND xd.rev = id.rev " +
|
456
|
"AND id.guid = ? " +
|
457
|
"AND user_owner = ? ";
|
458
|
pstmt = conn.prepareStatement(query );
|
459
|
pstmt.setString(1, _guid);
|
460
|
pstmt.setString(2, user);
|
461
|
pstmt.execute();
|
462
|
ResultSet rs = pstmt.getResultSet();
|
463
|
boolean hasRow = rs.next();
|
464
|
return hasRow;
|
465
|
} finally {
|
466
|
try {
|
467
|
if (pstmt != null) {
|
468
|
pstmt.close();
|
469
|
}
|
470
|
} finally {
|
471
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
472
|
}
|
473
|
}
|
474
|
}
|
475
|
}
|