【问题标题】:Zend Authentication where user role stored in separate db tableZend 身份验证,其中用户角色存储在单独的数据库表中
【发布时间】:2013-03-08 17:16:53
【问题描述】:

我有一个存储用户 ID、用户名和密码的用户表和一个包含用户角色的角色表。为了链接这两个表,我有一个包含 userID 和 roleID 的表 (user_role)。如何使用 Zend Auth 对用户进行身份验证并使用 Zend Acl 控制用户访问。 This is the database design

【问题讨论】:

  • @TimFountain 我在 YouTube 上关注 zf_turoial。问题是 Zend_Auth_Adapter_DbTable 的瞬间需要一个数据库表。该表需要有一列存储用户角色,但在我的数据库中,用户角色位于单独的表中。

标签: php zend-framework zend-db-table


【解决方案1】:

您可以创建一个 Zend_Auth 适配器,它适用于您的应用程序的任何结构。

这是一个 Auth 适配器的示例,它使用我的实体模型和映射器来提供凭据和用户数据以进行身份​​验证。

<?php

/**
 * Description of Auth_Adapter
 *
 */
class Auth_Adapter implements Zend_Auth_Adapter_Interface
{
    /**
     * The username
     *
     * @var string
     */
    protected $identity = null;
    /**
     * The password
     *
     * @var string
     */
    protected $credential = null;
    /**
     * Users database object
     *
     * @var Model_Mapper_Abstract
     */
    protected $usersMapper = null;

    /**
     * @param string $username
     * @param string $password
     * @param Model_Mapper_Abstract $userMapper
     */
    public function __construct($username, $password, Model_Mapper_Abstract $userMapper = null)
    {
        if (!is_null($userMapper)) {
            $this->setMapper($userMapper);
        } else {
            $this->usersMapper = new Application_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 using my own password class
        $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()
        );
    }

    /**
     * @param type $userName
     * @return \Auth_Adapter
     */
    public function setIdentity($userName)
    {
        $this->identity = $userName;
        return $this;
    }

    /**
     * @param type $password
     * @return \Auth_Adapter
     */
    public function setCredential($password)
    {
        $this->credential = $password;
        return $this;
    }

    /**
     * @param type $mapper
     * @return \Auth_Adapter
     */
    public function setMapper($mapper)
    {
        $this->usersMapper = $mapper;
        return $this;
    }

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

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

    /**
     * @return string
     */
    public function getIdentity()
    {
        return $this->identity;
    }

    /**
     * @return string
     */
    public function getCredential()
    {
        return $this->credential;
    }

    /**
     * @return object Model_Mapper_Abstract
     */
    public function getMapper()
    {
        return $this->usersMapper;
    }
}

您还可以扩展任何当前的适配器以提供您需要的功能。

祝你好运!

【讨论】:

  • 感谢@RockyFord 的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多