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
|
* Release: @release@
|
10
|
*
|
11
|
* '$Author: tao $'
|
12
|
* '$Date: 2003-03-13 17:35:26 -0800 (Thu, 13 Mar 2003) $'
|
13
|
* '$Revision: 1474 $'
|
14
|
*
|
15
|
* This program is free software; you can redistribute it and/or modify
|
16
|
* it under the terms of the GNU General Public License as published by
|
17
|
* the Free Software Foundation; either version 2 of the License, or
|
18
|
* (at your option) any later version.
|
19
|
*
|
20
|
* This program is distributed in the hope that it will be useful,
|
21
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23
|
* GNU General Public License for more details.
|
24
|
*
|
25
|
* You should have received a copy of the GNU General Public License
|
26
|
* along with this program; if not, write to the Free Software
|
27
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
28
|
*/
|
29
|
package edu.ucsb.nceas.metacat;
|
30
|
|
31
|
import java.sql.*;
|
32
|
import java.util.Hashtable;
|
33
|
import java.util.Stack;
|
34
|
import java.util.Vector;
|
35
|
|
36
|
public class PermissionController
|
37
|
{
|
38
|
private String docId = null;
|
39
|
private boolean hasSubTreeAccessControl = false; // flag if has a subtree
|
40
|
// access for this docid
|
41
|
private Vector subTreeList = new Vector();
|
42
|
|
43
|
|
44
|
/**
|
45
|
* Constructor for PermissionController
|
46
|
* @param myDocid the docid need to access
|
47
|
*/
|
48
|
public PermissionController(String myDocid) throws McdbException
|
49
|
{
|
50
|
// Get rid of rev number
|
51
|
docId = MetaCatUtil.getDocIdFromString(myDocid);
|
52
|
hasSubTreeAccessControl = checkSubTreeAccessControl();
|
53
|
}
|
54
|
|
55
|
/*
|
56
|
* Go through the access table and find if has subtree access control
|
57
|
* if has, store the subtree into list and return true. Otherwise,
|
58
|
* return false
|
59
|
*/
|
60
|
private boolean checkSubTreeAccessControl() throws McdbException
|
61
|
{
|
62
|
boolean flag = false;
|
63
|
PreparedStatement pStmt=null;
|
64
|
ResultSet rs=null;
|
65
|
DBConnection conn=null;
|
66
|
int serialNumber=-1;
|
67
|
String query="SELECT subtreeid, startnodeid, endnodeid from xml_access " +
|
68
|
"where docid =? ";
|
69
|
|
70
|
try
|
71
|
{
|
72
|
//check out DBConnection
|
73
|
conn=DBConnectionPool.getDBConnection("PermissionController.hasSubTreeA");
|
74
|
serialNumber=conn.getCheckOutSerialNumber();
|
75
|
|
76
|
pStmt=conn.prepareStatement(query);
|
77
|
//bind the value to query
|
78
|
pStmt.setString(1, docId);
|
79
|
//execute the query
|
80
|
pStmt.execute();
|
81
|
rs=pStmt.getResultSet();
|
82
|
//process the result
|
83
|
while (rs.next()) //
|
84
|
{
|
85
|
String subTreeId = rs.getString(1);
|
86
|
MetaCatUtil.debugMessage("subtreeid: "+subTreeId, 35);
|
87
|
long startNodeId = rs.getLong(2);
|
88
|
MetaCatUtil.debugMessage("subtree startNodeId: "+startNodeId, 35);
|
89
|
long endNodeId = rs.getLong(3);
|
90
|
MetaCatUtil.debugMessage("subtree endNodeId: "+endNodeId, 35);
|
91
|
// if startnodeid field in table is empty,startNodeId will be 0
|
92
|
if (subTreeId != null && startNodeId != 0 && startNodeId != 0)
|
93
|
{
|
94
|
flag = true;// has subtree control
|
95
|
SubTree tree = new SubTree();
|
96
|
tree.setSubTreeId(subTreeId);
|
97
|
tree.setStartNodeId(startNodeId);
|
98
|
tree.setEndNodeId(endNodeId);
|
99
|
subTreeList.add(tree);
|
100
|
}
|
101
|
}
|
102
|
}//try
|
103
|
catch (SQLException e)
|
104
|
{
|
105
|
throw new McdbException(e);
|
106
|
}
|
107
|
finally
|
108
|
{
|
109
|
try
|
110
|
{
|
111
|
pStmt.close();
|
112
|
}
|
113
|
catch(SQLException ee)
|
114
|
{
|
115
|
throw new McdbException(ee);
|
116
|
}
|
117
|
finally
|
118
|
{
|
119
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
120
|
}
|
121
|
}
|
122
|
return flag;
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Check from db connection if at least one of the list of @principals
|
127
|
* @param user the user name
|
128
|
* @param groups the groups which the use is in
|
129
|
* @param myPermission permission type to check for
|
130
|
*/
|
131
|
public boolean hasPermission(String user, String[]groups, String myPermission)
|
132
|
throws SQLException, Exception
|
133
|
{
|
134
|
boolean hasPermission=false;
|
135
|
String [] userPackage=null;
|
136
|
int permission =AccessControlList.intValue(myPermission);
|
137
|
|
138
|
//for the commnad line invocation
|
139
|
if ((user==null) && (groups==null || groups.length==0))
|
140
|
{
|
141
|
return true;
|
142
|
}
|
143
|
|
144
|
//create a userpackage including user, public and group member
|
145
|
userPackage=createUsersPackage(user, groups);
|
146
|
|
147
|
//if the requested document is access documents and requested permission
|
148
|
//is "write", the user should have "all" right
|
149
|
if (isAccessDocument(docId) && (permission == AccessControlInterface.WRITE))
|
150
|
{
|
151
|
hasPermission = hasPermission(userPackage,docId, 7);// 7 is all permission
|
152
|
}//if
|
153
|
else //in other situation, just check the request permission
|
154
|
{
|
155
|
|
156
|
// Check for @permission on @docid for @user and/or @groups
|
157
|
hasPermission = hasPermission(userPackage,docId, permission);
|
158
|
|
159
|
}//else
|
160
|
|
161
|
return hasPermission;
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Check from db connection if the users in String array @principals has
|
166
|
* @permission on @docid*
|
167
|
* @param principals, names in userPakcage need to check for @permission
|
168
|
* @param docid, document identifier to check on
|
169
|
* @param permission, permission (write or all...) to check for
|
170
|
*/
|
171
|
private boolean hasPermission(String [] principals, String docId,
|
172
|
int permission)
|
173
|
throws SQLException
|
174
|
{
|
175
|
String subTreeId = null;// this is for top level, so subtree id is null
|
176
|
try
|
177
|
{
|
178
|
//first, if there is a docid owner in user package, return true
|
179
|
//because doc owner has all permssion
|
180
|
if (containDocumentOwner(principals, docId))
|
181
|
{
|
182
|
|
183
|
return true;
|
184
|
}
|
185
|
|
186
|
//If there is no owner in user package, checking the table
|
187
|
//check perm_order
|
188
|
if (isAllowFirst(principals, docId, subTreeId))
|
189
|
{
|
190
|
|
191
|
if (hasExplicitDenyRule(principals, docId, permission, subTreeId))
|
192
|
{
|
193
|
//if it is allowfirst and has deny rule(either explicit )
|
194
|
//deny access
|
195
|
return false;
|
196
|
}//if
|
197
|
else if ( hasAllowRule(principals, docId, permission, subTreeId))
|
198
|
{
|
199
|
//if it is allowfirst and hasn't deny rule and has allow rule
|
200
|
//allow access
|
201
|
return true;
|
202
|
}//else if
|
203
|
else
|
204
|
{
|
205
|
//other situation deny access
|
206
|
return false;
|
207
|
}//else
|
208
|
}//if isAllowFirst
|
209
|
else //denyFirst
|
210
|
{
|
211
|
if (hasAllowRule(principals, docId, permission, subTreeId))
|
212
|
{
|
213
|
//if it is denyFirst and has allow rule, allow access
|
214
|
return true;
|
215
|
}
|
216
|
else
|
217
|
{
|
218
|
//if it is denyfirst but no allow rule, deny access
|
219
|
return false;
|
220
|
}
|
221
|
}//else denyfirst
|
222
|
}//try
|
223
|
catch (Exception e)
|
224
|
{
|
225
|
MetaCatUtil.debugMessage("There is a exception in hasPermission method: "
|
226
|
+e.getMessage(), 50);
|
227
|
}
|
228
|
|
229
|
return false;
|
230
|
}//hasPermission
|
231
|
|
232
|
/**
|
233
|
* This method will return a hasTable of subtree which user doesn't has the
|
234
|
* permssion to access
|
235
|
* @param user the user name
|
236
|
* @param groups the groups which the use is in
|
237
|
* @param myPermission permission type to check for
|
238
|
*/
|
239
|
public Hashtable hasUnaccessableSubTree(String user, String[] groups,
|
240
|
String myPermission) throws McdbException
|
241
|
{
|
242
|
Hashtable resultUnaccessableSubTree = new Hashtable();
|
243
|
String [] principals=null;
|
244
|
int permission =AccessControlList.intValue(myPermission);
|
245
|
|
246
|
//for the commnad line invocation return null(no unaccessable subtree)
|
247
|
if ((user==null) && (groups==null || groups.length==0))
|
248
|
{
|
249
|
return resultUnaccessableSubTree;
|
250
|
}
|
251
|
|
252
|
//create a userpackage including user, public and group member
|
253
|
principals=createUsersPackage(user, groups);
|
254
|
//for the document owner return null(no unaccessable subtree)
|
255
|
try
|
256
|
{
|
257
|
if (containDocumentOwner(principals, docId))
|
258
|
{
|
259
|
return resultUnaccessableSubTree;
|
260
|
}
|
261
|
}
|
262
|
catch (SQLException ee)
|
263
|
{
|
264
|
throw new McdbException(ee);
|
265
|
}
|
266
|
|
267
|
// go through every subtree which has access control
|
268
|
for (int i = 0; i< subTreeList.size(); i++)
|
269
|
{
|
270
|
SubTree tree = (SubTree)subTreeList.elementAt(i);
|
271
|
String subTreeId = (String)(tree.getSubTreeId());
|
272
|
|
273
|
if (subTreeId != null)
|
274
|
{
|
275
|
try
|
276
|
{
|
277
|
if (isAllowFirst(principals, docId, subTreeId))
|
278
|
{
|
279
|
|
280
|
if (hasExplicitDenyRule(principals, docId, permission, subTreeId))
|
281
|
{
|
282
|
|
283
|
//if it is allowfirst and has deny rule
|
284
|
// put the subtree into unaccessable vector
|
285
|
if (!resultUnaccessableSubTree.containsKey(subTreeId))
|
286
|
{
|
287
|
resultUnaccessableSubTree.put(subTreeId, tree);
|
288
|
}
|
289
|
}//if
|
290
|
else if ( hasAllowRule(principals, docId, permission, subTreeId))
|
291
|
{
|
292
|
//if it is allowfirst and hasn't deny rule and has allow rule
|
293
|
//allow access do nothing
|
294
|
|
295
|
}//else if
|
296
|
else
|
297
|
{
|
298
|
//other situation deny access
|
299
|
if (!resultUnaccessableSubTree.containsKey(subTreeId))
|
300
|
{
|
301
|
resultUnaccessableSubTree.put(subTreeId, tree);
|
302
|
}
|
303
|
|
304
|
}//else
|
305
|
}//if isAllowFirst
|
306
|
else //denyFirst
|
307
|
{
|
308
|
if (hasAllowRule(principals, docId, permission,subTreeId))
|
309
|
{
|
310
|
//if it is denyFirst and has allow rule, allow access, do nothing
|
311
|
|
312
|
}
|
313
|
else
|
314
|
{
|
315
|
//if it is denyfirst but no allow rule, deny access
|
316
|
// add into vector
|
317
|
if (!resultUnaccessableSubTree.containsKey(subTreeId))
|
318
|
{
|
319
|
resultUnaccessableSubTree.put(subTreeId, tree);
|
320
|
}
|
321
|
}
|
322
|
}//else denyfirst
|
323
|
}//try
|
324
|
catch( Exception e)
|
325
|
{
|
326
|
MetaCatUtil.debugMessage("error in PermissionControl.has" +
|
327
|
"UnaccessableSubTree "+e.getMessage(), 30);
|
328
|
throw new McdbException(e);
|
329
|
}
|
330
|
}//if
|
331
|
}//for
|
332
|
return resultUnaccessableSubTree;
|
333
|
}//hasUnaccessableSubtree
|
334
|
|
335
|
/**
|
336
|
* Check if a document id is a access document. Access document need user
|
337
|
* has "all" permission to access it.
|
338
|
* @param docId, the document id need to be checked
|
339
|
*/
|
340
|
private boolean isAccessDocument(String docId) throws SQLException
|
341
|
{
|
342
|
//detele the rev number if docid contains it
|
343
|
docId=MetaCatUtil.getDocIdFromString(docId);
|
344
|
PreparedStatement pStmt=null;
|
345
|
DBConnection conn = null;
|
346
|
int serialNumber = -1;
|
347
|
try
|
348
|
{
|
349
|
//check out DBConnection
|
350
|
conn=DBConnectionPool.getDBConnection("PermissionControl.isAccessDoc");
|
351
|
serialNumber=conn.getCheckOutSerialNumber();
|
352
|
pStmt = conn.prepareStatement("select 'x' from xml_access where " +
|
353
|
"accessfileid like '" + docId + "'");
|
354
|
pStmt.execute();
|
355
|
ResultSet rs = pStmt.getResultSet();
|
356
|
boolean hasRow = rs.next();
|
357
|
pStmt.close();
|
358
|
if(hasRow)
|
359
|
{
|
360
|
return true;
|
361
|
}
|
362
|
}
|
363
|
catch(SQLException e)
|
364
|
{
|
365
|
|
366
|
throw new SQLException("PermissionControl.isAccessDocument " +
|
367
|
"Error checking" +
|
368
|
" on document " + docId + ". " + e.getMessage());
|
369
|
}
|
370
|
finally
|
371
|
{
|
372
|
try
|
373
|
{
|
374
|
pStmt.close();
|
375
|
}
|
376
|
finally
|
377
|
{
|
378
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
379
|
}
|
380
|
}
|
381
|
return false;
|
382
|
}//isAccessDocument
|
383
|
|
384
|
|
385
|
|
386
|
/**
|
387
|
* Check if a stirng array contains a given documents' owner
|
388
|
* @param principals, a string array storing the username, groups name and
|
389
|
* public.
|
390
|
* @param docid, the id of given documents
|
391
|
*/
|
392
|
private boolean containDocumentOwner( String[] principals, String docId)
|
393
|
throws SQLException
|
394
|
{
|
395
|
int lengthOfArray=principals.length;
|
396
|
boolean hasRow;
|
397
|
PreparedStatement pStmt=null;
|
398
|
DBConnection conn = null;
|
399
|
int serialNumber = -1;
|
400
|
|
401
|
try
|
402
|
{
|
403
|
//check out DBConnection
|
404
|
conn=DBConnectionPool.getDBConnection("PermissionControl.containDocOnwer");
|
405
|
serialNumber=conn.getCheckOutSerialNumber();
|
406
|
pStmt = conn.prepareStatement(
|
407
|
"SELECT 'x' FROM xml_documents " +
|
408
|
"WHERE docid = ? AND user_owner = ?");
|
409
|
//check every element in the string array too see if it conatains
|
410
|
//the owner of document
|
411
|
for (int i=0; i<lengthOfArray; i++)
|
412
|
{
|
413
|
|
414
|
// Bind the values to the query
|
415
|
pStmt.setString(1, docId);
|
416
|
pStmt.setString(2, principals[i]);
|
417
|
|
418
|
pStmt.execute();
|
419
|
ResultSet rs = pStmt.getResultSet();
|
420
|
hasRow = rs.next();
|
421
|
if (hasRow)
|
422
|
{
|
423
|
pStmt.close();
|
424
|
return true;
|
425
|
}//if
|
426
|
|
427
|
}//for
|
428
|
}//try
|
429
|
catch (SQLException e)
|
430
|
{
|
431
|
pStmt.close();
|
432
|
|
433
|
throw new
|
434
|
SQLException("PermissionControl.hasPermission(). " +
|
435
|
"Error checking ownership for " + principals[0] +
|
436
|
" on document #" + docId + ". " + e.getMessage());
|
437
|
}//catch
|
438
|
finally
|
439
|
{
|
440
|
try
|
441
|
{
|
442
|
pStmt.close();
|
443
|
}
|
444
|
finally
|
445
|
{
|
446
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
447
|
}
|
448
|
}
|
449
|
return false;
|
450
|
}//containDocumentOwner
|
451
|
|
452
|
/**
|
453
|
* Check if the permission order for user at that documents is allowFirst
|
454
|
* @param principals, list of names of principals to check for
|
455
|
* @param docid, document identifier to check for
|
456
|
*/
|
457
|
private boolean isAllowFirst(String [] principals, String docId,
|
458
|
String subTreeId)
|
459
|
throws SQLException, Exception
|
460
|
{
|
461
|
int lengthOfArray=principals.length;
|
462
|
boolean hasRow;
|
463
|
PreparedStatement pStmt = null;
|
464
|
DBConnection conn = null;
|
465
|
int serialNumber = -1;
|
466
|
String sql = null;
|
467
|
boolean topLever =false;
|
468
|
if (subTreeId == null)
|
469
|
{
|
470
|
//top level
|
471
|
topLever = true;
|
472
|
sql = "SELECT perm_order FROM xml_access " +
|
473
|
"WHERE principal_name= ? AND docid = ? AND subtreeid is NULL";
|
474
|
}
|
475
|
else
|
476
|
{
|
477
|
//sub tree level
|
478
|
sql = "SELECT perm_order FROM xml_access " +
|
479
|
"WHERE principal_name= ? AND docid = ? AND subtreeid = ?";
|
480
|
}
|
481
|
|
482
|
try
|
483
|
{
|
484
|
//check out DBConnection
|
485
|
conn=DBConnectionPool.getDBConnection("AccessControlList.isAllowFirst");
|
486
|
serialNumber=conn.getCheckOutSerialNumber();
|
487
|
|
488
|
//select permission order from database
|
489
|
pStmt = conn.prepareStatement(sql);
|
490
|
|
491
|
//check every name in the array
|
492
|
for (int i=0; i<lengthOfArray;i++)
|
493
|
{
|
494
|
//bind value
|
495
|
pStmt.setString(1, principals[i]);//user name
|
496
|
pStmt.setString(2, docId);//docid
|
497
|
|
498
|
// if subtree, we need set subtree id
|
499
|
if (!topLever)
|
500
|
{
|
501
|
pStmt.setString(3, subTreeId);
|
502
|
}
|
503
|
|
504
|
pStmt.execute();
|
505
|
ResultSet rs = pStmt.getResultSet();
|
506
|
hasRow=rs.next();
|
507
|
if (hasRow)
|
508
|
{
|
509
|
//get the permission order from data base
|
510
|
String permissionOrder=rs.getString(1);
|
511
|
//if the permission order is "allowFirst
|
512
|
if (permissionOrder.equalsIgnoreCase(AccessControlInterface.ALLOWFIRST))
|
513
|
{
|
514
|
pStmt.close();
|
515
|
return true;
|
516
|
}
|
517
|
else
|
518
|
{
|
519
|
pStmt.close();
|
520
|
return false;
|
521
|
}
|
522
|
}//if
|
523
|
}//for
|
524
|
}//try
|
525
|
catch (SQLException e)
|
526
|
{
|
527
|
throw e;
|
528
|
}
|
529
|
finally
|
530
|
{
|
531
|
try
|
532
|
{
|
533
|
pStmt.close();
|
534
|
}
|
535
|
finally
|
536
|
{
|
537
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
538
|
}
|
539
|
}
|
540
|
|
541
|
//if reach here, means there is no permssion record for given names and
|
542
|
//docid. So throw a exception.
|
543
|
|
544
|
throw new Exception("There is no permission record for user"+principals[0]+
|
545
|
"at document "+docId);
|
546
|
|
547
|
}//isAllowFirst
|
548
|
|
549
|
/**
|
550
|
* Check if the users array has allow rules for given users, docid and
|
551
|
* permission.
|
552
|
* If it has permission rule and ticket count is greater than 0, the ticket
|
553
|
* number will decrease one for every allow rule
|
554
|
* @param principals, list of names of principals to check for
|
555
|
* @param docid, document identifier to check for
|
556
|
* @param permission, the permssion need to check
|
557
|
*/
|
558
|
private boolean hasAllowRule(String [] principals, String docId,
|
559
|
int permission, String subTreeId)
|
560
|
throws SQLException, Exception
|
561
|
{
|
562
|
int lengthOfArray=principals.length;
|
563
|
boolean allow=false;//initial value is no allow rule
|
564
|
ResultSet rs;
|
565
|
PreparedStatement pStmt = null;
|
566
|
int permissionValue=permission;
|
567
|
int permissionValueInTable;
|
568
|
int ticketCount;
|
569
|
DBConnection conn = null;
|
570
|
int serialNumber = -1;
|
571
|
boolean topLever = false;
|
572
|
String sql = null;
|
573
|
if (subTreeId == null)
|
574
|
{
|
575
|
// for toplevel
|
576
|
topLever = true;
|
577
|
sql = "SELECT permission FROM xml_access WHERE docid = ? " +
|
578
|
"AND principal_name = ? AND perm_type = ? AND subtreeid is NULL";
|
579
|
}
|
580
|
else
|
581
|
{
|
582
|
topLever =false;
|
583
|
sql = "SELECT permission FROM xml_access WHERE docid = ? " +
|
584
|
"AND principal_name = ? AND perm_type = ? AND subtreeid= ?";
|
585
|
}
|
586
|
try
|
587
|
{
|
588
|
//check out DBConnection
|
589
|
conn=DBConnectionPool.getDBConnection("AccessControlList.hasAllowRule");
|
590
|
serialNumber=conn.getCheckOutSerialNumber();
|
591
|
//This sql statement will select entry with
|
592
|
//begin_time<=currentTime<=end_time in xml_access table
|
593
|
//If begin_time or end_time is null in table, isnull(begin_time, sysdate)
|
594
|
//function will assign begin_time=sysdate
|
595
|
pStmt = conn.prepareStatement(sql);
|
596
|
//bind docid, perm_type
|
597
|
pStmt.setString(1, docId);
|
598
|
pStmt.setString(3, AccessControlInterface.ALLOW);
|
599
|
|
600
|
// if subtree lever, need to set subTreeId
|
601
|
if (!topLever)
|
602
|
{
|
603
|
pStmt.setString(4, subTreeId);
|
604
|
}
|
605
|
|
606
|
//bind every elenment in user name array
|
607
|
for (int i=0;i<lengthOfArray; i++)
|
608
|
{
|
609
|
pStmt.setString(2, principals[i]);
|
610
|
pStmt.execute();
|
611
|
rs=pStmt.getResultSet();
|
612
|
while (rs.next())//check every entry for one user
|
613
|
{
|
614
|
permissionValueInTable=rs.getInt(1);
|
615
|
|
616
|
//permission is ok
|
617
|
//the user have a permission to access the file
|
618
|
if (( permissionValueInTable & permissionValue )== permissionValue )
|
619
|
{
|
620
|
allow=true;//has allow rule entry
|
621
|
}//if
|
622
|
}//while
|
623
|
}//for
|
624
|
}//try
|
625
|
catch (SQLException sqlE)
|
626
|
{
|
627
|
throw sqlE;
|
628
|
}
|
629
|
catch (Exception e)
|
630
|
{
|
631
|
throw e;
|
632
|
}
|
633
|
finally
|
634
|
{
|
635
|
try
|
636
|
{
|
637
|
pStmt.close();
|
638
|
}
|
639
|
finally
|
640
|
{
|
641
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
642
|
}
|
643
|
}
|
644
|
return allow;
|
645
|
}//hasAllowRule
|
646
|
|
647
|
|
648
|
|
649
|
/**
|
650
|
* Check if the users array has explicit deny rules for given users, docid
|
651
|
* and permission. That means the perm_type is deny and current time is
|
652
|
* less than end_time and greater than begin time, or no time limit.
|
653
|
* @param principals, list of names of principals to check for
|
654
|
* @param docid, document identifier to check for
|
655
|
* @param permission, the permssion need to check
|
656
|
*/
|
657
|
private boolean hasExplicitDenyRule(String [] principals, String docId,
|
658
|
int permission, String subTreeId)
|
659
|
throws SQLException
|
660
|
{
|
661
|
int lengthOfArray=principals.length;
|
662
|
ResultSet rs;
|
663
|
PreparedStatement pStmt = null;
|
664
|
int permissionValue=permission;
|
665
|
int permissionValueInTable;
|
666
|
DBConnection conn = null;
|
667
|
int serialNumber = -1;
|
668
|
String sql = null;
|
669
|
boolean topLevel = false;
|
670
|
|
671
|
// decide top level or subtree level
|
672
|
if (subTreeId == null)
|
673
|
{
|
674
|
topLevel = true;
|
675
|
sql = "SELECT permission FROM xml_access WHERE docid = ? " +
|
676
|
"AND principal_name = ? AND perm_type = ? AND subtreeid is NULL";
|
677
|
}
|
678
|
else
|
679
|
{
|
680
|
topLevel = false;
|
681
|
sql = "SELECT permission FROM xml_access WHERE docid = ? " +
|
682
|
"AND principal_name = ? AND perm_type = ? AND subtreeid = ?";
|
683
|
}
|
684
|
|
685
|
try
|
686
|
{
|
687
|
//check out DBConnection
|
688
|
conn=DBConnectionPool.getDBConnection("PermissionControl.hasExplicitDeny");
|
689
|
serialNumber=conn.getCheckOutSerialNumber();
|
690
|
|
691
|
pStmt = conn.prepareStatement(sql);
|
692
|
//bind docid, perm_type
|
693
|
pStmt.setString(1, docId);
|
694
|
pStmt.setString(3, AccessControlInterface.DENY);
|
695
|
|
696
|
// subtree level need to set up subtreeid
|
697
|
if (!topLevel)
|
698
|
{
|
699
|
pStmt.setString(4, subTreeId);
|
700
|
}
|
701
|
|
702
|
//bind every elenment in user name array
|
703
|
for (int i=0;i<lengthOfArray; i++)
|
704
|
{
|
705
|
pStmt.setString(2, principals[i]);
|
706
|
pStmt.execute();
|
707
|
rs=pStmt.getResultSet();
|
708
|
while (rs.next())//check every entry for one user
|
709
|
{
|
710
|
permissionValueInTable=rs.getInt(1);
|
711
|
|
712
|
//permission is ok the user doesn't have permission to access the file
|
713
|
if (( permissionValueInTable & permissionValue )== permissionValue )
|
714
|
|
715
|
{
|
716
|
pStmt.close();
|
717
|
return true;
|
718
|
}//if
|
719
|
}//while
|
720
|
}//for
|
721
|
}//try
|
722
|
catch (SQLException e)
|
723
|
{
|
724
|
throw e;
|
725
|
}//catch
|
726
|
finally
|
727
|
{
|
728
|
try
|
729
|
{
|
730
|
pStmt.close();
|
731
|
}
|
732
|
finally
|
733
|
{
|
734
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
735
|
}
|
736
|
}//finally
|
737
|
return false;//no deny rule
|
738
|
}//hasExplicitDenyRule
|
739
|
|
740
|
|
741
|
/**
|
742
|
* Creat a users pakages to check permssion rule, user itself, public and
|
743
|
* the gourps the user belong will be include in this package
|
744
|
* @param user, the name of user
|
745
|
* @param groups, the string array of the groups that user belong to
|
746
|
*/
|
747
|
private String[] createUsersPackage(String user, String [] groups)
|
748
|
{
|
749
|
String [] usersPackage=null;
|
750
|
int lengthOfPackage;
|
751
|
|
752
|
if (groups!=null)
|
753
|
{
|
754
|
//if gouprs is not null and user is not public, we should create a array
|
755
|
//to store the groups and user and public.
|
756
|
//So the length of userPackage is the length of group plus two
|
757
|
if (!user.equalsIgnoreCase(AccessControlInterface.PUBLIC))
|
758
|
{
|
759
|
lengthOfPackage=(groups.length)+2;
|
760
|
usersPackage=new String [lengthOfPackage];
|
761
|
//the first two elements is user self and public
|
762
|
usersPackage[0]=user;
|
763
|
usersPackage[1]=AccessControlInterface.PUBLIC;
|
764
|
//put groups element from index 0 to lengthOfPackage-3 into userPackage
|
765
|
//from index 2 to lengthOfPackage-1
|
766
|
for (int i=2; i<lengthOfPackage; i++)
|
767
|
{
|
768
|
usersPackage[i]=groups[i-2];
|
769
|
} //for
|
770
|
}//if user!=public
|
771
|
else//use=public
|
772
|
{
|
773
|
lengthOfPackage=(groups.length)+1;
|
774
|
usersPackage=new String [lengthOfPackage];
|
775
|
//the first lements is public
|
776
|
usersPackage[0]=AccessControlInterface.PUBLIC;
|
777
|
//put groups element from index 0 to lengthOfPackage-2 into userPackage
|
778
|
//from index 1 to lengthOfPackage-1
|
779
|
for (int i=1; i<lengthOfPackage; i++)
|
780
|
{
|
781
|
usersPackage[i]=groups[i-1];
|
782
|
} //for
|
783
|
}//else user=public
|
784
|
|
785
|
}//if groups!=null
|
786
|
else
|
787
|
{
|
788
|
//because no groups, the userPackage only need two elements
|
789
|
//one is for user, the other is for public
|
790
|
if (!user.equalsIgnoreCase(AccessControlInterface.PUBLIC))
|
791
|
{
|
792
|
lengthOfPackage=2;
|
793
|
usersPackage=new String [lengthOfPackage];
|
794
|
usersPackage[0]=user;
|
795
|
usersPackage[1]=AccessControlInterface.PUBLIC;
|
796
|
}//if user!=public
|
797
|
else //user==public
|
798
|
{
|
799
|
//only put public into array
|
800
|
lengthOfPackage=1;
|
801
|
usersPackage=new String [lengthOfPackage];
|
802
|
usersPackage[0]=AccessControlInterface.PUBLIC;
|
803
|
}
|
804
|
}//else groups==null
|
805
|
return usersPackage;
|
806
|
}//createUsersPackage
|
807
|
|
808
|
/**
|
809
|
* This method will return a data set id for given access id.
|
810
|
* @param accessDocId, the accessDocId which need to be found data set id
|
811
|
*/
|
812
|
private String getDataSetId(String accessDocId)
|
813
|
throws SQLException
|
814
|
{
|
815
|
String dataSetId=null;
|
816
|
PreparedStatement pStmt=null;
|
817
|
ResultSet rs=null;
|
818
|
DBConnection conn=null;
|
819
|
int serialNumber=-1;
|
820
|
String query="SELECT docId from xml_relation where subject = ? or "
|
821
|
+"object = ?";
|
822
|
|
823
|
try
|
824
|
{
|
825
|
//check out DBConnection
|
826
|
conn=DBConnectionPool.getDBConnection("PermissionControl.getDataSetId");
|
827
|
serialNumber=conn.getCheckOutSerialNumber();
|
828
|
|
829
|
pStmt=conn.prepareStatement(query);
|
830
|
//bind the value to query
|
831
|
pStmt.setString(1, accessDocId);
|
832
|
pStmt.setString(2, accessDocId);
|
833
|
//execute the query
|
834
|
pStmt.execute();
|
835
|
rs=pStmt.getResultSet();
|
836
|
//process the result
|
837
|
if (rs.next()) //There are some records for the data set id for access id
|
838
|
{
|
839
|
dataSetId=rs.getString(1);
|
840
|
}
|
841
|
else //No data set id for the given access id in xml_relation table
|
842
|
{
|
843
|
dataSetId=null;
|
844
|
}
|
845
|
}//try
|
846
|
finally
|
847
|
{
|
848
|
try
|
849
|
{
|
850
|
pStmt.close();
|
851
|
}
|
852
|
finally
|
853
|
{
|
854
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
855
|
}
|
856
|
}
|
857
|
return dataSetId;
|
858
|
}//getDataPackageId()
|
859
|
|
860
|
/**
|
861
|
* To create a part of query: "docid like '" +str1+ "', " +"docid like '"
|
862
|
* +str2+"'" ... We need to check user, group and public together for the
|
863
|
* permission. So we need the principal in an array and according the array
|
864
|
* to create a part of query which will be used in other methods
|
865
|
* @param principals, a string array storing the username, groups name and
|
866
|
* public.
|
867
|
*/
|
868
|
private String partQueryAboutDocId( String [] principals)
|
869
|
{
|
870
|
String partQuery="";
|
871
|
int lengthOfArray=principals.length;
|
872
|
|
873
|
for (int i=0;i<(lengthOfArray-1);i++)
|
874
|
{
|
875
|
partQuery=partQuery+"docid like '"+principals[i]+"',";
|
876
|
}
|
877
|
|
878
|
//the last one dosen't has "'"
|
879
|
partQuery=partQuery+"docid like '"+principals[(lengthOfArray-1)]+"'";
|
880
|
return partQuery;
|
881
|
|
882
|
}
|
883
|
}
|