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
 *    Release: @release@
10
 *
11
 *   '$Author: tao $'
12
 *     '$Date: 2003-03-18 17:32:18 -0800 (Tue, 18 Mar 2003) $'
13
 * '$Revision: 1485 $'
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.Enumeration;
33
import java.util.Hashtable;
34
import java.util.Stack;
35
import java.util.Vector;
36

    
37
public class PermissionController
38
{
39
   private String docId = null;
40
   private boolean hasSubTreeAccessControl = false; // flag if has a subtree
41
                                                    // access for this docid
42
   private Vector subTreeList = new Vector();
43
   
44
   
45
   /**
46
    * Constructor for PermissionController
47
    * @param myDocid      the docid need to access
48
    */
49
   public PermissionController(String myDocid) throws McdbException
50
   {
51
     // Get rid of rev number
52
     docId = MetaCatUtil.getDocIdFromString(myDocid);
53
     hasSubTreeAccessControl = checkSubTreeAccessControl();
54
   }
55
   
56
   /**
57
    * Return if a document has subtree access control
58
    */
59
   public boolean hasSubTreeAccessControl()
60
   {
61
     return hasSubTreeAccessControl;
62
   }
63
   
64
   /*
65
    * Go through the access table and find if has subtree access control
66
    * if has, store the subtree into list and return true. Otherwise, 
67
    * return false
68
    */
69
  private boolean checkSubTreeAccessControl() throws McdbException
70
  {
71
    boolean flag = false;
72
    PreparedStatement pStmt=null;
73
    ResultSet rs=null;
74
    DBConnection conn=null;
75
    int serialNumber=-1;
76
    String query="SELECT subtreeid, startnodeid, endnodeid from xml_access " +
77
                 "where docid =? ";
78
    
79
    try
80
    {
81
      //check out DBConnection
82
      conn=DBConnectionPool.getDBConnection("PermissionController.hasSubTreeA");
83
      serialNumber=conn.getCheckOutSerialNumber();
84
      
85
      pStmt=conn.prepareStatement(query);
86
      //bind the value to query
87
      pStmt.setString(1, docId);
88
      //execute the query
89
      pStmt.execute();
90
      rs=pStmt.getResultSet();
91
      //process the result
92
      while (rs.next()) //
93
      {
94
        String subTreeId = rs.getString(1);
95
        MetaCatUtil.debugMessage("subtreeid: "+subTreeId, 35);
96
        long startNodeId = rs.getLong(2);
97
        MetaCatUtil.debugMessage("subtree startNodeId: "+startNodeId, 35);
98
        long endNodeId = rs.getLong(3);
99
        MetaCatUtil.debugMessage("subtree endNodeId: "+endNodeId, 35);
100
        // if startnodeid field in table is empty,startNodeId will be 0
101
        if (subTreeId != null && startNodeId != 0 && startNodeId != 0)
102
        {
103
          flag = true;// has subtree control
104
          SubTree tree = new SubTree();
105
          tree.setSubTreeId(subTreeId);
106
          tree.setStartNodeId(startNodeId);
107
          tree.setEndNodeId(endNodeId);
108
          subTreeList.add(tree);
109
         }
110
      }
111
    }//try
112
    catch (SQLException e)
113
    {
114
      throw new McdbException(e);
115
    }
116
    finally
117
    {
118
      try
119
      {
120
        pStmt.close();
121
      }
122
      catch(SQLException ee)
123
      {
124
        throw new McdbException(ee);
125
      }
126
      finally
127
      {
128
        DBConnectionPool.returnDBConnection(conn, serialNumber);
129
      }
130
    }
131
    return flag;
132
  }
133
   
134
  /**
135
    * Check from db connection if at least one of the list of @principals
136
    * @param user  the user name
137
    * @param groups  the groups which the use is in
138
    * @param myPermission  permission type to check for
139
    */
140
  public boolean hasPermission(String user, String[]groups, String myPermission) 
141
                              throws SQLException, Exception
142
  {
143
    boolean hasPermission=false;
144
    String [] userPackage=null;
145
    int permission =AccessControlList.intValue(myPermission);
146
       
147
    //for the commnad line invocation
148
    if ((user==null) && (groups==null || groups.length==0))
149
    {
150
      return true;
151
    }
152
   
153
    //create a userpackage including user, public and group member
154
    userPackage=createUsersPackage(user, groups);
155
    
156
    //if the requested document is access documents and requested permission
157
    //is "write", the user should have "all" right
158
    if (isAccessDocument(docId) && (permission == AccessControlInterface.WRITE))
159
    {
160
      hasPermission = hasPermission(userPackage,docId, 7);// 7 is all permission
161
    }//if
162
    else //in other situation, just check the request permission
163
    {
164
    
165
      // Check for @permission on @docid for @user and/or @groups
166
      hasPermission = hasPermission(userPackage,docId, permission);
167
     
168
    }//else
169
    
170
    return hasPermission;
171
  }
172
  
173
 
174
  /**
175
    * Check from db connection if the users in String array @principals has
176
    * @permission on @docid* 
177
    * @param principals, names in userPakcage need to check for @permission
178
    * @param docid, document identifier to check on
179
    * @param permission, permission (write or all...) to check for 
180
    */
181
  private boolean hasPermission(String [] principals, String docId,
182
                                           int permission)
183
                         throws SQLException
184
  {
185
    String subTreeId = null;// this is for top level, so subtree id is null
186
    try 
187
    {
188
      //first, if there is a docid owner in user package, return true
189
      //because doc owner has all permssion 
190
      if (containDocumentOwner(principals, docId))
191
      {
192
          
193
          return true;
194
      }
195
      
196
      //If there is no owner in user package, checking the table
197
      //check perm_order
198
      if (isAllowFirst(principals, docId, subTreeId))
199
      {
200
        
201
        if (hasExplicitDenyRule(principals, docId, permission, subTreeId))
202
        {
203
          //if it is allowfirst and has deny rule(either explicit )
204
          //deny access
205
          return false;
206
        }//if
207
        else if ( hasAllowRule(principals, docId, permission, subTreeId))
208
        {
209
          //if it is allowfirst and hasn't deny rule and has allow rule
210
          //allow access
211
          return true;
212
        }//else if
213
        else
214
        {
215
          //other situation deny access
216
          return false;
217
        }//else
218
     }//if isAllowFirst
219
     else //denyFirst
220
     {
221
       if (hasAllowRule(principals, docId, permission, subTreeId))
222
       {
223
         //if it is denyFirst and has allow rule, allow access
224
         return true;
225
       }
226
       else
227
       {
228
         //if it is denyfirst but no allow rule, deny access
229
         return false;
230
       }
231
     }//else denyfirst
232
    }//try
233
    catch (Exception e)
234
    {
235
      MetaCatUtil.debugMessage("There is a exception in hasPermission method: "
236
                         +e.getMessage(), 50);
237
    }
238
   
239
    return false;
240
  }//hasPermission
241
  
242
  /**
243
   * The method to determine of a node can be access by a user just by subtree
244
   * access control
245
   */
246
  public boolean hasPermissionForSubTreeNode(String user, String[] groups,
247
                                             String myPermission, long nodeId)
248
                                             throws McdbException
249
  {
250
    boolean flag = false;
251
    // Get unaccessble subtree for this user
252
    Hashtable unaccessableSubTree = hasUnaccessableSubTree(user, groups, 
253
                                                           myPermission);
254
    Enumeration en = unaccessableSubTree.elements();
255
    while (en.hasMoreElements())
256
    {
257
      SubTree tree = (SubTree)en.nextElement();
258
      long start = tree.getStartNodeId();
259
      long stop  = tree.getEndNodeId();
260
      if ( nodeId >= start && nodeId <= stop)
261
      {
262
        flag = true;
263
        break;
264
      }
265
    }
266
    return flag;
267
  }
268
  /**
269
   * This method will return a hasTable of subtree which user doesn't has the 
270
   * permssion to access
271
   * @param user  the user name
272
   * @param groups  the groups which the use is in
273
   * @param myPermission  permission type to check for
274
   */
275
  public Hashtable hasUnaccessableSubTree(String user, String[] groups, 
276
                                       String myPermission) throws McdbException
277
  {
278
    Hashtable resultUnaccessableSubTree = new Hashtable();
279
    String [] principals=null;
280
    int permission =AccessControlList.intValue(myPermission);
281
    
282
    //for the commnad line invocation return null(no unaccessable subtree)
283
    if ((user==null) && (groups==null || groups.length==0))
284
    {
285
      return resultUnaccessableSubTree;
286
    }
287
    
288
    //create a userpackage including user, public and group member
289
    principals=createUsersPackage(user, groups);
290
    //for the document owner return null(no unaccessable subtree)
291
    try
292
    {
293
      if (containDocumentOwner(principals, docId))
294
      {
295
       return resultUnaccessableSubTree;
296
      }
297
    }
298
    catch (SQLException ee)
299
    {
300
      throw new McdbException(ee);
301
    }
302
    
303
    // go through every subtree which has access control
304
    for (int i = 0; i< subTreeList.size(); i++)
305
    {
306
      SubTree tree = (SubTree)subTreeList.elementAt(i);
307
      String subTreeId = (String)(tree.getSubTreeId());
308
     
309
      if (subTreeId != null)
310
      {
311
        try
312
        {
313
          if (isAllowFirst(principals, docId, subTreeId))
314
          {
315
           
316
            if (hasExplicitDenyRule(principals, docId, permission, subTreeId))
317
            {
318
             
319
             //if it is allowfirst and has deny rule
320
              // put the subtree into unaccessable vector
321
              if (!resultUnaccessableSubTree.containsKey(subTreeId))
322
              {
323
                resultUnaccessableSubTree.put(subTreeId, tree);
324
              }
325
            }//if
326
            else if ( hasAllowRule(principals, docId, permission, subTreeId))
327
            {
328
              //if it is allowfirst and hasn't deny rule and has allow rule
329
              //allow access do nothing
330
              
331
            }//else if
332
            else
333
            {
334
              //other situation deny access
335
              if (!resultUnaccessableSubTree.containsKey(subTreeId))
336
              {
337
                resultUnaccessableSubTree.put(subTreeId, tree);
338
              }
339
             
340
            }//else
341
          }//if isAllowFirst
342
          else //denyFirst
343
          {
344
            if (hasAllowRule(principals, docId, permission,subTreeId))
345
            {
346
              //if it is denyFirst and has allow rule, allow access, do nothing
347
           
348
            }
349
            else
350
            {
351
              //if it is denyfirst but no allow rule, deny access
352
              // add into vector
353
              if (!resultUnaccessableSubTree.containsKey(subTreeId))
354
              {
355
                resultUnaccessableSubTree.put(subTreeId, tree);
356
              }
357
            }
358
          }//else denyfirst
359
        }//try
360
        catch( Exception e)
361
        {
362
          MetaCatUtil.debugMessage("error in PermissionControl.has" +
363
                                   "UnaccessableSubTree "+e.getMessage(), 30);
364
          throw new McdbException(e);
365
        }
366
      }//if
367
    }//for
368
    return resultUnaccessableSubTree;
369
  }//hasUnaccessableSubtree
370
  
371
  /**
372
    * Check if a document id is a access document. Access document need user
373
    * has "all" permission to access it.
374
    * @param docId, the document id need to be checked
375
    */
376
    private boolean isAccessDocument(String docId) throws SQLException
377
    {
378
      //detele the rev number if docid contains it
379
      docId=MetaCatUtil.getDocIdFromString(docId);
380
      PreparedStatement pStmt=null;
381
      DBConnection conn = null;
382
      int serialNumber = -1;
383
      try
384
      {
385
        //check out DBConnection
386
        conn=DBConnectionPool.getDBConnection("PermissionControl.isAccessDoc");
387
        serialNumber=conn.getCheckOutSerialNumber();
388
        pStmt = conn.prepareStatement("select 'x' from xml_access where " +
389
                                      "accessfileid like '" + docId +  "'");
390
        pStmt.execute();
391
        ResultSet rs = pStmt.getResultSet();
392
        boolean hasRow = rs.next();
393
        pStmt.close();
394
        if(hasRow)
395
        {
396
          return true;
397
        }
398
      }
399
      catch(SQLException e)
400
      {
401
       
402
        throw new SQLException("PermissionControl.isAccessDocument " +
403
                     "Error checking" +
404
                     " on document " + docId + ". " + e.getMessage());
405
      }
406
      finally
407
      {
408
        try
409
        {
410
           pStmt.close();
411
        }
412
        finally
413
        {
414
          DBConnectionPool.returnDBConnection(conn, serialNumber);
415
        }
416
      }
417
      return false;
418
    }//isAccessDocument
419
     
420
  
421
  
422
  /**
423
    * Check if a stirng array contains a given documents' owner
424
    * @param principals, a string array storing the username, groups name and
425
    * public.
426
    * @param docid, the id of given documents 
427
    */ 
428
  private boolean containDocumentOwner( String[] principals, String docId)
429
                    throws SQLException
430
  {
431
    int lengthOfArray=principals.length;
432
    boolean hasRow; 
433
    PreparedStatement pStmt=null;
434
    DBConnection conn = null;
435
    int serialNumber = -1;
436
    
437
    try
438
    {
439
      //check out DBConnection
440
     conn=DBConnectionPool.getDBConnection("PermissionControl.containDocOnwer");
441
      serialNumber=conn.getCheckOutSerialNumber();
442
      pStmt = conn.prepareStatement(
443
                "SELECT 'x' FROM xml_documents " +
444
                "WHERE docid = ? AND user_owner = ?"); 
445
      //check every element in the string array too see if it conatains
446
      //the owner of document
447
      for (int i=0; i<lengthOfArray; i++)
448
      {
449
             
450
        // Bind the values to the query
451
        pStmt.setString(1, docId);
452
        pStmt.setString(2, principals[i]);
453

    
454
        pStmt.execute();
455
        ResultSet rs = pStmt.getResultSet();
456
        hasRow = rs.next();
457
        if (hasRow) 
458
        {
459
          pStmt.close();
460
          return true;
461
        }//if    
462
     
463
      }//for
464
    }//try
465
    catch (SQLException e) 
466
    {
467
        pStmt.close();
468
       
469
        throw new 
470
        SQLException("PermissionControl.hasPermission(). " +
471
                     "Error checking ownership for " + principals[0] +
472
                     " on document #" + docId + ". " + e.getMessage());
473
    }//catch
474
    finally
475
    {
476
      try
477
      {
478
        pStmt.close();
479
      }
480
      finally
481
      {
482
        DBConnectionPool.returnDBConnection(conn, serialNumber);
483
      }
484
    }
485
    return false; 
486
  }//containDocumentOwner
487
  
488
  /**
489
    * Check if the permission order for user at that documents is allowFirst
490
    * @param principals, list of names of principals to check for 
491
    * @param docid, document identifier to check for
492
    */
493
  private boolean isAllowFirst(String [] principals, String docId, 
494
                               String subTreeId)
495
                  throws SQLException, Exception
496
  {
497
    int lengthOfArray=principals.length;
498
    boolean hasRow;
499
    PreparedStatement pStmt = null;
500
    DBConnection conn = null;
501
    int serialNumber = -1;
502
    String sql = null;
503
    boolean topLever =false;
504
    if (subTreeId == null)
505
    {
506
      //top level
507
      topLever = true;
508
      sql = "SELECT perm_order FROM xml_access " +
509
            "WHERE principal_name= ? AND docid = ? AND subtreeid is NULL";
510
    }
511
    else
512
    {
513
      //sub tree level
514
      sql = "SELECT perm_order FROM xml_access " +
515
            "WHERE principal_name= ? AND docid = ? AND subtreeid = ?";
516
    }
517
    
518
    try
519
    {
520
      //check out DBConnection
521
      conn=DBConnectionPool.getDBConnection("AccessControlList.isAllowFirst");
522
      serialNumber=conn.getCheckOutSerialNumber();
523
    
524
      //select permission order from database
525
      pStmt = conn.prepareStatement(sql);
526
   
527
      //check every name in the array
528
      for (int i=0; i<lengthOfArray;i++)
529
      {
530
        //bind value
531
        pStmt.setString(1, principals[i]);//user name
532
        pStmt.setString(2, docId);//docid
533
        
534
        // if subtree, we need set subtree id 
535
        if (!topLever)
536
        {
537
          pStmt.setString(3, subTreeId);
538
        }
539
    
540
        pStmt.execute();
541
        ResultSet rs = pStmt.getResultSet();
542
        hasRow=rs.next();
543
        if (hasRow)
544
        {
545
          //get the permission order from data base
546
          String permissionOrder=rs.getString(1);
547
          //if the permission order is "allowFirst
548
          if (permissionOrder.equalsIgnoreCase(AccessControlInterface.ALLOWFIRST))
549
          {
550
            pStmt.close();
551
            return true;
552
          }
553
          else
554
          {
555
            pStmt.close();
556
            return false;
557
          }
558
        }//if
559
      }//for
560
    }//try
561
    catch (SQLException e)
562
    {
563
      throw e;
564
    }
565
    finally
566
    {
567
      try
568
      {
569
        pStmt.close();
570
      }
571
      finally
572
      {
573
        DBConnectionPool.returnDBConnection(conn, serialNumber);
574
      }
575
    }
576
    
577
    //if reach here, means there is no permssion record for given names and 
578
    //docid. So throw a exception.
579
    
580
    throw new Exception("There is no permission record for user"+principals[0]+
581
                        "at document "+docId);
582
        
583
  }//isAllowFirst
584
  
585
  /**
586
    * Check if the users array has allow rules for given users, docid and 
587
    * permission.
588
    * If it has permission rule and ticket count is greater than 0, the ticket
589
    * number will decrease one for every allow rule
590
    * @param principals, list of names of principals to check for 
591
    * @param docid, document identifier to check for
592
    * @param permission, the permssion need to check
593
    */
594
  private boolean hasAllowRule(String [] principals, String docId, 
595
                                  int permission, String subTreeId)
596
                  throws SQLException, Exception
597
 {
598
   int lengthOfArray=principals.length;
599
   boolean allow=false;//initial value is no allow rule
600
   ResultSet rs;
601
   PreparedStatement pStmt = null;
602
   int permissionValue=permission;
603
   int permissionValueInTable;
604
   int ticketCount;
605
   DBConnection conn = null;
606
   int serialNumber = -1;
607
   boolean topLever = false;
608
   String sql = null;
609
   if (subTreeId == null)
610
   {
611
     // for toplevel
612
     topLever = true;
613
     sql = "SELECT permission FROM xml_access WHERE docid = ? " +  
614
           "AND principal_name = ? AND perm_type = ? AND subtreeid is NULL";
615
   }
616
   else
617
   {
618
     topLever =false;
619
     sql = "SELECT permission FROM xml_access WHERE docid = ? " +  
620
           "AND principal_name = ? AND perm_type = ? AND subtreeid= ?";
621
   }
622
   try
623
   {
624
     //check out DBConnection
625
     conn=DBConnectionPool.getDBConnection("AccessControlList.hasAllowRule");
626
     serialNumber=conn.getCheckOutSerialNumber();
627
    //This sql statement will select entry with 
628
    //begin_time<=currentTime<=end_time in xml_access table
629
    //If begin_time or end_time is null in table, isnull(begin_time, sysdate)
630
    //function will assign begin_time=sysdate
631
    pStmt = conn.prepareStatement(sql);
632
    //bind docid, perm_type
633
    pStmt.setString(1, docId);
634
    pStmt.setString(3, AccessControlInterface.ALLOW);
635
    
636
    // if subtree lever, need to set subTreeId
637
    if (!topLever)
638
    {
639
      pStmt.setString(4, subTreeId);
640
    }
641
   
642
    //bind every elenment in user name array
643
    for (int i=0;i<lengthOfArray; i++)
644
    {
645
      pStmt.setString(2, principals[i]);
646
      pStmt.execute();
647
      rs=pStmt.getResultSet();
648
      while (rs.next())//check every entry for one user
649
      {
650
        permissionValueInTable=rs.getInt(1);
651
            
652
        //permission is ok  
653
        //the user have a permission to access the file
654
        if (( permissionValueInTable & permissionValue )== permissionValue )
655
        {
656
           allow=true;//has allow rule entry
657
        }//if
658
      }//while
659
    }//for
660
   }//try
661
   catch (SQLException sqlE)
662
   {
663
     throw sqlE;
664
   }
665
   catch (Exception e)
666
   {
667
     throw e;
668
   }
669
   finally
670
   {
671
     try
672
     {
673
       pStmt.close();
674
     }
675
     finally
676
     {
677
       DBConnectionPool.returnDBConnection(conn, serialNumber);
678
     }
679
   }
680
    return allow;
681
 }//hasAllowRule
682
 
683
 
684
   
685
   /**
686
    * Check if the users array has explicit deny rules for given users, docid 
687
    * and permission. That means the perm_type is deny and current time is
688
    * less than end_time and greater than begin time, or no time limit.
689
    * @param principals, list of names of principals to check for 
690
    * @param docid, document identifier to check for
691
    * @param permission, the permssion need to check
692
    */
693
  private boolean hasExplicitDenyRule(String [] principals, String docId, 
694
                                      int permission, String subTreeId)
695
                  throws SQLException
696
 {
697
   int lengthOfArray=principals.length;
698
   ResultSet rs;
699
   PreparedStatement pStmt = null;
700
   int permissionValue=permission;
701
   int permissionValueInTable;
702
   DBConnection conn = null;
703
   int serialNumber = -1;
704
   String sql = null;
705
   boolean topLevel = false;
706
   
707
   // decide top level or subtree level
708
   if (subTreeId == null)
709
   {
710
     topLevel = true;
711
     sql = "SELECT permission FROM xml_access WHERE docid = ? " + 
712
            "AND principal_name = ? AND perm_type = ? AND subtreeid is NULL";
713
   }
714
   else
715
   {
716
     topLevel = false;
717
     sql = "SELECT permission FROM xml_access WHERE docid = ? " + 
718
            "AND principal_name = ? AND perm_type = ? AND subtreeid = ?";
719
   }
720
   
721
   try
722
   {
723
     //check out DBConnection
724
     conn=DBConnectionPool.getDBConnection("PermissionControl.hasExplicitDeny");
725
     serialNumber=conn.getCheckOutSerialNumber();
726
   
727
     pStmt = conn.prepareStatement(sql);
728
    //bind docid, perm_type
729
    pStmt.setString(1, docId);
730
    pStmt.setString(3, AccessControlInterface.DENY);
731
    
732
    // subtree level need to set up subtreeid
733
    if (!topLevel)
734
    {
735
      pStmt.setString(4, subTreeId);
736
    }
737
   
738
    //bind every elenment in user name array
739
    for (int i=0;i<lengthOfArray; i++)
740
    {
741
      pStmt.setString(2, principals[i]);
742
      pStmt.execute();
743
      rs=pStmt.getResultSet();
744
      while (rs.next())//check every entry for one user
745
      {
746
        permissionValueInTable=rs.getInt(1);
747
        
748
        //permission is ok the user doesn't have permission to access the file
749
        if (( permissionValueInTable & permissionValue )== permissionValue )
750
             
751
        {
752
           pStmt.close();
753
           return true;
754
         }//if
755
      }//while
756
    }//for
757
   }//try
758
   catch (SQLException e)
759
   {
760
     throw e;
761
   }//catch
762
   finally
763
   {
764
     try
765
     {
766
       pStmt.close();
767
     }
768
     finally
769
     {
770
       DBConnectionPool.returnDBConnection(conn, serialNumber);
771
     }
772
   }//finally
773
   return false;//no deny rule
774
  }//hasExplicitDenyRule 
775
   
776

    
777
  /**
778
    * Creat a users pakages to check permssion rule, user itself, public and
779
    * the gourps the user belong will be include in this package
780
    * @param user, the name of user
781
    * @param groups, the string array of the groups that user belong to
782
    */
783
  private String[] createUsersPackage(String user, String [] groups)
784
  {
785
    String [] usersPackage=null;
786
    int lengthOfPackage;
787
    
788
    if (groups!=null)
789
    {
790
      //if gouprs is not null and user is not public, we should create a array 
791
      //to store the groups and user and public. 
792
      //So the length of userPackage is the length of group plus two
793
      if (!user.equalsIgnoreCase(AccessControlInterface.PUBLIC))
794
      {
795
        lengthOfPackage=(groups.length)+2;
796
        usersPackage=new String [lengthOfPackage];
797
        //the first two elements is user self and public
798
        usersPackage[0]=user;
799
        usersPackage[1]=AccessControlInterface.PUBLIC;
800
        //put groups element from index 0 to lengthOfPackage-3 into userPackage
801
        //from index 2 to lengthOfPackage-1
802
        for (int i=2; i<lengthOfPackage; i++)
803
        {
804
          usersPackage[i]=groups[i-2];
805
        } //for
806
      }//if user!=public
807
      else//use=public
808
      {
809
        lengthOfPackage=(groups.length)+1;
810
        usersPackage=new String [lengthOfPackage];
811
        //the first lements is public
812
        usersPackage[0]=AccessControlInterface.PUBLIC;
813
        //put groups element from index 0 to lengthOfPackage-2 into userPackage
814
        //from index 1 to lengthOfPackage-1
815
        for (int i=1; i<lengthOfPackage; i++)
816
        {
817
          usersPackage[i]=groups[i-1];
818
        } //for
819
      }//else user=public
820
       
821
    }//if groups!=null
822
    else
823
    {
824
      //because no groups, the userPackage only need two elements
825
      //one is for user, the other is for public
826
      if (!user.equalsIgnoreCase(AccessControlInterface.PUBLIC))
827
      {
828
        lengthOfPackage=2;
829
        usersPackage=new String [lengthOfPackage];
830
        usersPackage[0]=user;
831
        usersPackage[1]=AccessControlInterface.PUBLIC;
832
      }//if user!=public
833
      else //user==public
834
      {
835
        //only put public into array
836
        lengthOfPackage=1;
837
        usersPackage=new String [lengthOfPackage];
838
        usersPackage[0]=AccessControlInterface.PUBLIC;
839
      }
840
    }//else groups==null
841
    return usersPackage;
842
  }//createUsersPackage
843
 
844
  /**
845
    * This method will return a data set id for given access id.
846
    * @param accessDocId, the accessDocId which need to be found data set id
847
   */
848
  private String getDataSetId(String accessDocId) 
849
                              throws SQLException
850
  {
851
    String dataSetId=null;
852
    PreparedStatement pStmt=null;
853
    ResultSet rs=null;
854
    DBConnection conn=null;
855
    int serialNumber=-1;
856
    String query="SELECT docId from xml_relation where subject = ? or "
857
                                                +"object = ?";
858
    
859
    try
860
    {
861
      //check out DBConnection
862
      conn=DBConnectionPool.getDBConnection("PermissionControl.getDataSetId");
863
      serialNumber=conn.getCheckOutSerialNumber();
864
      
865
      pStmt=conn.prepareStatement(query);
866
      //bind the value to query
867
      pStmt.setString(1, accessDocId);
868
      pStmt.setString(2, accessDocId);
869
      //execute the query
870
      pStmt.execute();
871
      rs=pStmt.getResultSet();
872
      //process the result
873
      if (rs.next()) //There are some records for the data set id for access id
874
      {
875
        dataSetId=rs.getString(1);
876
      }
877
      else //No data set id for the given access id in xml_relation table
878
      {
879
        dataSetId=null;
880
      }
881
    }//try
882
    finally
883
    {
884
      try
885
      {
886
        pStmt.close();
887
      }
888
      finally
889
      {
890
        DBConnectionPool.returnDBConnection(conn, serialNumber);
891
      }
892
    }
893
    return dataSetId;
894
  }//getDataPackageId() 
895
  
896
  /**
897
    * To create a part of query: "docid like '" +str1+ "', " +"docid like '" 
898
    * +str2+"'" ... We need to check user, group and public together for the 
899
    * permission. So we need the principal in an array and according the array
900
    * to create a part of query which will be used in other methods
901
    * @param principals, a string array storing the username, groups name and
902
    * public.
903
    */
904
   private String partQueryAboutDocId( String [] principals)
905
   {
906
     String partQuery="";
907
     int lengthOfArray=principals.length;
908
     
909
     for (int i=0;i<(lengthOfArray-1);i++)
910
     {
911
        partQuery=partQuery+"docid like '"+principals[i]+"',";
912
     }
913
     
914
     //the last one dosen't has "'"
915
     partQuery=partQuery+"docid like '"+principals[(lengthOfArray-1)]+"'";
916
     return partQuery;
917
     
918
   }
919
}
(44-44/54)