package org.dbxml.tests;

/*
 * 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: TestSecurity.java,v 1.8 2001/06/03 03:20:56 tom Exp $
 */

import org.dbxml.core.*;
import org.dbxml.core.security.*;
import org.dbxml.core.objects.*;
import org.dbxml.xml.dom.*;

import org.w3c.dom.*;

/**
 * Simple portal demo application.
 */
public class TestSecurity extends SimpleXMLObject {
   public TestSecurity() {
      super();
      try {
         meta.reflect(TestSecurity.class);

         String[] searchParams = {};
         meta.addMethod("test", searchParams);
      }
      catch(Exception e) {
        org.dbxml.Debug.printStackTrace(e);
      }
   }

   public String test() {
      StringBuffer result = new StringBuffer();
      try {
         DBSecurityManager man = collection.getDatabase().getSecurityManager();

         Credentials cred = null;
         try {
            cred = man.authenticate("kstaken", "test");
            result.append("Pass: User found kstaken<br />");
         }
         catch (Exception e) {
            result.append("Fail: User not found kstaken<br />");
         }

         if (cred.checkGroup("test")) {
            result.append("Pass: Got a group match<br />");
         }

         try {
            Credentials cred2 = man.authenticate("kstaken2", "test");
            result.append("Fail: Access granted for kstaken2<br />");
         }
         catch (Exception e) {
            result.append("Pass: User not found kstaken2<br />");
         }

         try {
            Credentials cred3 = man.authenticate("kstaken", "badpw");
            result.append("Fail: Access granted for kstaken with bad pw<br />");
         }
         catch (Exception e) {
            result.append("Pass: User kstaken denied access with bad pw<br />");
         }

         try {
            man.checkAccess("/local/", Permission.READ);
            result.append("Pass: Read access allowed<br />");
         }
         catch (Exception e) {
            result.append("Fail: Failed for read<br />");
         }

         try {
            man.checkAccess("/local/", Permission.WRITE);
            result.append("Pass: Write access allowed<br />");
         }
         catch (Exception e) {
            result.append("Fail: Failed for write<br />");
         }

         try {
            man.checkAccess("/local/", Permission.DELETE);
            result.append("Pass: Delete access allowed<br />");
         }
         catch (Exception e) {
            result.append("Fail: Failed for delete<br />");
         }

         try {
            man.checkAccess("/local/", Permission.ADMIN);
            result.append("Fail: Admin access allowed<br />");
         }
         catch (Exception e) {
            result.append("Pass: Admin access denied for kstaken<br />");
         }

         try {
            man.checkAccess("/local/top/obj1/", Permission.READ);
            result.append("Pass: Read access allowed based on all perm<br />");
         }
         catch (Exception e) {
            result.append("Fail: Read access denied for kstaken on all perm<br />");
         }
      }
      catch (Exception e) {
         org.dbxml.Debug.printStackTrace(e);
      }

      return "<html>" + result.toString() + "</html>";
   }
}


