ldappermissionmanagement.java

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

JAVA
473
字号
        }
    }

    /**
     * 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
    {
        BasicAttributes attr = new BasicAttributes();
        LDAPRole role;
        LDAPPermission permission;

        try
        {
            role = (LDAPRole)JetspeedSecurity.getRole(roleName);
            permission = (LDAPPermission)JetspeedSecurity.getPermission(permissionName);

            role.addRolePermissions(permissionName);
            role.update(false);

            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
    {
        BasicAttributes attr= new BasicAttributes();
        LDAPRole role;
        Vector userurls;

        try
        {
            userurls = JetspeedLDAP.search(JetspeedLDAP.buildURL("ou=roles"),
                       "(&(uid="+ roleName+")(objectclass=jetspeedrole))", ATTRS, true);

            if (userurls.size() == 0)
            {
            	throw new PermissionException("Role '" + roleName + "' does not exist!");
            }
            else
            {
                role = new LDAPRole((LDAPURL) ((Vector)userurls.elementAt(0)).firstElement());
                role.getRolePermissions().remove(permissionName);
                role.update(false);

                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
    {
        BasicAttributes attr= new BasicAttributes();
        LDAPRole role;
        Vector userurls;

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

            userurls = JetspeedLDAP.search(JetspeedLDAP.buildURL("ou=roles"),
                       "(&(uid="+ roleName+")(objectclass=jetspeedrole))", ATTRS, true);

            if (userurls.size() > 0)
            {
                role = new LDAPRole((LDAPURL) ((Vector)userurls.elementAt(0)).firstElement());
                return role.permissionExists(permissionName);
            }
        }
        catch(Exception e)
        {
            throw new PermissionException("Grant permission '" + permissionName + "' to role '" + roleName + "' failed: ", e);
        }
        return false;
    }

    /**
     * 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
    {
        if (permissionExists(permissionName))
        {
            return new LDAPPermission(permissionName, false);
        }
        else
        {
            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
    {
        BasicAttributes attr= new BasicAttributes();
        Vector permissionurls;

        try
        {
            permissionurls = JetspeedLDAP.search(JetspeedLDAP.buildURL("ou=permissions"),
                             "(&(uid=" + permissionName + ")(objectclass=jetspeedpermission))", ATTRS, true);

            if (permissionurls.size() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new PermissionException("Failed to retrieve permission ", e);
        }
    }

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

⌨️ 快捷键说明

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