【问题标题】:Zend Acl - is it possible for an assertion to allow access to a privilege when the acl rules return false?Zend Acl - 当 acl 规则返回 false 时,断言是否可以允许访问特权?
【发布时间】:2011-08-09 12:59:58
【问题描述】:

默认情况下,即使 acl 规则允许,在断言中返回 false 也会拒绝访问。我想要做的是相反的方式 - 即在 acl 规则拒绝时在断言中返回 true 以允许访问。

例如:

class TestAssert implements Zend_Acl_Assert_Interface
{
    public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $role = null, Zend_Acl_Resource_Interface $resource = null,
                           $privilege = null)
    {
        return true;
    }
}

$acl = new Zend_Acl;
$acl->addResource('page');
$acl->addRole('user');

$acl->allow('user', 'page', 'read', new TestAssert);
$acl->isAllowed('user', 'page', 'write');

上面的 isAllowed() 将返回 false,因为我询问的权限在 acl 规则中是不允许的,因此它没有达到断言的程度。

我想要的可能吗?

我想这样做的原因是用户可能对所有页面都具有某些权限,然后对他们创建的页面具有不同的权限集 - 在查询权限时,我希望能够判断页面是否是他们的,如果是,则应用“自己的页面”而不是“页面”的规则。很明显,在真正的应用中,传递的是用户和页面对象,而不是字符串。

更新:我已经通过继承 Zend_Acl 来完成我想做的事情。

此类允许为特定角色和资源组合添加回调。调用 isAllowed() 时,将运行回调(如果存在)。它以与断言相同的参数传递(以及 parent::isAllowed() 的结果)。这个回调的返回值是从isAllowed()返回的,除非为null,这种情况下使用正常的返回值。

class Acl extends Zend_Acl
{
    protected $_callbacks = array();

    /**
     * Takes into account the callback to determine whether the role is allowed access
     * 
     * @param Zend_Acl_Role_Interface $role
     * @param Zend_Acl_Resource_Interface $resource
     * @param string $privilege
     * @return bool
     */
    public function isAllowed($role = null, $resource = null, $privilege = null)
    {
        $result = parent::isAllowed($role, $resource, $privilege);
        $role = $this->getRole($role)->getRoleId();
        $resource = $this->get($resource)->getResourceId();

        if(isset($this->_callbacks[$role][$resource]))
        {           
            $callbackResult = call_user_func($this->_callbacks[$role][$resource], $this, $role, $resource, $privilege, $result);
        }

        return isset($callbackResult) ? $callbackResult : $result;
    }

    /**
     * Add a callback for a specific role and resource combination.
     * If the callback returns a value, this is used as the return value for isAllowed().
     * Otherwise, if the callback returns null, the normal result of isAllowed() will be used.
     * 
     * @param Zend_Acl_Role_Interface $role
     * @param Zend_Acl_Resource_Interface $resource
     * @param callback $callback 
     */
    public function addCallback($role, $resource, $callback)
    {
        $role = $this->getRole($role)->getRoleId();
        $resource = $this->get($resource)->getResourceId();
        $this->_callbacks[$role][$resource] = $callback;
    }
}

用法:

$acl = new Acl;
$acl->addResource('page');
$acl->addRole('user');

$acl->addCallback('user', 'page', function($acl, $role, $resource, $privilege, $result)
{
    return true;
});


$acl->allow('user', 'page', array('read'));
$acl->isAllowed('user', 'page', 'edit'); //returns true even though 'edit' is not allowed.

【问题讨论】:

    标签: php zend-framework acl


    【解决方案1】:

    听起来你想要一些类似的东西:

    class WriteAssert implements Zend_Acl_Assert_Interface
    {
        public function assert(Zend_Acl $acl,
                               Zend_Acl_Role_Interface $role = null,
                               Zend_Acl_Resource_Interface $resource = null,
                               $privilege = null)
        {
            // return whether the user owns the page
        }
    }
    
    $acl = new Zend_Acl;
    $acl->addResource('page');
    $acl->addRole('user');
    
    $acl->allow('user', 'page', 'read');
    $acl->allow('user', 'page', 'write', new WriteAssert);
    $acl->isAllowed('user', 'page', 'write');
    

    在这里,您希望用户能够阅读任何页面,但只能写入自己的页面。

    【讨论】:

    • 问题是,我希望自己的页面规则成为不同的资源(因为它们将存储在数据库中并从管理面板进行编辑)。因此,例如,当在页面上调用 isAllowed() 以查看是否允许写访问时,如果用户拥有该页面,我希望测试“自己的页面”资源而不仅仅是“页面”。据我所知,没有办法用断言来做到这一点
    猜你喜欢
    • 2017-04-12
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多