1

我正在建立一个用户和公司可以加入的门户。用户可以是独立的,也可以在公司下工作。有一些基本访问权限可供所有用户使用,无论其类型如何(独立或与公司相关联)。还有一些功能可供独立用户使用,但如果用户属于公司,公司经理将能够允许/禁止他们访问特定功能。我如何使用 Zend_Acl 来管理它?

4

1 回答 1

1

你是ACL的可以有条件。

在我声明我的 ACL(顺便说一下,一个插件)的文件中,我有以下声明。Acl_AdminCanAccessUsers是 a并且将Zend_Acl_Assert_Interface返回TRUEFALSE。在这里,我还将请求对象传递给构造函数。

    // Complex function to see if the current user can create/edit the desired user account.
    $acl->allow('client', 'user', array('edit','create'), new Acl_AdminCanAccessUsers($this->_request));

现在让我们来看看Acl_AdminCanAccessUsers

<?php

class Acl_AdminCanAccessUsers implements Zend_Acl_Assert_Interface
{

public function  __construct($request) {
    $this->request = $request;
}

public function  assert(Zend_Acl $acl,
                        Zend_Acl_Role_Interface $role = null,
                        Zend_Acl_Resource_Interface $resource = null,
                        $privilege = null)
{

    /**
     * Can only test when trying to edit a user account
     */
    if ("edit" != $privilege) {
        return TRUE;
    }

    $identity = Zend_Auth::getInstance()->getIdentity();

    /**
     * Get the id from the URL request
     */
    $id = $this->request->getParam('id');

    /**
     * Get user account from DB
     */
    $user = Doctrine_Core::getTable('User')->find($id);

    // Are they editing their own account? Give them a pass
    if ($identity->user_id == $user->user_id) {
        return TRUE;
    }

    // If they don't have the isAdmin flag set to yes on their account
    // Then we'll just deny them immediately
    if ($identity->isAdmin) {
         return TRUE;
    }

    return FALSE;
}
}

正如您在此处看到的,我们正在检查数据库中的用户记录并将其与请求的参数进行比较,或者检查他们是否在 Zend_Auth 身份中设置了 isAdmin 标志。如果要访问的不仅仅是角色、资源和权限,您可以对 ACL 进行大量条件检查。

快乐编码!

于 2011-01-04T22:49:45.497 回答