Project

General

Profile

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: leinfelder $'
11
 *     '$Date: 2011-11-17 15:44:32 -0800 (Thu, 17 Nov 2011) $'
12
 * '$Revision: 6675 $'
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.AuthUtil;
47
import edu.ucsb.nceas.metacat.util.DocumentUtil;
48
import edu.ucsb.nceas.metacat.util.MetacatUtil;
49
import edu.ucsb.nceas.metacat.util.SessionData;
50
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
51
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
52
import edu.ucsb.nceas.metacat.shared.ServiceException;
53

    
54
public class PermissionController
55
{
56
   private String docId = null;
57
   private boolean hasSubTreeAccessControl = false; // flag if has a subtree
58
                                                    // access for this docid
59
   private Vector subTreeList = new Vector();
60

    
61
   private static final long TOPLEVELSTARTNODEID = 0; //if start node is 0, means it is top
62
                                         //level document
63

    
64
   private static Logger logMetacat = Logger.getLogger(PermissionController.class);
65

    
66
   /**
67
    * Constructor for PermissionController
68
    * @param myDocid      the docid need to access
69
    */
70
   public PermissionController(String myDocid) throws McdbException
71
   {
72
     // Get rid of rev number
73
     docId = DocumentUtil.getSmartDocId(myDocid);
74
     //hasSubTreeAccessControl = checkSubTreeAccessControl();
75
   }
76

    
77
   /**
78
    * Constructor for PermssionController
79
    * @param myDocid String
80
    * @param needDeleteRev boolean
81
    */
82
   public PermissionController(String myDocid, boolean needDeleteRevFromDocid)
83
   {
84
     if (!needDeleteRevFromDocid)
85
     {
86
       docId = myDocid;
87
     }
88
     else
89
     {
90
         docId = DocumentUtil.getDocIdFromAccessionNumber(myDocid);
91
     }
92
   }
93

    
94
   /**
95
    * Return if a document has subtree access control
96
    */
97
   public boolean hasSubTreeAccessControl()
98
   {
99
     return hasSubTreeAccessControl;
100
   }
101

    
102
   public boolean hasPermission(String sessionId, String myPermission) throws SQLException {
103
       SessionData sessionData = null;
104
       sessionData = SessionService.getInstance().getRegisteredSession(sessionId);
105
       if (sessionData == null) {
106
           return false;
107
       }
108
	   
109
	   return hasPermission(sessionData.getUserName(), sessionData.getGroupNames(), myPermission); 
110
   }
111
   
112

    
113
  /**
114
    * Check from db connection if at least one of the list of @principals
115
    * Administrators are allowed all permission
116
    * @param user  the user name
117
    * @param groups  the groups which the use is in
118
    * @param myPermission  permission type to check for
119
    */
120
  public boolean hasPermission(String user, String[]groups, String myPermission)
121
                              throws SQLException //, Exception
122
  {
123
    boolean hasPermission=false;
124
    String [] userPackage=null;
125
    int permission = AccessControlList.intValue(myPermission);
126

    
127
    //for the command line invocation and replication
128
    if ((user==null) && (groups==null || groups.length==0))
129
    {
130
      return true;
131
    }
132
    
133
    // for administrators
134
    //see http://bugzilla.ecoinformatics.org/show_bug.cgi?id=4728
135
    try {
136
		if (AuthUtil.isAdministrator(user, groups)) {
137
			return true;
138
		}
139
	} catch (MetacatUtilException e) {
140
		// not much we can do here, except treat them as normal
141
		logMetacat.warn("Error checking for administrator: " + e.getMessage(), e);
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

    
150
    if (isAccessDocument(docId) && (permission == AccessControlInterface.WRITE))
151
    {
152

    
153
      hasPermission = hasPermission(userPackage,docId, 7);// 7 is all permission
154
    }//if
155
    else //in other situation, just check the request permission
156
    {
157
      // Check for @permission on @docid for @user and/or @groups
158
      hasPermission = hasPermission(userPackage,docId, permission);
159
    }//else
160

    
161
    return hasPermission;
162
  }
163

    
164

    
165
  /**
166
    * Check from db connection if the users in String array @principals has
167
    * @permission on @docid*
168
    * @param principals, names in userPakcage need to check for @permission
169
    * @param docid, document identifier to check on
170
    * @param permission, permission (write or all...) to check for
171
    */
172
  private boolean hasPermission(String [] principals, String docId,
173
                                           int permission)
174
                         throws SQLException
175
  {
176
    long startId = TOPLEVELSTARTNODEID;// this is for top level, so startid is 0
177
    try
178
    {
179
      //first, if there is a docid owner in user package, return true
180
      //because doc owner has all permssion
181
      if (containDocumentOwner(principals, docId))
182
      {
183

    
184
          return true;
185
      }
186

    
187
      //If there is no owner in user package, checking the table
188
      //check perm_order
189
      if (isAllowFirst(principals, docId, startId))
190
      {
191

    
192
        if (hasExplicitDenyRule(principals, docId, permission, startId))
193
        {
194
          //if it is allowfirst and has deny rule(either explicit )
195
          //deny access
196

    
197
          return false;
198
        }//if
199
        else if ( hasAllowRule(principals, docId, permission, startId))
200
        {
201
          //if it is allowfirst and hasn't deny rule and has allow rule
202
          //allow access
203

    
204
          return true;
205
        }//else if
206
        else
207
        {
208
          //other situation deny access
209

    
210
          return false;
211
        }//else
212
     }//if isAllowFirst
213
     else //denyFirst
214
     {
215
       if (hasAllowRule(principals, docId, permission, startId))
216
       {
217
         //if it is denyFirst and has allow rule, allow access
218
         return true;
219
       }
220
       else
221
       {
222
         //if it is denyfirst but no allow rule, deny access
223
         return false;
224
       }
225
     }//else denyfirst
226
    }//try
227
    catch (Exception e)
228
    {
229
      logMetacat.warn("PermissionController.hasPermission - There is a exception in hasPermission method: "
230
                         +e.getMessage());
231
    }
232

    
233
    return false;
234
  }//hasPermission
235

    
236
  /**
237
   *  Method to check if a person has permission to a inline data file
238
   * @param user String
239
   * @param groups String[]
240
   * @param myPermission String
241
   * @param inlineDataId String
242
   * @throws McdbException
243
   * @return boolean
244
   */
245
  private boolean hasPermissionForInlineData(String user, String[] groups,
246
                                      String myPermission, String inlineDataId)
247
      throws McdbException
248
  {
249
     // this method can call the public method - hasPermission(...)
250
     // the only difference is about the ownership, you couldn't find the owner
251
     // from inlinedataId directly. You should get it from eml document itself
252
     String []userPackage = createUsersPackage(user, groups);
253
     try {
254
        if (containDocumentOwner(userPackage, docId))
255
         {
256
           return true;
257
         }
258
         else
259
         {
260
             PermissionController controller =
261
                                   new PermissionController(inlineDataId, false);
262
             return controller.hasPermission(user, groups, myPermission);
263
         }
264
    } catch (SQLException e) {
265
        throw new McdbException(e.getMessage());
266
    }
267
  }
268

    
269
  /**
270
   * The method to determine of a node can be access by a user just by subtree
271
   * access control
272
   */
273
  public boolean hasPermissionForSubTreeNode(String user, String[] groups,
274
                                             String myPermission, long nodeId)
275
                                             throws McdbException
276
  {
277
    boolean flag = true;
278
    // Get unaccessble subtree for this user
279
    Hashtable unaccessableSubTree = hasUnaccessableSubTree(user, groups,
280
                                                           myPermission);
281
    Enumeration en = unaccessableSubTree.elements();
282
    while (en.hasMoreElements())
283
    {
284
      SubTree tree = (SubTree)en.nextElement();
285
      long start = tree.getStartNodeId();
286
      long stop  = tree.getEndNodeId();
287
      // nodeid in unaccessablesubtree, return false
288
      if ( nodeId >= start && nodeId <= stop)
289
      {
290
        flag = false;
291
        break;
292
      }
293
    }
294
    return flag;
295
  }
296
  /**
297
   * This method will return a hasTable of subtree which user doesn't has the
298
   * permssion to access
299
   * @param user  the user name
300
   * @param groups  the groups which the use is in
301
   * @param myPermission  permission type to check for
302
   */
303
  public Hashtable hasUnaccessableSubTree(String user, String[] groups,
304
                                       String myPermission) throws McdbException
305
  {
306
    Hashtable resultUnaccessableSubTree = new Hashtable();
307
    String [] principals=null;
308
    int permission =AccessControlList.intValue(myPermission);
309

    
310
    //for the commnad line invocation return null(no unaccessable subtree)
311
    if ((user==null) && (groups==null || groups.length==0))
312
    {
313
      return resultUnaccessableSubTree;
314
    }
315

    
316
    //create a userpackage including user, public and group member
317
    principals=createUsersPackage(user, groups);
318
    //for the document owner return null(no unaccessable subtree)
319
    try
320
    {
321
      if (containDocumentOwner(principals, docId))
322
      {
323
       return resultUnaccessableSubTree;
324
      }
325
    }
326
    catch (SQLException ee)
327
    {
328
      throw new McdbException(ee);
329
    }
330

    
331
    // go through every subtree which has access control
332
    for (int i = 0; i< subTreeList.size(); i++)
333
    {
334
      SubTree tree = (SubTree)subTreeList.elementAt(i);
335
      long startId = tree.getStartNodeId();
336

    
337

    
338
        try
339
        {
340
          if (isAllowFirst(principals, docId, startId))
341
          {
342

    
343
            if (hasExplicitDenyRule(principals, docId, permission, startId ))
344
            {
345

    
346
             //if it is allowfirst and has deny rule
347
              // put the subtree into unaccessable vector
348
              if (!resultUnaccessableSubTree.containsKey(new Long(startId)))
349
              {
350
                resultUnaccessableSubTree.put(new Long(startId), tree);
351
              }
352
            }//if
353
            else if ( hasAllowRule(principals, docId, permission, startId))
354
            {
355
              //if it is allowfirst and hasn't deny rule and has allow rule
356
              //allow access do nothing
357

    
358
            }//else if
359
            else
360
            {
361
              //other situation deny access
362
              if (!resultUnaccessableSubTree.containsKey(new Long(startId)))
363
              {
364
                resultUnaccessableSubTree.put(new Long(startId), tree);
365
              }
366

    
367
            }//else
368
          }//if isAllowFirst
369
          else //denyFirst
370
          {
371
            if (hasAllowRule(principals, docId, permission,startId))
372
            {
373
              //if it is denyFirst and has allow rule, allow access, do nothing
374

    
375
            }
376
            else
377
            {
378
              //if it is denyfirst but no allow rule, deny access
379
              // add into vector
380
              if (!resultUnaccessableSubTree.containsKey(new Long(startId)))
381
              {
382
                resultUnaccessableSubTree.put(new Long(startId), tree);
383
              }
384
            }
385
          }//else denyfirst
386
        }//try
387
        catch( Exception e)
388
        {
389
          logMetacat.error("PermissionController.hasUnaccessableSubTree - error in PermissionControl.has" +
390
                                   "UnaccessableSubTree "+e.getMessage());
391
          throw new McdbException(e);
392
        }
393

    
394
    }//for
395
    // merge the subtree if a subtree is another subtree'subtree
396
    resultUnaccessableSubTree = mergeEquivalentSubtree(resultUnaccessableSubTree);
397
    return resultUnaccessableSubTree;
398
  }//hasUnaccessableSubtree
399

    
400

    
401
  /*
402
   * A method to merge nested subtree into bigger one. For example subtree b
403
   * is a subtree of subtree a. And user doesn't have read permission for both
404
   * so we only use subtree a is enough.
405
   */
406
  private Hashtable mergeEquivalentSubtree(Hashtable unAccessSubTree)
407
  {
408
    Hashtable newSubTreeHash = new Hashtable();
409
    boolean   needDelete = false;
410
    // check the parameters
411
    if (unAccessSubTree == null || unAccessSubTree.isEmpty())
412
    {
413
      return newSubTreeHash;
414
    }
415
    else
416
    {
417
      // look every subtree start point and stop point, to see if it is embedded
418
      // in another one. If embedded, they are equavelent and we can use bigger
419
      // one to replace smaller one
420
      Enumeration en = unAccessSubTree.elements();
421
      while (en.hasMoreElements())
422
      {
423
        SubTree tree    = (SubTree)en.nextElement();
424
        String  treeId  = tree.getSubTreeId();
425
        long    startId = tree.getStartNodeId();
426
        long    endId   = tree.getEndNodeId();
427

    
428
        Enumeration enu = unAccessSubTree.elements();
429
        while (enu.hasMoreElements())
430
        {
431
          SubTree subTree = (SubTree)enu.nextElement();
432
          String subTreeId= subTree.getSubTreeId();
433
          long   subTreeStartId = subTree.getStartNodeId();
434
          long   subTreeEndId   = subTree.getEndNodeId();
435
          //compare and if the first subtree is a subtree of the second
436
          // one, set neeDelete true
437
          if (startId > subTreeStartId && endId < subTreeEndId)
438
          {
439
            needDelete = true;
440
            logMetacat.info("PermissionController.mergeEquivalentSubtree - the subtree: "+ treeId +
441
                                     " need to be get rid of from unaccessable"+
442
                                     " subtree list becuase it is a subtree of"+
443
                                     " another subtree in the list");
444
            break;
445
          }//if
446
        }//while
447
        // if not need to delete, put the subtree into hash
448
        if (!needDelete)
449
        {
450
          newSubTreeHash.put(new Long(startId), tree);
451
        }
452
        //reset needDelete
453
        needDelete = false;
454
      }//while
455
      return newSubTreeHash;
456
    }//else
457
  }
458

    
459
  /**
460
	 * Check if a document id is a access document. Access document need user
461
	 * has "all" permission to access it.
462
	 * 
463
	 * @param docId,
464
	 *            the document id need to be checked
465
	 */
466
	private boolean isAccessDocument(String docId) throws SQLException {
467
		// detele the rev number if docid contains it
468
		docId = DocumentUtil.getDocIdFromString(docId);
469
		PreparedStatement pStmt = null;
470
		DBConnection conn = null;
471
		int serialNumber = -1;
472
		try {
473
			// check out DBConnection
474
			conn = DBConnectionPool.getDBConnection("PermissionControl.isAccessDoc");
475
			serialNumber = conn.getCheckOutSerialNumber();
476
			pStmt = conn.prepareStatement("select doctype from xml_documents where docid like ? ");
477
			pStmt.setString(1, docId);
478
			pStmt.execute();
479
			ResultSet rs = pStmt.getResultSet();
480
			boolean hasRow = rs.next();
481
			String doctype = null;
482
			if (hasRow) {
483
				doctype = rs.getString(1);
484

    
485
			}
486
			pStmt.close();
487

    
488
			// if it is an access document
489
			if (doctype != null
490
					&& ((MetacatUtil.getOptionList(PropertyService
491
							.getProperty("xml.accessdoctype")).contains(doctype)))) {
492

    
493
				return true;
494
			}
495

    
496
		} catch (SQLException e) {
497
			throw new SQLException("PermissionControl.isAccessDocument "
498
					+ "Error checking" + " on document " + docId + ". " + e.getMessage());
499
		} catch (PropertyNotFoundException pnfe) {
500
			throw new SQLException("PermissionControl.isAccessDocument "
501
					+ "Error checking" + " on document " + docId + ". " + pnfe.getMessage());
502
		} finally {
503
			try {
504
				pStmt.close();
505
			} finally {
506
				DBConnectionPool.returnDBConnection(conn, serialNumber);
507
			}
508
		}
509

    
510
		return false;
511
	}// isAccessDocument
512

    
513

    
514

    
515
  /**
516
	 * Check if a stirng array contains a given documents' owner
517
	 * 
518
	 * @param principals,
519
	 *            a string array storing the username, groups name and public.
520
	 * @param docid,
521
	 *            the id of given documents
522
	 */
523
  private boolean containDocumentOwner( String[] principals, String docId)
524
                    throws SQLException
525
  {
526
    int lengthOfArray=principals.length;
527
    boolean hasRow;
528
    PreparedStatement pStmt=null;
529
    DBConnection conn = null;
530
    int serialNumber = -1;
531

    
532
    try
533
    {
534
      //check out DBConnection
535
     conn=DBConnectionPool.getDBConnection("PermissionControl.containDocOnwer");
536
      serialNumber=conn.getCheckOutSerialNumber();
537
      pStmt = conn.prepareStatement(
538
                "SELECT 'x' FROM xml_documents " +
539
                "WHERE docid = ? AND lower(user_owner) = ? " +
540
                "UNION ALL " +
541
                "SELECT 'x' FROM xml_revisions " +
542
                "WHERE docid = ? AND lower(user_owner) = ? ");
543
      //check every element in the string array too see if it conatains
544
      //the owner of document
545
      for (int i=0; i<lengthOfArray; i++)
546
      {
547

    
548
        // Bind the values to the query
549
        pStmt.setString(1, docId);
550
        pStmt.setString(2, principals[i]);
551
        pStmt.setString(3, docId);
552
        pStmt.setString(4, principals[i]);
553
        logMetacat.info("PermissionController.containDocumentOwner - the principle stack is : " +
554
                                  principals[i]);
555

    
556
        pStmt.execute();
557
        ResultSet rs = pStmt.getResultSet();
558
        hasRow = rs.next();
559
        if (hasRow)
560
        {
561
          pStmt.close();
562
           logMetacat.info("PermissionController.containDocumentOwner - find the owner");
563
          return true;
564
        }//if
565

    
566
      }//for
567
    }//try
568
    catch (SQLException e)
569
    {
570
        pStmt.close();
571

    
572
        throw new
573
        SQLException("PermissionControl.hasPermission - " +
574
                     "Error checking ownership for " + principals[0] +
575
                     " on document #" + docId + ". " + e.getMessage());
576
    }//catch
577
    finally
578
    {
579
      try
580
      {
581
        pStmt.close();
582
      }
583
      finally
584
      {
585
        DBConnectionPool.returnDBConnection(conn, serialNumber);
586
      }
587
    }
588
    return false;
589
  }//containDocumentOwner
590

    
591
  /**
592
    * Check if the permission order for user at that documents is allowFirst
593
    * @param principals, list of names of principals to check for
594
    * @param docid, document identifier to check for
595
    */
596
  private boolean isAllowFirst(String [] principals, String docId,
597
                               long startId)
598
                  throws SQLException, Exception
599
  {
600
    int lengthOfArray=principals.length;
601
    boolean hasRow;
602
    PreparedStatement pStmt = null;
603
    DBConnection conn = null;
604
    int serialNumber = -1;
605
    String sql = null;
606
    boolean topLever =false;
607
    if (startId == TOPLEVELSTARTNODEID)
608
    {
609
      //top level
610
      topLever = true;
611
      sql = "SELECT perm_order FROM xml_access " +
612
    "WHERE lower(principal_name) = ? AND docid = ? AND startnodeid is NULL";
613
    }
614
    else
615
    {
616
      //sub tree level
617
      sql = "SELECT perm_order FROM xml_access " +
618
        "WHERE lower(principal_name)= ? AND docid = ? AND startnodeid = ?";
619
    }
620

    
621
    try
622
    {
623
      //check out DBConnection
624
      conn=DBConnectionPool.getDBConnection("AccessControlList.isAllowFirst");
625
      serialNumber=conn.getCheckOutSerialNumber();
626

    
627
      //select permission order from database
628
      pStmt = conn.prepareStatement(sql);
629

    
630
      //check every name in the array
631
      for (int i=0; i<lengthOfArray;i++)
632
      {
633
        //bind value
634
        pStmt.setString(1, principals[i]);//user name
635
        pStmt.setString(2, docId);//docid
636

    
637
        // if subtree, we need set subtree id
638
        if (!topLever)
639
        {
640
          pStmt.setLong(3, startId);
641
        }
642

    
643
        pStmt.execute();
644
        ResultSet rs = pStmt.getResultSet();
645
        hasRow=rs.next();
646
        if (hasRow)
647
        {
648
          //get the permission order from data base
649
          String permissionOrder=rs.getString(1);
650
          //if the permission order is "allowFirst
651
          if (permissionOrder.equalsIgnoreCase(AccessControlInterface.ALLOWFIRST))
652
          {
653
            pStmt.close();
654
            return true;
655
          }
656
          else
657
          {
658
            pStmt.close();
659
            return false;
660
          }
661
        }//if
662
      }//for
663
    }//try
664
    catch (SQLException e)
665
    {
666
      throw e;
667
    }
668
    finally
669
    {
670
      try
671
      {
672
        pStmt.close();
673
      }
674
      finally
675
      {
676
        DBConnectionPool.returnDBConnection(conn, serialNumber);
677
      }
678
    }
679

    
680
    //if reach here, means there is no permssion record for given names and
681
    //docid. So throw a exception.
682

    
683
    throw new Exception("PermissionController.isAllowFirst - There is no permission record for user "+ principals[0] + 
684
                        " at document " + docId);
685

    
686
  }//isAllowFirst
687

    
688
  /**
689
    * Check if the users array has allow rules for given users, docid and
690
    * permission.
691
    * If it has permission rule and ticket count is greater than 0, the ticket
692
    * number will decrease one for every allow rule
693
    * @param principals, list of names of principals to check for
694
    * @param docid, document identifier to check for
695
    * @param permission, the permssion need to check
696
    */
697
  private boolean hasAllowRule(String [] principals, String docId,
698
                                  int permission, long startId)
699
                  throws SQLException, Exception
700
 {
701
   int lengthOfArray=principals.length;
702
   boolean allow=false;//initial value is no allow rule
703
   ResultSet rs;
704
   PreparedStatement pStmt = null;
705
   int permissionValue=permission;
706
   int permissionValueInTable;
707
   int ticketCount;
708
   DBConnection conn = null;
709
   int serialNumber = -1;
710
   boolean topLever = false;
711
   String sql = null;
712
   if (startId == TOPLEVELSTARTNODEID)
713
   {
714
     // for toplevel
715
     topLever = true;
716
     sql = "SELECT permission FROM xml_access WHERE docid = ? " +
717
   "AND lower(principal_name) = ? AND perm_type = ? AND startnodeid is NULL";
718
   }
719
   else
720
   {
721
     topLever =false;
722
     sql = "SELECT permission FROM xml_access WHERE docid = ? " +
723
      "AND lower(principal_name) = ? AND perm_type = ? AND startnodeid = ?";
724
   }
725
   try
726
   {
727
     //check out DBConnection
728
     conn=DBConnectionPool.getDBConnection("AccessControlList.hasAllowRule");
729
     serialNumber=conn.getCheckOutSerialNumber();
730
    //This sql statement will select entry with
731
    //begin_time<=currentTime<=end_time in xml_access table
732
    //If begin_time or end_time is null in table, isnull(begin_time, sysdate)
733
    //function will assign begin_time=sysdate
734
    pStmt = conn.prepareStatement(sql);
735
    //bind docid, perm_type
736
    pStmt.setString(1, docId);
737
    pStmt.setString(3, AccessControlInterface.ALLOW);
738

    
739
    // if subtree lever, need to set subTreeId
740
    if (!topLever)
741
    {
742
      pStmt.setLong(4, startId);
743
    }
744

    
745
    //bind every elenment in user name array
746
    for (int i=0;i<lengthOfArray; i++)
747
    {
748
      pStmt.setString(2, principals[i]);
749
      pStmt.execute();
750
      rs=pStmt.getResultSet();
751
      while (rs.next())//check every entry for one user
752
      {
753
        permissionValueInTable=rs.getInt(1);
754

    
755
        //permission is ok
756
        //the user have a permission to access the file
757
        if (( permissionValueInTable & permissionValue )== permissionValue )
758
        {
759

    
760
           allow=true;//has allow rule entry
761
        }//if
762
      }//while
763
    }//for
764
   }//try
765
   catch (SQLException sqlE)
766
   {
767
     throw sqlE;
768
   }
769
   catch (Exception e)
770
   {
771
     throw e;
772
   }
773
   finally
774
   {
775
     try
776
     {
777
       pStmt.close();
778
     }
779
     finally
780
     {
781
       DBConnectionPool.returnDBConnection(conn, serialNumber);
782
     }
783
   }
784
    return allow;
785
 }//hasAllowRule
786

    
787

    
788

    
789
   /**
790
    * Check if the users array has explicit deny rules for given users, docid
791
    * and permission. That means the perm_type is deny and current time is
792
    * less than end_time and greater than begin time, or no time limit.
793
    * @param principals, list of names of principals to check for
794
    * @param docid, document identifier to check for
795
    * @param permission, the permssion need to check
796
    */
797
  private boolean hasExplicitDenyRule(String [] principals, String docId,
798
                                      int permission, long startId)
799
                  throws SQLException
800
 {
801
   int lengthOfArray=principals.length;
802
   ResultSet rs;
803
   PreparedStatement pStmt = null;
804
   int permissionValue=permission;
805
   int permissionValueInTable;
806
   DBConnection conn = null;
807
   int serialNumber = -1;
808
   String sql = null;
809
   boolean topLevel = false;
810

    
811
   // decide top level or subtree level
812
   if (startId == TOPLEVELSTARTNODEID)
813
   {
814
     topLevel = true;
815
     sql = "SELECT permission FROM xml_access WHERE docid = ? " +
816
    "AND lower(principal_name) = ? AND perm_type = ? AND startnodeid is NULL";
817
   }
818
   else
819
   {
820
     topLevel = false;
821
     sql = "SELECT permission FROM xml_access WHERE docid = ? " +
822
     "AND lower(principal_name) = ? AND perm_type = ? AND startnodeid = ?";
823
   }
824

    
825
   try
826
   {
827
     //check out DBConnection
828
     conn=DBConnectionPool.getDBConnection("PermissionControl.hasExplicitDeny");
829
     serialNumber=conn.getCheckOutSerialNumber();
830

    
831
     pStmt = conn.prepareStatement(sql);
832
    //bind docid, perm_type
833
    pStmt.setString(1, docId);
834
    pStmt.setString(3, AccessControlInterface.DENY);
835

    
836
    // subtree level need to set up subtreeid
837
    if (!topLevel)
838
    {
839
      pStmt.setLong(4, startId);
840
    }
841

    
842
    //bind every elenment in user name array
843
    for (int i=0;i<lengthOfArray; i++)
844
    {
845
      pStmt.setString(2, principals[i]);
846
      pStmt.execute();
847
      rs=pStmt.getResultSet();
848
      while (rs.next())//check every entry for one user
849
      {
850
        permissionValueInTable=rs.getInt(1);
851

    
852
        //permission is ok the user doesn't have permission to access the file
853
        if (( permissionValueInTable & permissionValue )== permissionValue )
854

    
855
        {
856
           pStmt.close();
857
           return true;
858
         }//if
859
      }//while
860
    }//for
861
   }//try
862
   catch (SQLException e)
863
   {
864
     throw e;
865
   }//catch
866
   finally
867
   {
868
     try
869
     {
870
       pStmt.close();
871
     }
872
     finally
873
     {
874
       DBConnectionPool.returnDBConnection(conn, serialNumber);
875
     }
876
   }//finally
877
   return false;//no deny rule
878
  }//hasExplicitDenyRule
879

    
880

    
881
  /**
882
    * Creat a users pakages to check permssion rule, user itself, public and
883
    * the gourps the user belong will be include in this package
884
    * @param user, the name of user
885
    * @param groups, the string array of the groups that user belong to
886
    */
887
  private String[] createUsersPackage(String user, String [] groups)
888
  {
889
    String [] usersPackage=null;
890
    int lengthOfPackage;
891

    
892
    if (groups!=null)
893
    {
894
      //if gouprs is not null and user is not public, we should create a array
895
      //to store the groups and user and public.
896
      //So the length of userPackage is the length of group plus two
897
      if (!user.equalsIgnoreCase(AccessControlInterface.PUBLIC))
898
      {
899
        lengthOfPackage=(groups.length)+2;
900
        usersPackage=new String [lengthOfPackage];
901
        //the first two elements is user self and public
902
        //in order to ignore case sensitive, we transfer user to lower case
903
        if (user != null)
904
        {
905
          usersPackage[0]= user.toLowerCase();
906
          logMetacat.info("PermissionController.createUsersPackage - after transfer to lower case(not null): "+
907
                                     usersPackage[0]);
908
        }
909
        else
910
        {
911
          usersPackage[0] = user;
912
          usersPackage[0]= user.toLowerCase();
913
          logMetacat.info("PermissionController.createUsersPackage - after transfer to lower case(null): "+
914
                                     usersPackage[0]);
915
        }
916
        usersPackage[1]=AccessControlInterface.PUBLIC;
917
        //put groups element from index 0 to lengthOfPackage-3 into userPackage
918
        //from index 2 to lengthOfPackage-1
919
        for (int i=2; i<lengthOfPackage; i++)
920
        {
921
          //tansfer group to lower case too
922
          if (groups[i-2] != null)
923
          {
924
            usersPackage[i]=groups[i-2].toLowerCase();
925
          }
926
        } //for
927
      }//if user!=public
928
      else//use=public
929
      {
930
        lengthOfPackage=(groups.length)+1;
931
        usersPackage=new String [lengthOfPackage];
932
        //the first lements is public
933
        usersPackage[0]=AccessControlInterface.PUBLIC;
934
        //put groups element from index 0 to lengthOfPackage-2 into userPackage
935
        //from index 1 to lengthOfPackage-1
936
        for (int i=1; i<lengthOfPackage; i++)
937
        {
938
          if (groups[i-1] != null)
939
          {
940
            usersPackage[i]=groups[i-1].toLowerCase();
941
          }
942
        } //for
943
      }//else user=public
944

    
945
    }//if groups!=null
946
    else
947
    {
948
      //because no groups, the userPackage only need two elements
949
      //one is for user, the other is for public
950
      if (!user.equalsIgnoreCase(AccessControlInterface.PUBLIC))
951
      {
952
        lengthOfPackage=2;
953
        usersPackage=new String [lengthOfPackage];
954
        if (user != null)
955
        {
956
          usersPackage[0]=user.toLowerCase();
957
        }
958
        else
959
        {
960
          usersPackage[0]=user;
961
        }
962
        usersPackage[1]=AccessControlInterface.PUBLIC;
963
      }//if user!=public
964
      else //user==public
965
      {
966
        //only put public into array
967
        lengthOfPackage=1;
968
        usersPackage=new String [lengthOfPackage];
969
        usersPackage[0]=AccessControlInterface.PUBLIC;
970
      }
971
    }//else groups==null
972
    return usersPackage;
973
  }//createUsersPackage
974

    
975

    
976
  /**
977
   * A static method to get Hashtable which cointains a inlinedata object list that
978
   * user can't read it. The key is subtree id of inlinedata, the data is
979
   * 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> getUnReadableInlineDataIdList(String docidWithoutRev,
987
                                                   String user, String[] groups,
988
                                                   boolean withRevision)
989
                                                throws McdbException
990
   {
991
     Hashtable<String, String> inlineDataList = getUnAccessableInlineDataIdList(docidWithoutRev,
992
                              user, groups, AccessControlInterface.READSTRING,
993
                              withRevision);
994

    
995
     return inlineDataList;
996
   }
997

    
998
   /**
999
  * A static method to get Hashtable which cointains a inline  data object list that
1000
  * user can't overwrite it. The key is subtree id of inline data distrubition,
1001
  * the value is internal file name for the inline data which is stored as docid
1002
  * in xml_access table or data object doc id.
1003
  * @param docidWithoutRev, metadata docid which should be the accessfileid
1004
  *                         in the table
1005
  * @param user , the name of user
1006
  * @param groups, the group which the user belong to
1007
  */
1008
  public static Hashtable<String, String> getUnWritableInlineDataIdList(String docidWithoutRev,
1009
                                                  String user, String[] groups,
1010
                                                  boolean withRevision)
1011
                                               throws Exception
1012
  {
1013
    Hashtable<String, String> inlineDataList = getUnAccessableInlineDataIdList(docidWithoutRev,
1014
                            user, groups, AccessControlInterface.WRITESTRING,
1015
                            withRevision);
1016

    
1017
    return inlineDataList;
1018
  }
1019

    
1020

    
1021
   /*
1022
    * This method will get hashtable which contains a unaccessable distribution
1023
    * inlinedata object list
1024
    *
1025
    * withRevision is used to get inline id list with or without revision number
1026
    * e.g. when withRevision is true, temp.1.1.1, temp.1.1.2 would be returned
1027
    * otherwise temp.1.1 and temp.1.2 would be returned.
1028
    */
1029
   private static Hashtable<String, String> getUnAccessableInlineDataIdList(String docid,
1030
                               String user, String[] groups, String permission,
1031
                               boolean withRevision)
1032
                             throws McdbException
1033
   {
1034
       Hashtable<String, String> unAccessibleIdList = new Hashtable();
1035
       if (user == null) {
1036
           return unAccessibleIdList;
1037
       }
1038

    
1039
       Hashtable allIdList;
1040
       try {
1041
           allIdList = getAllInlineDataIdList(docid);
1042
       } catch (SQLException e) {
1043
           throw new McdbException(e.getMessage());
1044
       }
1045
       Enumeration<String> en = allIdList.keys();
1046
      while (en.hasMoreElements())
1047
      {
1048
        String subTreeId = (String) en.nextElement();
1049
        String fileId = (String) allIdList.get(subTreeId);
1050
        //Here fileid is internal file id for line data. It stored in docid
1051
        // field in xml_access table. so we don't need to delete rev
1052
        PermissionController controller = new PermissionController(docid, false);
1053
        if (!controller.hasPermissionForInlineData(user, groups, permission, fileId))
1054
        {
1055
            if(withRevision)
1056
            {
1057
                logMetacat.info("PermissionController.getUnAccessableInlineDataIdList - Put subtree id " + subTreeId +
1058
                                         " and " + "inline data file name " +
1059
                                         fileId + " into " + "un" + permission +
1060
                                         " hash");
1061
                unAccessibleIdList.put(subTreeId, fileId);
1062

    
1063
            }
1064
            else
1065
            {
1066
                logMetacat.info("PermissionController.getUnAccessableInlineDataIdList - Put subtree id " + subTreeId +
1067
                                         " and " + "inline data file name " +
1068
                                         DocumentUtil.getInlineDataIdWithoutRev(fileId) +
1069
                                         " into " + "un" + permission +
1070
                                         " hash");
1071
                unAccessibleIdList.put(subTreeId, 
1072
                		DocumentUtil.getInlineDataIdWithoutRev(fileId));
1073
            }
1074
        }
1075
      }
1076
      return unAccessibleIdList;
1077
   }
1078

    
1079

    
1080
   /*
1081
    * This method will get a hash table from xml_access table for all records
1082
    * about the inline data. The key is subtree id and data is a inline internal
1083
    * file name
1084
    */
1085
   private static Hashtable getAllInlineDataIdList(String docid) throws SQLException
1086
   {
1087
     Hashtable inlineDataList = new Hashtable();
1088
     String sql = "SELECT subtreeid, docid FROM xml_access WHERE " +
1089
                   "accessfileid = ? AND subtreeid  IS NOT NULL";
1090
     PreparedStatement pStmt=null;
1091
     ResultSet rs=null;
1092
     DBConnection conn=null;
1093
     int serialNumber=-1;
1094
     try
1095
     {
1096
       //check out DBConnection
1097
       conn=DBConnectionPool.getDBConnection("PermissionControl.getDataSetId");
1098
       serialNumber=conn.getCheckOutSerialNumber();
1099
       pStmt=conn.prepareStatement(sql);
1100
       //bind the value to query
1101
       pStmt.setString(1, docid);
1102
       //execute the query
1103
       pStmt.execute();
1104
       rs=pStmt.getResultSet();
1105
       //process the result
1106
       while(rs.next())
1107
       {
1108
         String subTreeId = rs.getString(1);
1109
         String inlineDataId = rs.getString(2);
1110
         if (subTreeId != null && !subTreeId.trim().equals("") &&
1111
            inlineDataId != null && !inlineDataId.trim().equals(""))
1112
         {
1113
           inlineDataList.put(subTreeId, inlineDataId);
1114
         }
1115
      }//while
1116
     }//try
1117
     finally
1118
     {
1119
       try
1120
       {
1121
         pStmt.close();
1122
       }
1123
       finally
1124
       {
1125
         DBConnectionPool.returnDBConnection(conn, serialNumber);
1126
       }
1127
     }//finally
1128
     return inlineDataList;
1129
   }//getAllInlineDataIdList
1130
}
(52-52/65)