0

I have a class R00_Model_User, which, curiously enough, represents user as he is. Can $result->getIdentity() return me an object of this class? (Or maybe it's stupid?)

(There is a factory method in R00_Model_User which prevents from duplicating objects. I'd like Zend_Auth to use it instead of creating a new object, if it can)

4

2 回答 2

2

两种选择:

  • 编写您自己的身份验证适配器,将与您的场景最匹配的开箱即用适配器子类化

    class R00_Auth_Adapter extends Zend_Auth_Adapter_*
    {
        /**
         * authenticate() - defined by Zend_Auth_Adapter_Interface.  This method is called to
         * attempt an authentication.  Previous to this call, this adapter would have already
         * been configured with all necessary information to successfully connect to a database
         * table and attempt to find a record matching the provided identity.
         *
         * @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible
         * @return Zend_Auth_Result
         */
        public function authenticate()
        {
            $result = parent::authenticate();
            if ($result->isValid() {
                return new Zend_Auth_Result(
                    $result->getCode(),
                    R00_Model_User::load($result->getIdentity()),
                    $result->getMessages()
                );
            } else {
                return $result;
            }
        }
    }
    

    这将允许您编写代码

    $adapter = new R00_Auth_Adapter();
    //... adapter initialisation (username, password, etc.)
    $result = Zend_Auth::getInstance()->authenticate($adapter);
    

    并且在成功认证时,您的用户对象会自动存储在认证存储中(默认为会话)。

  • 或使用您的登录操作更新存储的用户身份

    $adapter = new Zend_Auth_Adapter_*();
    $result = $adapter->authenticate();
    if ($result->isValid()) {
        $user = R00_Model_User::load($result->getIdentity());
        Zend_Auth::getInstance()->getStorage()->write($user);
    }
    
于 2009-10-20T08:34:18.940 回答
0

在我的一个应用程序中,我让 getIdentity() 返回一个用户对象,它对我来说效果很好。要使用您的工厂方法,请执行以下操作:

$auth = Zend_Auth::getInstance();
$user = R00_Model_User::getInstance(...);
$auth->getStorage()->write($user);

然后,当您调用 getIdentity() 时,您将拥有您的用户对象。

于 2009-10-19T20:23:33.547 回答