package org.dbxml.core.security;

/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * 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 acknowledgment:
 *       "This product includes software developed by
 *        The dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML 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 DBXML GROUP 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.
 *
 * $Id: ResourcePermissions.java,v 1.6 2001/05/23 00:35:29 tom Exp $
 */

import java.util.*;
import org.dbxml.server.*;

/**
 *
 */
public class ResourcePermissions implements Configurable {
   private static final String ALLOW = "allow";
   private static final String DENY = "deny";
   private static final String ALL = "all";

   protected ArrayList allowRead = null;
   protected ArrayList allowWrite = null;
   protected ArrayList allowExecute = null;
   protected ArrayList allowDelete = null;
   protected ArrayList allowModify = null;
   protected ArrayList allowAdmin = null;

   protected ArrayList denyRead = null;
   protected ArrayList denyWrite = null;
   protected ArrayList denyExecute = null;
   protected ArrayList denyDelete = null;
   protected ArrayList denyModify = null;
   protected ArrayList denyAdmin = null;

   protected String resource = "";
   protected Configuration config = null;

   public ResourcePermissions(String resource) {
      this.resource = resource;
   }

   /**
    * Grants the specified group a permission for this resource.
    *
    *    Valid permission types are
    *       Permission.READ
    *       Permission.WRITE
    *       Permission.DELETE
    *       Permission.MODIFY
    *       Permission.EXECUTE
    *       Permission.ADMIN
    *
    * @param type The type of permission to set.
    * @param group The group being given the permission.
    */
   public void addPermissionAllow(int type, String group) {
      switch(type) {
         case Permission.READ:
            allowRead = addPermission(allowRead, group);
            break;

         case Permission.WRITE:
            allowWrite = addPermission(allowWrite, group);
            break;

         case Permission.DELETE:
            allowDelete = addPermission(allowDelete, group);
            break;

         case Permission.MODIFY:
            allowModify = addPermission(allowModify, group);
            break;

         case Permission.EXECUTE:
            allowExecute = addPermission(allowExecute, group);
            break;

         case Permission.ADMIN:
            allowAdmin = addPermission(allowAdmin, group);
            break;
      }
   }


   /**
    * Denys the specified group a permission for this resource.
    *
    *    Valid permission types are
    *       Permission.READ
    *       Permission.WRITE
    *       Permission.DELETE
    *       Permission.MODIFY
    *       Permission.EXECUTE
    *       Permission.ADMIN
    *
    * @param type The type of permission to set.
    * @param group The group being denied the permission.
    */
   public void addPermissionDeny(int type, String group) {
      switch(type) {
         case Permission.READ:
            denyRead = addPermission(denyRead, group);
            break;

         case Permission.WRITE:
            denyWrite = addPermission(denyWrite, group);
            break;

         case Permission.DELETE:
            denyDelete = addPermission(denyDelete, group);
            break;

         case Permission.MODIFY:
            denyModify = addPermission(denyModify, group);
            break;

         case Permission.EXECUTE:
            denyExecute = addPermission(denyExecute, group);
            break;

         case Permission.ADMIN:
            denyAdmin = addPermission(denyAdmin, group);
            break;
      }
   }

   protected ArrayList addPermission(ArrayList list, String group) {
      if (list == null) {
         list = new ArrayList();
      }

      list.add(group);
      return list;
   }

   /**
    * Checks whether the specified group has type permission to access this
    * resource.
    *
    *    Valid permission types are
    *       Permission.READ
    *       Permission.WRITE
    *       Permission.DELETE
    *       Permission.MODIFY
    *       Permission.EXECUTE
    *       Permission.ADMIN
    *
    * @param type The type of permission to check.
    * @param group The group requested permission.
    */
   public boolean checkPermission(int access, int type, String group) {
      if (access == Permission.ALLOW) {
         switch(type) {
            case Permission.READ:
               return checkPermission(allowRead, group);

            case Permission.WRITE:
               return checkPermission(allowWrite, group);

            case Permission.DELETE:
               return checkPermission(allowDelete, group);

            case Permission.MODIFY:
               return checkPermission(allowModify, group);

            case Permission.EXECUTE:
               return checkPermission(allowExecute, group);

            case Permission.ADMIN:
               return checkPermission(allowAdmin, group);
         }
      }
      else if (access == Permission.DENY) {
         switch(type) {
            case Permission.READ:
               return checkPermission(denyRead, group);

            case Permission.WRITE:
               return checkPermission(denyWrite, group);

            case Permission.DELETE:
               return checkPermission(denyDelete, group);

            case Permission.MODIFY:
               return checkPermission(denyModify, group);

            case Permission.EXECUTE:
               return checkPermission(denyExecute, group);

            case Permission.ADMIN:
               return checkPermission(denyAdmin, group);
         }
      }

      return false;
   }

   protected boolean checkPermission(ArrayList list, String group) {
      if (list != null) {
         if (list.indexOf(group) != -1) {
            return true;
         }
      }

      return false;
   }



   /**
    * setConfig sets the configuration information for the Configurable
    * object instance.
    *
    * @param config The configuration Node
    */
   public void setConfig(Configuration config) {
      this.config = config;
      config.processChildren(ALLOW, new ConfigurationCallback() {
               public void process(Configuration cfg) {
                  String type = cfg.getAttribute("type", "");
                  String group = cfg.getAttribute("group", "");

                  // If we have a type of all then all user level perms for
                  // this group should be granted.
                  if (type.equalsIgnoreCase(ALL)) {
                     addPermissionAllow(Permission.READ, group);
                     addPermissionAllow(Permission.WRITE, group);
                     addPermissionAllow(Permission.DELETE, group);
                     addPermissionAllow(Permission.EXECUTE, group);
                     addPermissionAllow(Permission.MODIFY, group);
                  }
                  else {
                     addPermissionAllow(Permission.mapType(type), group);
                  }
               }
            }
         );

      config.processChildren(DENY, new ConfigurationCallback() {
               public void process(Configuration cfg) {
                  String type = cfg.getAttribute("type", "");
                  String group = cfg.getAttribute("group", "");

                  // If we have a type of all then all user level perms for
                  // this group should be granted.
                  if (type.equalsIgnoreCase(ALL)) {
                     addPermissionDeny(Permission.READ, group);
                     addPermissionDeny(Permission.WRITE, group);
                     addPermissionDeny(Permission.DELETE, group);
                     addPermissionDeny(Permission.EXECUTE, group);
                     addPermissionDeny(Permission.MODIFY, group);
                  }
                  else {
                     addPermissionDeny(Permission.mapType(type), group);
                  }
               }
            }
         );
   }

   /**
    * getConfig retrieves the configuration information for the
    * Configurable object instance.
    *
    * @return The configuration Node
    */
   public Configuration getConfig() {
      return config;
   }
}
