Project

General

Profile

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