juergen     2002/10/30 01:49:08

  Added:       src/share/org/apache/slide/security
                        SecurityImplAllGrant.java
  Log:
  added acl semantic switch
  
  Revision  Changes    Path
  1.1                  
jakarta-slide/src/share/org/apache/slide/security/SecurityImplAllGrant.java
  
  Index: SecurityImplAllGrant.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/security/SecurityImplAllGrant.java,v
 1.1 2002/10/30 09:49:08 juergen Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/30 09:49:08 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Slide", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.slide.security;
  
  import java.util.Enumeration;
  import java.util.Hashtable;
  import java.util.HashMap;
  import java.util.Vector;
  import java.lang.reflect.Constructor;
  import java.lang.reflect.InvocationTargetException;
  import org.apache.slide.common.*;
  import org.apache.slide.structure.*;
  import org.apache.slide.authenticate.CredentialsToken;
  import org.apache.slide.util.Configuration;
  
  /**
   * Security helper.
   *
   * @author <a href="mailto:eckehard.hermann@;softwareag.com">Eckehard Hermann</a>
   * @version $Revision: 1.1 $
   */
  public final class SecurityImplAllGrant extends SecurityImpl implements Security {
  
      
      // ----------------------------------------------------------- Constructors
      
      
      /**
       * Constructor.
       *
       * @param namespace Namespace
       * @param namespaceConfig Namespace configuration
       */
      public SecurityImplAllGrant(Namespace namespace, NamespaceConfig 
namespaceConfig) {
          super(namespace, namespaceConfig);
      }
  
      
      /**
       * Check whether or not an actor can perform the specified activity
       * on a collection.
       *
       * @param object Object on which access is tested
       * @param subject Subject who seeks to perform the action
       * @param action Action which is to be performed
       * @return true if the action can be performed
       * @exception ServiceAccessException DataSource access error
       * @exception ObjectNotFoundException Specified object was not found
       * in the DataSource
       */
      public boolean hasPermission(ObjectNode object, SubjectNode subject,
                                   ActionNode action)
          throws ServiceAccessException, ObjectNotFoundException {
          
          boolean granted = false;
          boolean denied = false;
          boolean rootObjectReached = false;
          
          ObjectNode courObject = object;
          
          Uri subjectUri = namespace.getUri(subject.getUri());
          Uri actionUri = namespace.getUri(action.getUri());
          
          // check if allready granded
  
          while (!granted && !denied && !rootObjectReached) {
              
              Uri courUri = namespace.getUri(courObject.getUri());
              Enumeration permissions = courUri.getStore()
                  .enumeratePermissions(courUri);
              
              while (!granted && !denied && permissions.hasMoreElements()) {
                  
                  boolean oldGranted = granted;
                  boolean oldDenied = denied;
                  
                  NodePermission permission =
                      (NodePermission) permissions.nextElement();
                  String permissionSubject = permission.getSubjectUri();
                  
                  if (permissionSubject.equals("~")) {
                      
                      boolean check;
                      check = object.getUri().equals(subjectUri.toString());
                      if (permission.isInheritable()) {
                          String subjectUriString = subjectUri.toString();
                          if(!subjectUriString.endsWith("/"))
                              subjectUriString = subjectUriString + "/";
  
                          check |= object.getUri().startsWith(subjectUriString);
                      }
                      
                      // Self permission
                      granted = (!permission.isNegative())
                          && (check)
                          && (actionUri.toString()
                              .startsWith(permission.getActionUri()));
                      denied = (permission.isNegative())
                          && (check)
                          && (actionUri.toString()
                              .startsWith(permission.getActionUri()));
                      
                  } else if (permission.isInheritable()
                      || permission.getObjectUri().equals(object.getUri())) {
                      
                      if (permissionSubject.startsWith("/")) {
                          
                          // Node permission
  
                          String permSubj = permission.getSubjectUri();
                          if(!permSubj.endsWith("/"))
                              permSubj = permSubj + "/";
                          boolean match = subjectUri.toString().
                              equals(permission.getSubjectUri()) ||
                              subjectUri.toString().startsWith(permSubj);
                          match &= actionUri.toString().
                              startsWith(permission.getActionUri());
  
                          granted = (!permission.isNegative()) && match;
                          denied = permission.isNegative() && match;
                          
                      } else if (permissionSubject.startsWith("+")) {
                          
                          // Permission group which needs to be expanded
                          Uri permissionSubjectUri =
                              namespace.getUri(permissionSubject.substring(1));
                          ObjectNode group =
                              permissionSubjectUri.getStore().retrieveObject
                              (permissionSubjectUri);
                          // if the node is a GroupNode, expand it out to
                          // normal permissions
                          if (group instanceof
                              org.apache.slide.structure.GroupNode ) {
                              if (group.hasChildren()) {
                                  Enumeration groupMembers =
                                      group.enumerateChildren();
                                  // parse thru the children of the group and
                                  // check permissions on each
                                  while (groupMembers.hasMoreElements()) {
                                      
                                      oldGranted = granted;
                                      oldDenied = denied;
                                      
                                      Uri childUri =
                                          namespace.getUri
                                          ((String) groupMembers.nextElement());
                                      ObjectNode childNode =
                                          childUri.getStore().retrieveObject
                                          (childUri);
                                      String childSubjectUri = childNode
                                          instanceof LinkNode ?
                                          ((LinkNode) childNode)
                                          .getLinkedUri() :
                                          childNode.getUri() ;
  
                                      String testUri;
                                      if(!childSubjectUri.endsWith("/"))
                                          testUri = childSubjectUri+"/";
                                      else
                                          testUri = childSubjectUri;
  
                                      boolean match = subjectUri.toString().
                                          equals(childSubjectUri) ||
                                          subjectUri.toString().
                                          startsWith(testUri);
                                      match &= actionUri.toString().
                                          startsWith(permission.getActionUri());
                                      
                                      granted = (!permission.isNegative()) &&
                                          match;
                                      denied = permission.isNegative() && match;
                                      
                                      granted = granted | oldGranted;
                                      denied = denied | oldDenied;
                                      
                                  }
                              }
                          }
                          
                      } else {
                          
                          // Role permission
                          granted = (!permission.isNegative())
                              && (hasRole(subject, permissionSubject))
                              && (actionUri.toString()
                                  .startsWith(permission.getActionUri()));
                          denied = (permission.isNegative())
                              && (hasRole(subject, permissionSubject))
                              && (actionUri.toString()
                                  .startsWith(permission.getActionUri()));
                          
                      }
                      
                  }
                  
                  granted = granted | oldGranted;
                  denied = denied | oldDenied;
                  
              }
              
              Uri parentUri = courUri.getParentUri();
              
              if (parentUri != null) {
                  courObject = parentUri.getStore()
                      .retrieveObject(parentUri);
              } else {
                  rootObjectReached = true;
              }
          }
          
          // Negative permissions have priority (if they're defined on the same
          // node)
          if (denied) {
              return false;
          }
          
          if (!granted) {
              return false;
          }
          
          return true;
          
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:slide-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:slide-dev-help@;jakarta.apache.org>

Reply via email to