turbinepermissionmanagement.java

来自「jetspeed源代码」· Java 代码 · 共 576 行 · 第 1/2 页

JAVA
576
字号
/*
 * Copyright 2000-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

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

import javax.servlet.ServletConfig;


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

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.JetspeedSecurityService;

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

// 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;

// Jetspeed logging 
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;

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

// 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.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.10 2004/02/23 03:54:49 jford Exp $
 */

public class TurbinePermissionManagement extends TurbineBaseService
                                   implements PermissionManagement
{
    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(TurbinePermissionManagement.class.getName());
    
    private JetspeedRunDataService runDataService = null;
    private final static String CASCADE_DELETE = "programmatic.cascade.delete";
    private final static boolean DEFAULT_CASCADE_DELETE = true;
    private final static String CONFIG_SYSTEM_PERMISSIONS = "system.permissions";
    private boolean cascadeDelete;
    private final static String CACHING_ENABLE = "caching.enable";
    private boolean cachingEnable = true;
    private Vector systemPermissions = null;

    ///////////////////////////////////////////////////////////////////////////
    // 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)
        {
            logger.error( "Failed to Retrieve Role: ", 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)
        {
            logger.error( "Failed to retrieve permissions ", 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)
        {
            logger.error( "Failed to retrieve permissions ", 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)
        {
            String message = "Failed to create permission '" + permission.getName() + "'";
            logger.error( message, e );
            throw new PermissionException(message, 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)
        {
            String message = "Failed to create permission '" + permission.getName() + "'";
            logger.error( message, e );
            throw new PermissionException( message, 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
        {

            if (systemPermissions.contains(permissionName))
            {
                throw new PermissionException("[" + permissionName + "] is a system permission and cannot be removed");
            }

            conn = Torque.getConnection();
            Permission permission = this.getPermission(permissionName);

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

            if(cascadeDelete)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?