1

我正在尝试使用 1.11 框架链接来遵循 Zend Auth 和 Zend Acl 的教程!

我已经成功设置了身份验证,并且能够使用 Acl.php 页面中给出的控制器::操作对的身份验证。首先,我想测试用户表上的两个附加参数,即用户帐户是否已激活以及用户在允许访问该站点之前是否被管理员禁止。我如何在这段代码中实现它。

其次,我想知道如何将一个控制器下的所有操作包含到用户授权级别。即我有一个主控制器,它在它下面有许多针对各种表的操作。您能否告诉我如何将访问 Masters 控制器的所有操作仅限于管理员角色。无需添加资源并允许 Acl.php 中的每个操作使用资源。另外请告诉我是否可以扩展此逻辑以允许访问整个模块而不仅仅是控制器(通过一个添加资源并允许资源)?如果是怎么办?

4

1 回答 1

0

首先,我想测试用户表上的两个附加参数,即用户帐户是否已激活以及用户在允许访问该站点之前是否被管理员禁止。

教程代码使用香草版本,Zend_Auth_Adapter_DbTable它使用特定的 api 进行身份验证。要让 Zend_Auth 以您希望的方式工作并不是很困难,但需要一些思考,因为您需要实现Zend_Auth_Adapter_Interface. 听起来更糟,您只需要实现该authenticate()方法。下面是一个可用于代替 auth 适配器的示例Zend_Auth_Adapter_DbTable

<?php
//some code truncated for length and relevance
class My_Auth_Adapter implements Zend_Auth_Adapter_Interface
{

    protected $identity = null;

    protected $credential = null;

    protected $usersMapper = null;


    public function __construct($username, $password, My_Model_Mapper_Abstract $userMapper = null)
    {
        if (!is_null($userMapper)) {
            $this->setMapper($userMapper);
        } else {
            $this->usersMapper = new Users_Model_Mapper_User();
        }
        $this->setIdentity($username);
        $this->setCredential($password);
    }

    /**
     * @return \Zend_Auth_Result
     */
    public function authenticate()
    {
        // Fetch user information according to username
        $user = $this->getUserObject();

        if (is_null($user)) {
            return new Zend_Auth_Result(
                    Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND,
                    $this->getIdentity(),
                    array('Invalid username')
            );
        }
        // check whether or not the hash matches
        $check = Password::comparePassword($this->getCredential(), $user->password);
        if (!$check) {
            return new Zend_Auth_Result(
                    Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
                    $this->getIdentity(),
                    array('Incorrect password')
            );
        }
        // Success!
        return new Zend_Auth_Result(
                Zend_Auth_Result::SUCCESS,
                $this->getIdentity(),
                array()
        );
    }


   // public function setIdentity($userName)

   // public function setCredential($password)

   // public function setMapper($mapper)


    /**
     * @return object
     */
    private function getUserObject()
    {
        return $this->getMapper()->findOneByColumn('username', $this->getIdentity());
    }

    /**
     * @return object
     */
    public function getUser()
    {
        $object = $this->getUserObject();
        $array = array(
            'id'   => $object->id,
            'username' => $object->username,
            'role' => $object->getRoleId()
        );
        return (object) $array;
    }
   // public function getIdentity()

   //  public function getCredential()

   // public function getMapper()

}

你可以修改 auth 适配器来做你需要的任何事情。

就您的访问列表而言,要记住的是您的资源是由字符串定义的。在本教程中,资源定义为:

$this->add(new Zend_Acl_Resource('error::error'));

其中冒号左侧的字符串代表控制器,冒号右侧的字符串代表动作。正是 acl 插件中的这一行告诉我们资源是什么:

if(!$acl->isAllowed($user->role, $request->getControllerName() . '::' . $request->getActionName()))

您可以将资源所代表的定义更改为适合您的任何内容。

很难提供关于如何实现 ACL 的硬性规则,因为似乎每个项目都需要不同的东西。

环顾网络,您会发现 Zend Framework ACL 的几种不同实现,其中一些可能非常复杂。

这是一个可能提供更多见解的方法。http://codeutopia.net/blog/2009/02/06/zend_acl-part-1-misconceptions-and-simple-acls/

祝你好运

于 2013-05-02T14:27:11.010 回答