/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000-2001 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 acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 *     "Apache Jetspeed" must not be used to endorse or promote products
 *    derived from this software without prior written permission. For
 *    written permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache" or
 *    "Apache Jetspeed", nor may "Apache" appear in their name, without
 *    prior written permission of the Apache Software Foundation.
 *
 * 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/>.
 */

package org.apache.jetspeed.services.security.turbine;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;

import javax.servlet.ServletConfig;


// Jetspeed Security
import org.apache.jetspeed.services.security.PermissionManagement;
import org.apache.jetspeed.services.security.JetspeedSecurityCache;
import org.apache.jetspeed.services.security.CachedAcl;

import org.apache.jetspeed.om.security.JetspeedUser;
import org.apache.jetspeed.om.security.BaseJetspeedUser;
import org.apache.jetspeed.om.security.Role;
import org.apache.jetspeed.om.security.Permission;

import org.apache.jetspeed.services.JetspeedSecurity;
import org.apache.jetspeed.services.security.UserManagement;
import org.apache.jetspeed.services.security.JetspeedSecurityService;
import org.apache.jetspeed.om.security.UserNamePrincipal;

// Jetspeed Security Exceptions
import org.apache.jetspeed.services.security.PermissionException;
import org.apache.jetspeed.services.security.JetspeedSecurityException;

import org.apache.jetspeed.services.PsmlManager;

// Jetspeed Database OM
import org.apache.jetspeed.om.security.turbine.TurbinePermission;
import org.apache.jetspeed.om.security.turbine.TurbinePermissionPeer;
import org.apache.jetspeed.om.security.turbine.TurbineRolePermission;
import org.apache.jetspeed.om.security.turbine.TurbineRolePermissionPeer;

// Torque
import org.apache.torque.util.Criteria;
import org.apache.torque.om.NumberKey;
import org.apache.torque.Torque;
import org.apache.torque.TorqueException;

// Rundata
import org.apache.jetspeed.services.rundata.JetspeedRunDataService;
import org.apache.jetspeed.services.rundata.JetspeedRunData;
import org.apache.turbine.services.rundata.RunDataService;

// Turbine
import org.apache.turbine.util.Log;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.resources.ResourceService;

/**
 * Default Jetspeed-Turbine Permission Management implementation
 *
 *
 * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
 * @version $Id: TurbinePermissionManagement.java,v 1.5 2002/10/18 19:16:32 taylor Exp $
 */

