1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class that handles checking permssision for a document
|
4
|
and subtree in a document
|
5
|
*
|
6
|
* Copyright: 2000 Regents of the University of California and the
|
7
|
* National Center for Ecological Analysis and Synthesis
|
8
|
* Authors: Chad Berkley
|
9
|
*
|
10
|
* '$Author: daigle $'
|
11
|
* '$Date: 2009-11-03 10:31:34 -0800 (Tue, 03 Nov 2009) $'
|
12
|
* '$Revision: 5099 $'
|
13
|
*
|
14
|
* This program is free software; you can redistribute it and/or modify
|
15
|
* it under the terms of the GNU General Public License as published by
|
16
|
* the Free Software Foundation; either version 2 of the License, or
|
17
|
* (at your option) any later version.
|
18
|
*
|
19
|
* This program is distributed in the hope that it will be useful,
|
20
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
* GNU General Public License for more details.
|
23
|
*
|
24
|
* You should have received a copy of the GNU General Public License
|
25
|
* along with this program; if not, write to the Free Software
|
26
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27
|
*/
|
28
|
package edu.ucsb.nceas.metacat;
|
29
|
|
30
|
import java.sql.*;
|
31
|
import java.util.Enumeration;
|
32
|
import java.util.Hashtable;
|
33
|
import java.util.Stack;
|
34
|
import java.util.Vector;
|
35
|
import java.util.Iterator;
|
36
|
|
37
|
import org.apache.log4j.Logger;
|
38
|
|
39
|
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
|
40
|
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
|
41
|
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlList;
|
42
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
43
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
44
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
45
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
46
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
47
|
import edu.ucsb.nceas.metacat.util.MetacatUtil;
|
48
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
49
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
50
|
|
51
|
public class PermissionController
|
52
|
{
|
53
|
private String docId = null;
|
54
|
private boolean hasSubTreeAccessControl = false; // flag if has a subtree
|
55
|
// access for this docid
|
56
|
private Vector subTreeList = new Vector();
|
57
|
|
58
|
private static final long TOPLEVELSTARTNODEID = 0; //if start node is 0, means it is top
|
59
|
//level document
|
60
|
|
61
|
private static Logger logMetacat = Logger.getLogger(PermissionController.class);
|
62
|
|
63
|
/**
|
64
|
* Constructor for PermissionController
|
65
|
* @param myDocid the docid need to access
|
66
|
*/
|
67
|
public PermissionController(String myDocid) throws McdbException
|
68
|
{
|
69
|
// Get rid of rev number
|
70
|
docId = DocumentUtil.getSmartDocId(myDocid);
|
71
|
//hasSubTreeAccessControl = checkSubTreeAccessControl();
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Constructor for PermssionController
|
76
|
* @param myDocid String
|
77
|
* @param needDeleteRev boolean
|
78
|
*/
|
79
|
public PermissionController(String myDocid, boolean needDeleteRevFromDocid)
|
80
|
{
|
81
|
if (!needDeleteRevFromDocid)
|
82
|
{
|
83
|
docId = myDocid;
|
84
|
}
|
85
|
else
|
86
|
{
|
87
|
docId = DocumentUtil.getDocIdFromAccessionNumber(myDocid);
|
88
|
}
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Return if a document has subtree access control
|
93
|
*/
|
94
|
public boolean hasSubTreeAccessControl()
|
95
|
{
|
96
|
return hasSubTreeAccessControl;
|
97
|
}
|
98
|
|
99
|
public boolean hasPermission(String sessionId, String myPermission) throws SQLException {
|
100
|
SessionData sessionData = SessionService.getRegisteredSession(sessionId);
|
101
|
if (sessionData == null) {
|
102
|
return false;
|
103
|
}
|
104
|
|
105
|
return hasPermission(sessionData.getUserName(), sessionData.getGroupNames(), myPermission);
|
106
|
}
|
107
|
|
108
|
|
109
|
/**
|
110
|
* Check from db connection if at least one of the list of @principals
|
111
|
* @param user the user name
|
112
|
* @param groups the groups which the use is in
|
113
|
* @param myPermission permission type to check for
|
114
|
*/
|
115
|
public boolean hasPermission(String user, String[]groups, String myPermission)
|
116
|
throws SQLException //, Exception
|
117
|
{
|
118
|
boolean hasPermission=false;
|
119
|
String [] userPackage=null;
|
120
|
int permission =AccessControlList.intValue(myPermission);
|
121
|
|
122
|
//for the commnad line invocation
|
123
|
if ((user==null) && (groups==null || groups.length==0))
|
124
|
{
|
125
|
return true;
|
126
|
}
|
127
|
|
128
|
//create a userpackage including user, public and group member
|
129
|
userPackage=createUsersPackage(user, groups);
|
130
|
|
131
|
//if the requested document is access documents and requested permission
|
132
|
//is "write", the user should have "all" right
|
133
|
|
134
|
if (isAccessDocument(docId) && (permission == AccessControlInterface.WRITE))
|
135
|
{
|
136
|
|
137
|
hasPermission = hasPermission(userPackage,docId, 7);// 7 is all permission
|
138
|
}//if
|
139
|
else //in other situation, just check the request permission
|
140
|
{
|
141
|
|
142
|
|
143
|
// Check for @permission on @docid for @user and/or @groups
|
144
|
hasPermission = hasPermission(userPackage,docId, permission);
|
145
|
|
146
|
}//else
|
147
|
|
148
|
return hasPermission;
|
149
|
}
|
150
|
|
151
|
|
152
|
/**
|
153
|
* Check from db connection if the users in String array @principals has
|
154
|
* @permission on @docid*
|
155
|
* @param principals, names in userPakcage need to check for @permission
|
156
|
* @param docid, document identifier to check on
|
157
|
* @param permission, permission (write or all...) to check for
|
158
|
*/
|
159
|
private boolean hasPermission(String [] principals, String docId,
|
160
|
int permission)
|
161
|
throws SQLException
|
162
|
{
|
163
|
long startId = TOPLEVELSTARTNODEID;// this is for top level, so startid is 0
|
164
|
try
|
165
|
{
|
166
|
//first, if there is a docid owner in user package, return true
|
167
|
//because doc owner has all permssion
|
168
|
if (containDocumentOwner(principals, docId))
|
169
|
{
|
170
|
|
171
|
return true;
|
172
|
}
|
173
|
|
174
|
//If there is no owner in user package, checking the table
|
175
|
//check perm_order
|
176
|
if (isAllowFirst(principals, docId, startId))
|
177
|
{
|
178
|
|
179
|
if (hasExplicitDenyRule(principals, docId, permission, startId))
|
180
|
{
|
181
|
//if it is allowfirst and has deny rule(either explicit )
|
182
|
//deny access
|
183
|
|
184
|
return false;
|
185
|
}//if
|
186
|
else if ( hasAllowRule(principals, docId, permission, startId))
|
187
|
{
|
188
|
//if it is allowfirst and hasn't deny rule and has allow rule
|
189
|
//allow access
|
190
|
|
191
|
return true;
|
192
|
}//else if
|
193
|
else
|
194
|
{
|
195
|
//other situation deny access
|
196
|
|
197
|
return false;
|
198
|
}//else
|
199
|
}//if isAllowFirst
|
200
|
else //denyFirst
|
201
|
{
|
202
|
if (hasAllowRule(principals, docId, permission, startId))
|
203
|
{
|
204
|
//if it is denyFirst and has allow rule, allow access
|
205
|
return true;
|
206
|
}
|
207
|
else
|
208
|
{
|
209
|
//if it is denyfirst but no allow rule, deny access
|
210
|
return false;
|
211
|
}
|
212
|
}//else denyfirst
|
213
|
}//try
|
214
|
catch (Exception e)
|
215
|
{
|
216
|
logMetacat.warn("There is a exception in hasPermission method: "
|
217
|
+e.getMessage());
|
218
|
}
|
219
|
|
220
|
return false;
|
221
|
}//hasPermission
|
222
|
|
223
|
/**
|
224
|
* Method to check if a person has permission to a inline data file
|
225
|
* @param user String
|
226
|
* @param groups String[]
|
227
|
* @param myPermission String
|
228
|
* @param inlineDataId String
|
229
|
* @throws McdbException
|
230
|
* @return boolean
|
231
|
*/
|
232
|
private boolean hasPermissionForInlineData(String user, String[] groups,
|
233
|
String myPermission, String inlineDataId)
|
234
|
throws Exception
|
235
|
{
|
236
|
// this method can call the public method - hasPermission(...)
|
237
|
// the only difference is about the ownership, you couldn't find the owner
|
238
|
// from inlinedataId directly. You should get it from eml document itself
|
239
|
String []userPackage = createUsersPackage(user, groups);
|
240
|
if (containDocumentOwner(userPackage, docId))
|
241
|
{
|
242
|
return true;
|
243
|
}
|
244
|
else
|
245
|
{
|
246
|
PermissionController controller =
|
247
|
new PermissionController(inlineDataId, false);
|
248
|
return controller.hasPermission(user, groups, myPermission);
|
249
|
}
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* The method to determine of a node can be access by a user just by subtree
|
254
|
* access control
|
255
|
*/
|
256
|
public boolean hasPermissionForSubTreeNode(String user, String[] groups,
|
257
|
String myPermission, long nodeId)
|
258
|
throws McdbException
|
259
|
{
|
260
|
boolean flag = true;
|
261
|
// Get unaccessble subtree for this user
|
262
|
Hashtable unaccessableSubTree = hasUnaccessableSubTree(user, groups,
|
263
|
myPermission);
|
264
|
Enumeration en = unaccessableSubTree.elements();
|
265
|
while (en.hasMoreElements())
|
266
|
{
|
267
|
SubTree tree = (SubTree)en.nextElement();
|
268
|
long start = tree.getStartNodeId();
|
269
|
long stop = tree.getEndNodeId();
|
270
|
// nodeid in unaccessablesubtree, return false
|
271
|
if ( nodeId >= start && nodeId <= stop)
|
272
|
{
|
273
|
flag = false;
|
274
|
break;
|
275
|
}
|
276
|
}
|
277
|
return flag;
|
278
|
}
|
279
|
/**
|
280
|
* This method will return a hasTable of subtree which user doesn't has the
|
281
|
* permssion to access
|
282
|
* @param user the user name
|
283
|
* @param groups the groups which the use is in
|
284
|
* @param myPermission permission type to check for
|
285
|
*/
|
286
|
public Hashtable hasUnaccessableSubTree(String user, String[] groups,
|
287
|
String myPermission) throws McdbException
|
288
|
{
|
289
|
Hashtable resultUnaccessableSubTree = new Hashtable();
|
290
|
String [] principals=null;
|
291
|
int permission =AccessControlList.intValue(myPermission);
|
292
|
|
293
|
//for the commnad line invocation return null(no unaccessable subtree)
|
294
|
if ((user==null) && (groups==null || groups.length==0))
|
295
|
{
|
296
|
return resultUnaccessableSubTree;
|
297
|
}
|
298
|
|
299
|
//create a userpackage including user, public and group member
|
300
|
principals=createUsersPackage(user, groups);
|
301
|
//for the document owner return null(no unaccessable subtree)
|
302
|
try
|
303
|
{
|
304
|
if (containDocumentOwner(principals, docId))
|
305
|
{
|
306
|
return resultUnaccessableSubTree;
|
307
|
}
|
308
|
}
|
309
|
catch (SQLException ee)
|
310
|
{
|
311
|
throw new McdbException(ee);
|
312
|
}
|
313
|
|
314
|
// go through every subtree which has access control
|
315
|
for (int i = 0; i< subTreeList.size(); i++)
|
316
|
{
|
317
|
SubTree tree = (SubTree)subTreeList.elementAt(i);
|
318
|
long startId = tree.getStartNodeId();
|
319
|
|
320
|
|
321
|
try
|
322
|
{
|
323
|
if (isAllowFirst(principals, docId, startId))
|
324
|
{
|
325
|
|
326
|
if (hasExplicitDenyRule(principals, docId, permission, startId ))
|
327
|
{
|
328
|
|
329
|
//if it is allowfirst and has deny rule
|
330
|
// put the subtree into unaccessable vector
|
331
|
if (!resultUnaccessableSubTree.containsKey(new Long(startId)))
|
332
|
{
|
333
|
resultUnaccessableSubTree.put(new Long(startId), tree);
|
334
|
}
|
335
|
}//if
|
336
|
else if ( hasAllowRule(principals, docId, permission, startId))
|
337
|
{
|
338
|
//if it is allowfirst and hasn't deny rule and has allow rule
|
339
|
//allow access do nothing
|
340
|
|
341
|
}//else if
|
342
|
else
|
343
|
{
|
344
|
//other situation deny access
|
345
|
if (!resultUnaccessableSubTree.containsKey(new Long(startId)))
|
346
|
{
|
347
|
resultUnaccessableSubTree.put(new Long(startId), tree);
|
348
|
}
|
349
|
|
350
|
}//else
|
351
|
}//if isAllowFirst
|
352
|
else //denyFirst
|
353
|
{
|
354
|
if (hasAllowRule(principals, docId, permission,startId))
|
355
|
{
|
356
|
//if it is denyFirst and has allow rule, allow access, do nothing
|
357
|
|
358
|
}
|
359
|
else
|
360
|
{
|
361
|
//if it is denyfirst but no allow rule, deny access
|
362
|
// add into vector
|
363
|
if (!resultUnaccessableSubTree.containsKey(new Long(startId)))
|
364
|
{
|
365
|
resultUnaccessableSubTree.put(new Long(startId), tree);
|
366
|
}
|
367
|
}
|
368
|
}//else denyfirst
|
369
|
}//try
|
370
|
catch( Exception e)
|
371
|
{
|
372
|
logMetacat.error("error in PermissionControl.has" +
|
373
|
"UnaccessableSubTree "+e.getMessage());
|
374
|
throw new McdbException(e);
|
375
|
}
|
376
|
|
377
|
}//for
|
378
|
// merge the subtree if a subtree is another subtree'subtree
|
379
|
resultUnaccessableSubTree = mergeEquivalentSubtree(resultUnaccessableSubTree);
|
380
|
return resultUnaccessableSubTree;
|
381
|
}//hasUnaccessableSubtree
|
382
|
|
383
|
|
384
|
/*
|
385
|
* A method to merge nested subtree into bigger one. For example subtree b
|
386
|
* is a subtree of subtree a. And user doesn't have read permission for both
|
387
|
* so we only use subtree a is enough.
|
388
|
*/
|
389
|
private Hashtable mergeEquivalentSubtree(Hashtable unAccessSubTree)
|
390
|
{
|
391
|
Hashtable newSubTreeHash = new Hashtable();
|
392
|
boolean needDelete = false;
|
393
|
// check the parameters
|
394
|
if (unAccessSubTree == null || unAccessSubTree.isEmpty())
|
395
|
{
|
396
|
return newSubTreeHash;
|
397
|
}
|
398
|
else
|
399
|
{
|
400
|
// look every subtree start point and stop point, to see if it is embedded
|
401
|
// in another one. If embedded, they are equavelent and we can use bigger
|
402
|
// one to replace smaller one
|
403
|
Enumeration en = unAccessSubTree.elements();
|
404
|
while (en.hasMoreElements())
|
405
|
{
|
406
|
SubTree tree = (SubTree)en.nextElement();
|
407
|
String treeId = tree.getSubTreeId();
|
408
|
long startId = tree.getStartNodeId();
|
409
|
long endId = tree.getEndNodeId();
|
410
|
|
411
|
Enumeration enu = unAccessSubTree.elements();
|
412
|
while (enu.hasMoreElements())
|
413
|
{
|
414
|
SubTree subTree = (SubTree)enu.nextElement();
|
415
|
String subTreeId= subTree.getSubTreeId();
|
416
|
long subTreeStartId = subTree.getStartNodeId();
|
417
|
long subTreeEndId = subTree.getEndNodeId();
|
418
|
//compare and if the first subtree is a subtree of the second
|
419
|
// one, set neeDelete true
|
420
|
if (startId > subTreeStartId && endId < subTreeEndId)
|
421
|
{
|
422
|
needDelete = true;
|
423
|
logMetacat.info("the subtree: "+ treeId +
|
424
|
" need to be get rid of from unaccessable"+
|
425
|
" subtree list becuase it is a subtree of"+
|
426
|
" another subtree in the list");
|
427
|
break;
|
428
|
}//if
|
429
|
}//while
|
430
|
// if not need to delete, put the subtree into hash
|
431
|
if (!needDelete)
|
432
|
{
|
433
|
newSubTreeHash.put(new Long(startId), tree);
|
434
|
}
|
435
|
//reset needDelete
|
436
|
needDelete = false;
|
437
|
}//while
|
438
|
return newSubTreeHash;
|
439
|
}//else
|
440
|
}
|
441
|
|
442
|
/**
|
443
|
* Check if a document id is a access document. Access document need user
|
444
|
* has "all" permission to access it.
|
445
|
*
|
446
|
* @param docId,
|
447
|
* the document id need to be checked
|
448
|
*/
|
449
|
private boolean isAccessDocument(String docId) throws SQLException {
|
450
|
// detele the rev number if docid contains it
|
451
|
docId = DocumentUtil.getDocIdFromString(docId);
|
452
|
PreparedStatement pStmt = null;
|
453
|
DBConnection conn = null;
|
454
|
int serialNumber = -1;
|
455
|
try {
|
456
|
// check out DBConnection
|
457
|
conn = DBConnectionPool.getDBConnection("PermissionControl.isAccessDoc");
|
458
|
serialNumber = conn.getCheckOutSerialNumber();
|
459
|
pStmt = conn.prepareStatement("select doctype from xml_documents where "
|
460
|
+ "docid like '" + docId + "'");
|
461
|
pStmt.execute();
|
462
|
ResultSet rs = pStmt.getResultSet();
|
463
|
boolean hasRow = rs.next();
|
464
|
String doctype = null;
|
465
|
if (hasRow) {
|
466
|
doctype = rs.getString(1);
|
467
|
|
468
|
}
|
469
|
pStmt.close();
|
470
|
|
471
|
// if it is an access document
|
472
|
if (doctype != null
|
473
|
&& ((MetacatUtil.getOptionList(PropertyService
|
474
|
.getProperty("xml.accessdoctype")).contains(doctype)))) {
|
475
|
|
476
|
return true;
|
477
|
}
|
478
|
|
479
|
} catch (SQLException e) {
|
480
|
throw new SQLException("PermissionControl.isAccessDocument "
|
481
|
+ "Error checking" + " on document " + docId + ". " + e.getMessage());
|
482
|
} catch (PropertyNotFoundException pnfe) {
|
483
|
throw new SQLException("PermissionControl.isAccessDocument "
|
484
|
+ "Error checking" + " on document " + docId + ". " + pnfe.getMessage());
|
485
|
} finally {
|
486
|
try {
|
487
|
pStmt.close();
|
488
|
} finally {
|
489
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
490
|
}
|
491
|
}
|
492
|
|
493
|
return false;
|
494
|
}// isAccessDocument
|
495
|
|
496
|
|
497
|
|
498
|
/**
|
499
|
* Check if a stirng array contains a given documents' owner
|
500
|
*
|
501
|
* @param principals,
|
502
|
* a string array storing the username, groups name and public.
|
503
|
* @param docid,
|
504
|
* the id of given documents
|
505
|
*/
|
506
|
private boolean containDocumentOwner( String[] principals, String docId)
|
507
|
throws SQLException
|
508
|
{
|
509
|
int lengthOfArray=principals.length;
|
510
|
boolean hasRow;
|
511
|
PreparedStatement pStmt=null;
|
512
|
DBConnection conn = null;
|
513
|
int serialNumber = -1;
|
514
|
|
515
|
try
|
516
|
{
|
517
|
//check out DBConnection
|
518
|
conn=DBConnectionPool.getDBConnection("PermissionControl.containDocOnwer");
|
519
|
serialNumber=conn.getCheckOutSerialNumber();
|
520
|
pStmt = conn.prepareStatement(
|
521
|
"SELECT 'x' FROM xml_documents " +
|
522
|
"WHERE docid = ? AND lower(user_owner) = ?");
|
523
|
//check every element in the string array too see if it conatains
|
524
|
//the owner of document
|
525
|
for (int i=0; i<lengthOfArray; i++)
|
526
|
{
|
527
|
|
528
|
// Bind the values to the query
|
529
|
pStmt.setString(1, docId);
|
530
|
pStmt.setString(2, principals[i]);
|
531
|
logMetacat.info("the principle stack is : " +
|
532
|
principals[i]);
|
533
|
|
534
|
pStmt.execute();
|
535
|
ResultSet rs = pStmt.getResultSet();
|
536
|
hasRow = rs.next();
|
537
|
if (hasRow)
|
538
|
{
|
539
|
pStmt.close();
|
540
|
logMetacat.info("find the owner");
|
541
|
return true;
|
542
|
}//if
|
543
|
|
544
|
}//for
|
545
|
}//try
|
546
|
catch (SQLException e)
|
547
|
{
|
548
|
pStmt.close();
|
549
|
|
550
|
throw new
|
551
|
SQLException("PermissionControl.hasPermission - " +
|
552
|
"Error checking ownership for " + principals[0] +
|
553
|
" on document #" + docId + ". " + e.getMessage());
|
554
|
}//catch
|
555
|
finally
|
556
|
{
|
557
|
try
|
558
|
{
|
559
|
pStmt.close();
|
560
|
}
|
561
|
finally
|
562
|
{
|
563
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
564
|
}
|
565
|
}
|
566
|
return false;
|
567
|
}//containDocumentOwner
|
568
|
|
569
|
/**
|
570
|
* Check if the permission order for user at that documents is allowFirst
|
571
|
* @param principals, list of names of principals to check for
|
572
|
* @param docid, document identifier to check for
|
573
|
*/
|
574
|
private boolean isAllowFirst(String [] principals, String docId,
|
575
|
long startId)
|
576
|
throws SQLException, Exception
|
577
|
{
|
578
|
int lengthOfArray=principals.length;
|
579
|
boolean hasRow;
|
580
|
PreparedStatement pStmt = null;
|
581
|
DBConnection conn = null;
|
582
|
int serialNumber = -1;
|
583
|
String sql = null;
|
584
|
boolean topLever =false;
|
585
|
if (startId == TOPLEVELSTARTNODEID)
|
586
|
{
|
587
|
//top level
|
588
|
topLever = true;
|
589
|
sql = "SELECT perm_order FROM xml_access " +
|
590
|
"WHERE lower(principal_name) = ? AND docid = ? AND startnodeid is NULL";
|
591
|
}
|
592
|
else
|
593
|
{
|
594
|
//sub tree level
|
595
|
sql = "SELECT perm_order FROM xml_access " +
|
596
|
"WHERE lower(principal_name)= ? AND docid = ? AND startnodeid = ?";
|
597
|
}
|
598
|
|
599
|
try
|
600
|
{
|
601
|
//check out DBConnection
|
602
|
conn=DBConnectionPool.getDBConnection("AccessControlList.isAllowFirst");
|
603
|
serialNumber=conn.getCheckOutSerialNumber();
|
604
|
|
605
|
//select permission order from database
|
606
|
pStmt = conn.prepareStatement(sql);
|
607
|
|
608
|
//check every name in the array
|
609
|
for (int i=0; i<lengthOfArray;i++)
|
610
|
{
|
611
|
//bind value
|
612
|
pStmt.setString(1, principals[i]);//user name
|
613
|
pStmt.setString(2, docId);//docid
|
614
|
|
615
|
// if subtree, we need set subtree id
|
616
|
if (!topLever)
|
617
|
{
|
618
|
pStmt.setLong(3, startId);
|
619
|
}
|
620
|
|
621
|
pStmt.execute();
|
622
|
ResultSet rs = pStmt.getResultSet();
|
623
|
hasRow=rs.next();
|
624
|
if (hasRow)
|
625
|
{
|
626
|
//get the permission order from data base
|
627
|
String permissionOrder=rs.getString(1);
|
628
|
//if the permission order is "allowFirst
|
629
|
if (permissionOrder.equalsIgnoreCase(AccessControlInterface.ALLOWFIRST))
|
630
|
{
|
631
|
pStmt.close();
|
632
|
return true;
|
633
|
}
|
634
|
else
|
635
|
{
|
636
|
pStmt.close();
|
637
|
return false;
|
638
|
}
|
639
|
}//if
|
640
|
}//for
|
641
|
}//try
|
642
|
catch (SQLException e)
|
643
|
{
|
644
|
throw e;
|
645
|
}
|
646
|
finally
|
647
|
{
|
648
|
try
|
649
|
{
|
650
|
pStmt.close();
|
651
|
}
|
652
|
finally
|
653
|
{
|
654
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
655
|
}
|
656
|
}
|
657
|
|
658
|
//if reach here, means there is no permssion record for given names and
|
659
|
//docid. So throw a exception.
|
660
|
|
661
|
throw new Exception("PermissionController.isAllowFirst - There is no permission record for user "+ principals[0] +
|
662
|
" at document " + docId);
|
663
|
|
664
|
}//isAllowFirst
|
665
|
|
666
|
/**
|
667
|
* Check if the users array has allow rules for given users, docid and
|
668
|
* permission.
|
669
|
* If it has permission rule and ticket count is greater than 0, the ticket
|
670
|
* number will decrease one for every allow rule
|
671
|
* @param principals, list of names of principals to check for
|
672
|
* @param docid, document identifier to check for
|
673
|
* @param permission, the permssion need to check
|
674
|
*/
|
675
|
private boolean hasAllowRule(String [] principals, String docId,
|
676
|
int permission, long startId)
|
677
|
throws SQLException, Exception
|
678
|
{
|
679
|
int lengthOfArray=principals.length;
|
680
|
boolean allow=false;//initial value is no allow rule
|
681
|
ResultSet rs;
|
682
|
PreparedStatement pStmt = null;
|
683
|
int permissionValue=permission;
|
684
|
int permissionValueInTable;
|
685
|
int ticketCount;
|
686
|
DBConnection conn = null;
|
687
|
int serialNumber = -1;
|
688
|
boolean topLever = false;
|
689
|
String sql = null;
|
690
|
if (startId == TOPLEVELSTARTNODEID)
|
691
|
{
|
692
|
// for toplevel
|
693
|
topLever = true;
|
694
|
sql = "SELECT permission FROM xml_access WHERE docid = ? " +
|
695
|
"AND lower(principal_name) = ? AND perm_type = ? AND startnodeid is NULL";
|
696
|
}
|
697
|
else
|
698
|
{
|
699
|
topLever =false;
|
700
|
sql = "SELECT permission FROM xml_access WHERE docid = ? " +
|
701
|
"AND lower(principal_name) = ? AND perm_type = ? AND startnodeid = ?";
|
702
|
}
|
703
|
try
|
704
|
{
|
705
|
//check out DBConnection
|
706
|
conn=DBConnectionPool.getDBConnection("AccessControlList.hasAllowRule");
|
707
|
serialNumber=conn.getCheckOutSerialNumber();
|
708
|
//This sql statement will select entry with
|
709
|
//begin_time<=currentTime<=end_time in xml_access table
|
710
|
//If begin_time or end_time is null in table, isnull(begin_time, sysdate)
|
711
|
//function will assign begin_time=sysdate
|
712
|
pStmt = conn.prepareStatement(sql);
|
713
|
//bind docid, perm_type
|
714
|
pStmt.setString(1, docId);
|
715
|
pStmt.setString(3, AccessControlInterface.ALLOW);
|
716
|
|
717
|
// if subtree lever, need to set subTreeId
|
718
|
if (!topLever)
|
719
|
{
|
720
|
pStmt.setLong(4, startId);
|
721
|
}
|
722
|
|
723
|
//bind every elenment in user name array
|
724
|
for (int i=0;i<lengthOfArray; i++)
|
725
|
{
|
726
|
pStmt.setString(2, principals[i]);
|
727
|
pStmt.execute();
|
728
|
rs=pStmt.getResultSet();
|
729
|
while (rs.next())//check every entry for one user
|
730
|
{
|
731
|
permissionValueInTable=rs.getInt(1);
|
732
|
|
733
|
//permission is ok
|
734
|
//the user have a permission to access the file
|
735
|
if (( permissionValueInTable & permissionValue )== permissionValue )
|
736
|
{
|
737
|
|
738
|
allow=true;//has allow rule entry
|
739
|
}//if
|
740
|
}//while
|
741
|
}//for
|
742
|
}//try
|
743
|
catch (SQLException sqlE)
|
744
|
{
|
745
|
throw sqlE;
|
746
|
}
|
747
|
catch (Exception e)
|
748
|
{
|
749
|
throw e;
|
750
|
}
|
751
|
finally
|
752
|
{
|
753
|
try
|
754
|
{
|
755
|
pStmt.close();
|
756
|
}
|
757
|
finally
|
758
|
{
|
759
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
760
|
}
|
761
|
}
|
762
|
return allow;
|
763
|
}//hasAllowRule
|
764
|
|
765
|
|
766
|
|
767
|
/**
|
768
|
* Check if the users array has explicit deny rules for given users, docid
|
769
|
* and permission. That means the perm_type is deny and current time is
|
770
|
* less than end_time and greater than begin time, or no time limit.
|
771
|
* @param principals, list of names of principals to check for
|
772
|
* @param docid, document identifier to check for
|
773
|
* @param permission, the permssion need to check
|
774
|
*/
|
775
|
private boolean hasExplicitDenyRule(String [] principals, String docId,
|
776
|
int permission, long startId)
|
777
|
throws SQLException
|
778
|
{
|
779
|
int lengthOfArray=principals.length;
|
780
|
ResultSet rs;
|
781
|
PreparedStatement pStmt = null;
|
782
|
int permissionValue=permission;
|
783
|
int permissionValueInTable;
|
784
|
DBConnection conn = null;
|
785
|
int serialNumber = -1;
|
786
|
String sql = null;
|
787
|
boolean topLevel = false;
|
788
|
|
789
|
// decide top level or subtree level
|
790
|
if (startId == TOPLEVELSTARTNODEID)
|
791
|
{
|
792
|
topLevel = true;
|
793
|
sql = "SELECT permission FROM xml_access WHERE docid = ? " +
|
794
|
"AND lower(principal_name) = ? AND perm_type = ? AND startnodeid is NULL";
|
795
|
}
|
796
|
else
|
797
|
{
|
798
|
topLevel = false;
|
799
|
sql = "SELECT permission FROM xml_access WHERE docid = ? " +
|
800
|
"AND lower(principal_name) = ? AND perm_type = ? AND startnodeid = ?";
|
801
|
}
|
802
|
|
803
|
try
|
804
|
{
|
805
|
//check out DBConnection
|
806
|
conn=DBConnectionPool.getDBConnection("PermissionControl.hasExplicitDeny");
|
807
|
serialNumber=conn.getCheckOutSerialNumber();
|
808
|
|
809
|
pStmt = conn.prepareStatement(sql);
|
810
|
//bind docid, perm_type
|
811
|
pStmt.setString(1, docId);
|
812
|
pStmt.setString(3, AccessControlInterface.DENY);
|
813
|
|
814
|
// subtree level need to set up subtreeid
|
815
|
if (!topLevel)
|
816
|
{
|
817
|
pStmt.setLong(4, startId);
|
818
|
}
|
819
|
|
820
|
//bind every elenment in user name array
|
821
|
for (int i=0;i<lengthOfArray; i++)
|
822
|
{
|
823
|
pStmt.setString(2, principals[i]);
|
824
|
pStmt.execute();
|
825
|
rs=pStmt.getResultSet();
|
826
|
while (rs.next())//check every entry for one user
|
827
|
{
|
828
|
permissionValueInTable=rs.getInt(1);
|
829
|
|
830
|
//permission is ok the user doesn't have permission to access the file
|
831
|
if (( permissionValueInTable & permissionValue )== permissionValue )
|
832
|
|
833
|
{
|
834
|
pStmt.close();
|
835
|
return true;
|
836
|
}//if
|
837
|
}//while
|
838
|
}//for
|
839
|
}//try
|
840
|
catch (SQLException e)
|
841
|
{
|
842
|
throw e;
|
843
|
}//catch
|
844
|
finally
|
845
|
{
|
846
|
try
|
847
|
{
|
848
|
pStmt.close();
|
849
|
}
|
850
|
finally
|
851
|
{
|
852
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
853
|
}
|
854
|
}//finally
|
855
|
return false;//no deny rule
|
856
|
}//hasExplicitDenyRule
|
857
|
|
858
|
|
859
|
/**
|
860
|
* Creat a users pakages to check permssion rule, user itself, public and
|
861
|
* the gourps the user belong will be include in this package
|
862
|
* @param user, the name of user
|
863
|
* @param groups, the string array of the groups that user belong to
|
864
|
*/
|
865
|
private String[] createUsersPackage(String user, String [] groups)
|
866
|
{
|
867
|
String [] usersPackage=null;
|
868
|
int lengthOfPackage;
|
869
|
|
870
|
if (groups!=null)
|
871
|
{
|
872
|
//if gouprs is not null and user is not public, we should create a array
|
873
|
//to store the groups and user and public.
|
874
|
//So the length of userPackage is the length of group plus two
|
875
|
if (!user.equalsIgnoreCase(AccessControlInterface.PUBLIC))
|
876
|
{
|
877
|
lengthOfPackage=(groups.length)+2;
|
878
|
usersPackage=new String [lengthOfPackage];
|
879
|
//the first two elements is user self and public
|
880
|
//in order to ignore case sensitive, we transfer user to lower case
|
881
|
if (user != null)
|
882
|
{
|
883
|
usersPackage[0]= user.toLowerCase();
|
884
|
logMetacat.info("after transfer to lower case(not null): "+
|
885
|
usersPackage[0]);
|
886
|
}
|
887
|
else
|
888
|
{
|
889
|
usersPackage[0] = user;
|
890
|
usersPackage[0]= user.toLowerCase();
|
891
|
logMetacat.info("after transfer to lower case(null): "+
|
892
|
usersPackage[0]);
|
893
|
}
|
894
|
usersPackage[1]=AccessControlInterface.PUBLIC;
|
895
|
//put groups element from index 0 to lengthOfPackage-3 into userPackage
|
896
|
//from index 2 to lengthOfPackage-1
|
897
|
for (int i=2; i<lengthOfPackage; i++)
|
898
|
{
|
899
|
//tansfer group to lower case too
|
900
|
if (groups[i-2] != null)
|
901
|
{
|
902
|
usersPackage[i]=groups[i-2].toLowerCase();
|
903
|
}
|
904
|
} //for
|
905
|
}//if user!=public
|
906
|
else//use=public
|
907
|
{
|
908
|
lengthOfPackage=(groups.length)+1;
|
909
|
usersPackage=new String [lengthOfPackage];
|
910
|
//the first lements is public
|
911
|
usersPackage[0]=AccessControlInterface.PUBLIC;
|
912
|
//put groups element from index 0 to lengthOfPackage-2 into userPackage
|
913
|
//from index 1 to lengthOfPackage-1
|
914
|
for (int i=1; i<lengthOfPackage; i++)
|
915
|
{
|
916
|
if (groups[i-1] != null)
|
917
|
{
|
918
|
usersPackage[i]=groups[i-1].toLowerCase();
|
919
|
}
|
920
|
} //for
|
921
|
}//else user=public
|
922
|
|
923
|
}//if groups!=null
|
924
|
else
|
925
|
{
|
926
|
//because no groups, the userPackage only need two elements
|
927
|
//one is for user, the other is for public
|
928
|
if (!user.equalsIgnoreCase(AccessControlInterface.PUBLIC))
|
929
|
{
|
930
|
lengthOfPackage=2;
|
931
|
usersPackage=new String [lengthOfPackage];
|
932
|
if (user != null)
|
933
|
{
|
934
|
usersPackage[0]=user.toLowerCase();
|
935
|
}
|
936
|
else
|
937
|
{
|
938
|
usersPackage[0]=user;
|
939
|
}
|
940
|
usersPackage[1]=AccessControlInterface.PUBLIC;
|
941
|
}//if user!=public
|
942
|
else //user==public
|
943
|
{
|
944
|
//only put public into array
|
945
|
lengthOfPackage=1;
|
946
|
usersPackage=new String [lengthOfPackage];
|
947
|
usersPackage[0]=AccessControlInterface.PUBLIC;
|
948
|
}
|
949
|
}//else groups==null
|
950
|
return usersPackage;
|
951
|
}//createUsersPackage
|
952
|
|
953
|
|
954
|
/**
|
955
|
* A static method to get Hashtable which cointains a inlinedata object list that
|
956
|
* user can't read it. The key is subtree id of inlinedata, the data is
|
957
|
* internal file name for the inline data which is stored as docid
|
958
|
* in xml_access table or data object doc id.
|
959
|
* @param docidWithoutRev, metadata docid which should be the accessfileid
|
960
|
* in the table
|
961
|
* @param user , the name of user
|
962
|
* @param groups, the group which the user belong to
|
963
|
*/
|
964
|
public static Hashtable<String, String> getUnReadableInlineDataIdList(String docidWithoutRev,
|
965
|
String user, String[] groups,
|
966
|
boolean withRevision)
|
967
|
throws Exception
|
968
|
{
|
969
|
Hashtable<String, String> inlineDataList = getUnAccessableInlineDataIdList(docidWithoutRev,
|
970
|
user, groups, AccessControlInterface.READSTRING,
|
971
|
withRevision);
|
972
|
|
973
|
return inlineDataList;
|
974
|
}
|
975
|
|
976
|
/**
|
977
|
* A static method to get Hashtable which cointains a inline data object list that
|
978
|
* user can't overwrite it. The key is subtree id of inline data distrubition,
|
979
|
* the value is internal file name for the inline data which is stored as docid
|
980
|
* in xml_access table or data object doc id.
|
981
|
* @param docidWithoutRev, metadata docid which should be the accessfileid
|
982
|
* in the table
|
983
|
* @param user , the name of user
|
984
|
* @param groups, the group which the user belong to
|
985
|
*/
|
986
|
public static Hashtable<String, String> getUnWritableInlineDataIdList(String docidWithoutRev,
|
987
|
String user, String[] groups,
|
988
|
boolean withRevision)
|
989
|
throws Exception
|
990
|
{
|
991
|
Hashtable<String, String> inlineDataList = getUnAccessableInlineDataIdList(docidWithoutRev,
|
992
|
user, groups, AccessControlInterface.WRITESTRING,
|
993
|
withRevision);
|
994
|
|
995
|
return inlineDataList;
|
996
|
}
|
997
|
|
998
|
|
999
|
/*
|
1000
|
* This method will get hashtable which contains a unaccessable distribution
|
1001
|
* inlinedata object list
|
1002
|
*
|
1003
|
* withRevision is used to get inline id list with or without revision number
|
1004
|
* e.g. when withRevision is true, temp.1.1.1, temp.1.1.2 would be returned
|
1005
|
* otherwise temp.1.1 and temp.1.2 would be returned.
|
1006
|
*/
|
1007
|
private static Hashtable<String, String> getUnAccessableInlineDataIdList(String docid,
|
1008
|
String user, String[] groups, String permission,
|
1009
|
boolean withRevision)
|
1010
|
throws SQLException,McdbException, Exception
|
1011
|
{
|
1012
|
Hashtable<String, String> unAccessibleIdList = new Hashtable();
|
1013
|
if (user == null) {
|
1014
|
return unAccessibleIdList;
|
1015
|
}
|
1016
|
|
1017
|
Hashtable allIdList = getAllInlineDataIdList(docid);
|
1018
|
Enumeration<String> en = allIdList.keys();
|
1019
|
while (en.hasMoreElements())
|
1020
|
{
|
1021
|
String subTreeId = (String) en.nextElement();
|
1022
|
String fileId = (String) allIdList.get(subTreeId);
|
1023
|
//Here fileid is internal file id for line data. It stored in docid
|
1024
|
// field in xml_access table. so we don't need to delete rev
|
1025
|
PermissionController controller = new PermissionController(docid, false);
|
1026
|
if (!controller.hasPermissionForInlineData(user, groups, permission, fileId))
|
1027
|
{
|
1028
|
if(withRevision)
|
1029
|
{
|
1030
|
logMetacat.info("Put subtree id " + subTreeId +
|
1031
|
" and " + "inline data file name " +
|
1032
|
fileId + " into " + "un" + permission +
|
1033
|
" hash");
|
1034
|
unAccessibleIdList.put(subTreeId, fileId);
|
1035
|
|
1036
|
}
|
1037
|
else
|
1038
|
{
|
1039
|
logMetacat.info("Put subtree id " + subTreeId +
|
1040
|
" and " + "inline data file name " +
|
1041
|
DocumentUtil.getInlineDataIdWithoutRev(fileId) +
|
1042
|
" into " + "un" + permission +
|
1043
|
" hash");
|
1044
|
unAccessibleIdList.put(subTreeId,
|
1045
|
DocumentUtil.getInlineDataIdWithoutRev(fileId));
|
1046
|
}
|
1047
|
}
|
1048
|
}
|
1049
|
return unAccessibleIdList;
|
1050
|
}
|
1051
|
|
1052
|
|
1053
|
/*
|
1054
|
* This method will get a hash table from xml_access table for all records
|
1055
|
* about the inline data. The key is subtree id and data is a inline internal
|
1056
|
* file name
|
1057
|
*/
|
1058
|
private static Hashtable getAllInlineDataIdList(String docid) throws SQLException
|
1059
|
{
|
1060
|
Hashtable inlineDataList = new Hashtable();
|
1061
|
String sql = "SELECT subtreeid, docid FROM xml_access WHERE " +
|
1062
|
"accessfileid = ? AND subtreeid IS NOT NULL";
|
1063
|
PreparedStatement pStmt=null;
|
1064
|
ResultSet rs=null;
|
1065
|
DBConnection conn=null;
|
1066
|
int serialNumber=-1;
|
1067
|
try
|
1068
|
{
|
1069
|
//check out DBConnection
|
1070
|
conn=DBConnectionPool.getDBConnection("PermissionControl.getDataSetId");
|
1071
|
serialNumber=conn.getCheckOutSerialNumber();
|
1072
|
pStmt=conn.prepareStatement(sql);
|
1073
|
//bind the value to query
|
1074
|
pStmt.setString(1, docid);
|
1075
|
//execute the query
|
1076
|
pStmt.execute();
|
1077
|
rs=pStmt.getResultSet();
|
1078
|
//process the result
|
1079
|
while(rs.next())
|
1080
|
{
|
1081
|
String subTreeId = rs.getString(1);
|
1082
|
String inlineDataId = rs.getString(2);
|
1083
|
if (subTreeId != null && !subTreeId.trim().equals("") &&
|
1084
|
inlineDataId != null && !inlineDataId.trim().equals(""))
|
1085
|
{
|
1086
|
inlineDataList.put(subTreeId, inlineDataId);
|
1087
|
}
|
1088
|
}//while
|
1089
|
}//try
|
1090
|
finally
|
1091
|
{
|
1092
|
try
|
1093
|
{
|
1094
|
pStmt.close();
|
1095
|
}
|
1096
|
finally
|
1097
|
{
|
1098
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
1099
|
}
|
1100
|
}//finally
|
1101
|
return inlineDataList;
|
1102
|
}//getAllInlineDataIdList
|
1103
|
}
|