public class TurbinePermissionManagement extends TurbineBaseService
                                   implements PermissionManagement
{
    private JetspeedRunDataService runDataService = null;
    private final static String CASCADE_DELETE = "programmatic.cascade.delete";
    private final static boolean DEFAULT_CASCADE_DELETE = true;
    private boolean cascadeDelete;
    private final static String CACHING_ENABLE = "caching.enable";
    private boolean cachingEnable = true;

    ///////////////////////////////////////////////////////////////////////////
    // Permission Management Interfaces
    ///////////////////////////////////////////////////////////////////////////

    /**
     * Retrieves all <code>Permission</code>s for a given rolename principal.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @param rolename a role name identity to be retrieved.
     * @return Iterator over all permissions associated to the role principal.
     * @exception PermissionException when the security provider has a general failure.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public Iterator getPermissions(String rolename)
        throws JetspeedSecurityException
    {
        Role role = null;
        try
        {
            if (cachingEnable)
            {
                Iterator iterator = JetspeedSecurityCache.getPermissions(rolename);
                if (iterator != null)
                {
                    return iterator;
                }
            }
            role = JetspeedSecurity.getRole(rolename);
        }
        catch(JetspeedSecurityException e)
        {
            throw new PermissionException("Failed to Retrieve Role: ", e);
        }
        Criteria criteria = new Criteria();
        criteria.add(TurbineRolePermissionPeer.ROLE_ID, role.getId());
        List rels;
        HashMap perms;

        try
        {
            rels = TurbineRolePermissionPeer.doSelect(criteria);
            if (rels.size() > 0)
            {
                perms = new HashMap(rels.size());
            }
            else
                perms = new HashMap();

            for (int ix = 0; ix < rels.size(); ix++)
            {
                TurbineRolePermission rel = (TurbineRolePermission)rels.get(ix);
                Permission perm = rel.getTurbinePermission();
                perms.put(perm.getName(), perm);
            }
        }
        catch(Exception e)
        {
            throw new PermissionException("Failed to retrieve permissions ", e);
        }
        return perms.values().iterator();
    }

    /**
     * Retrieves all <code>Permission</code>s.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @return Iterator over all permissions.
     * @exception PermissionException when the security provider has a general failure.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public Iterator getPermissions()
        throws JetspeedSecurityException
    {
        Criteria criteria = new Criteria();
        List permissions;
        try
        {
            permissions = TurbinePermissionPeer.doSelect(criteria);
        }
        catch(Exception e)
        {
            throw new PermissionException("Failed to retrieve permissions ", e);
        }
        return permissions.iterator();
    }

    /**
     * Adds a <code>Permission</code> into permanent storage.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @exception PermissionException when the security provider has a general failure.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public void addPermission(Permission permission)
        throws JetspeedSecurityException
    {
        if(permissionExists(permission.getName()))
        {
            throw new PermissionException("The permission '" +
                permission.getName() + "' already exists");
        }

        try
        {
            TurbinePermission tpermission = new TurbinePermission();
            tpermission.setPermissionName(permission.getName());
            Criteria criteria = TurbinePermissionPeer.buildCriteria(tpermission);
            NumberKey key = (NumberKey)TurbinePermissionPeer.doInsert(criteria);
            permission.setId(key.toString());
        }
        catch(Exception e)
        {
            throw new PermissionException("Failed to create permission '" +
                permission.getName() + "'", e);
        }
    }


    /**
     * Saves a <code>Permission</code> into permanent storage.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @exception PermissionException when the security provider has a general failure.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public void savePermission(Permission permission)
        throws JetspeedSecurityException
    {
        if(!permissionExists(permission.getName()))
        {
            throw new PermissionException("The permission '" +
                permission.getName() + "' doesn't exists");
        }

        try
        {
            if (permission instanceof TurbinePermission)
            {
                TurbinePermissionPeer.doUpdate((TurbinePermission)permission);
            }
            else
            {
                throw new PermissionException("TurbinePermissionManagment: Permission is not a Turbine permission, cannot update");
            }

        }
        catch(Exception e)
        {
            throw new PermissionException("Failed to create permission '" +
                permission.getName() + "'", e);
        }

    }

    /**
     * Removes a <code>Permission</code> from the permanent store.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @param permissionName the principal identity of the permission to be retrieved.
     * @exception PermissionException when the security provider has a general failure.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public void removePermission(String permissionName)
        throws JetspeedSecurityException
    {
        Connection conn = null;
        try
        {
            conn = Torque.getConnection();
            Permission permission = this.getPermission(permissionName);

            Criteria criteria = new Criteria();
            criteria.add(TurbinePermissionPeer.PERMISSION_NAME, permissionName);

            if(cascadeDelete)
            {
                // CASCADE to TURBINE_ROLE_PERMISSION
                Criteria critRolePerm = new Criteria();
                critRolePerm.add(TurbineRolePermissionPeer.PERMISSION_ID, permission.getId());
                TurbineRolePermissionPeer.doDelete(critRolePerm, conn);
            }
            TurbinePermissionPeer.doDelete(criteria, conn);
                
            conn.commit();
            
            if (cachingEnable)
            {
                JetspeedSecurityCache.removeAllPermissions(permissionName);
            }
        }
        catch(Exception e)
        {
            try
            {
                conn.rollback();
            }
            catch (java.sql.SQLException sqle)
            {
                Log.error(sqle);
            }
            throw new PermissionException("Failed to remove permission '" +
                permissionName + "'", e);
        }
        finally
        {
            try
            {
                Torque.closeConnection(conn);
            }
            catch (Exception e){}
        }

    }

    /**
     * Grants a permission to a role.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @param roleName grant a permission to this role.
     * @param permissionName the permission to grant to the role.
     * @exception PermissionException when the security provider has a general failure retrieving permissions.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public void grantPermission(String roleName, String permissionName)
        throws JetspeedSecurityException
    {
        try
        {
            Role role = JetspeedSecurity.getRole(roleName);
            Permission permission = this.getPermission(permissionName);

            Criteria criteria = new Criteria();
            criteria.add(TurbineRolePermissionPeer.ROLE_ID, role.getId());
            criteria.add(TurbineRolePermissionPeer.PERMISSION_ID, permission.getId());
            TurbineRolePermissionPeer.doInsert(criteria);
            if (cachingEnable)
            {
                JetspeedSecurityCache.addPermission(roleName,permission);
            }
        }
        catch(Exception e)
        {
            throw new PermissionException("Grant permission '" + permissionName + "' to role '" + roleName + "' failed: ", e);
        }
    }

    /**
     * Revokes a permission from a role.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @param roleName grant a permission to this role.
     * @param permissionName the permission to grant to the role.
     * @exception PermissionException when the security provider has a general failure retrieving permissions.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public void revokePermission(String roleName, String permissionName)
        throws JetspeedSecurityException
    {
        try
        {
            Role role = JetspeedSecurity.getRole(roleName);
            Permission permission = this.getPermission(permissionName);

            Criteria criteria = new Criteria();
            criteria.add(TurbineRolePermissionPeer.ROLE_ID, role.getId());
            criteria.add(TurbineRolePermissionPeer.PERMISSION_ID, permission.getId());
            TurbineRolePermissionPeer.doDelete(criteria);
            if (cachingEnable)
            {
                JetspeedSecurityCache.removePermission(roleName, permissionName);
            }
        }
        catch(Exception e)
        {
            throw new PermissionException("Revoke permission '" + permissionName + "' to role '" + roleName + "' failed: ", e);
        }

    }

    /**
     * Checks for the relationship of role has a permission. Returns true when the role has the given permission.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @param roleName grant a permission to this role.
     * @param permissionName the permission to grant to the role.
     * @exception PermissionException when the security provider has a general failure retrieving permissions.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public boolean hasPermission(String roleName, String permissionName)
        throws JetspeedSecurityException
    {
        List permissions;

        try
        {
            if (cachingEnable)
            {
                return JetspeedSecurityCache.hasPermission(roleName, permissionName);
            }

            Role role = JetspeedSecurity.getRole(roleName);
            Permission permission = this.getPermission(permissionName);

            Criteria criteria = new Criteria();
            criteria.add(TurbineRolePermissionPeer.ROLE_ID, role.getId());
            criteria.add(TurbineRolePermissionPeer.PERMISSION_ID, permission.getId());
            permissions = TurbineRolePermissionPeer.doSelect(criteria);

        }
        catch(Exception e)
        {
            throw new PermissionException("Failed to check permission '" +
                permissionName + "'", e);
        }
        return ( permissions.size() > 0 );
    }


    /**
     * Retrieves a single <code>Permission</code> for a given permissionName principal.
     *
     * The security service may optionally check the current user context
     * to determine if the requestor has permission to perform this action.
     *
     * @param permissionName a permission principal identity to be retrieved.
     * @return Permission the permission record retrieved.
     * @exception PermissionException when the security provider has a general failure.
     * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
     */
    public Permission getPermission(String permissionName)
        throws JetspeedSecurityException
    {
        List permissions;

        try
        {
            Criteria criteria = new Criteria();
            criteria.add(TurbinePermissionPeer.PERMISSION_NAME, permissionName);
            permissions = TurbinePermissionPeer.doSelect(criteria);
        }
        catch(Exception e)
        {
            throw new PermissionException("Failed to retrieve permission '" +
                permissionName + "'", e);
        }
        if ( permissions.size() > 1 )
        {
            throw new PermissionException(
                "Multiple Permissions with same permissionname '" + permissionName + "'");
        }
        if ( permissions.size() == 1 )
        {
            TurbinePermission permission = (TurbinePermission)permissions.get(0);
            return permission;
        }
        throw new PermissionException("Unknown permission '" + permissionName + "'");

    }


    ///////////////////////////////////////////////////////////////////////////
    // Internal
    ///////////////////////////////////////////////////////////////////////////

    protected JetspeedRunData getRunData()
     {
         JetspeedRunData rundata = null;
         if (this.runDataService != null)
         {
             rundata = this.runDataService.getCurrentRunData();
         }
         return rundata;
     }

    /**
     * Check whether a specified permission exists.
     *
     * The login name is used for looking up the account.
     *
     * @param permissionName the name of the permission to check for existence.
     * @return true if the specified account exists
     * @throws PermissionException if there was a general db access error
     *
     */
    protected boolean permissionExists(String permissionName)
        throws PermissionException
    {
        Criteria criteria = new Criteria();
        criteria.add(TurbinePermissionPeer.PERMISSION_NAME, permissionName);
        List permissions;
        try
        {
            permissions = TurbinePermissionPeer.doSelect(criteria);
        }
        catch(Exception e)
        {
            throw new PermissionException(
                "Failed to check account's presence", e);
        }
        if (permissions.size() < 1)
        {
            return false;
        }
        return true;
    }

    ///////////////////////////////////////////////////////////////////////////
    // Service Init
    ///////////////////////////////////////////////////////////////////////////


    /**
     * This is the early initialization method called by the
     * Turbine <code>Service</code> framework
     * @param conf The <code>ServletConfig</code>
     * @exception throws a <code>InitializationException</code> if the service
     * fails to initialize
     */
    public synchronized void init(ServletConfig conf)
        throws InitializationException
    {
        if (getInit()) return;

        super.init(conf);

        // get configuration parameters from Jetspeed Resources
        ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance())
                                                     .getResources(JetspeedSecurityService.SERVICE_NAME);

        this.runDataService =
           (JetspeedRunDataService)TurbineServices.getInstance()
               .getService(RunDataService.SERVICE_NAME);

        cascadeDelete = serviceConf.getBoolean( CASCADE_DELETE, DEFAULT_CASCADE_DELETE );
        cachingEnable = serviceConf.getBoolean( CACHING_ENABLE, cachingEnable );
        setInit(true);
     }



}



