0

私はクラスR00_Model_Userを持っています。これは、不思議なことに、ユーザーをそのまま表します。$ result-> getIdentity()はこのクラスのオブジェクトを返すことができますか?(または多分それは愚かですか?)

(R00_Model_Userには、オブジェクトの複製を防ぐファクトリメソッドがあります。可能であれば、新しいオブジェクトを作成する代わりに、Zend_Authで使用したいと思います)

4

2 に答える 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);
    

    認証が成功すると、ユーザーオブジェクトは自動的に認証ストレージに保存されます(デフォルトではセッション)。

  • または、ログインアクションを使用して、保存されているユーザーIDを更新します

    $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

私のアプリケーションの1つでは、getIdentity()がユーザーオブジェクトを返すようにしていますが、これは非常にうまく機能します。ファクトリメソッドを使用するには、次のようにします。

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

次に、getIdentity()を呼び出すと、ユーザーオブジェクトが作成されます。

于 2009-10-19T20:23:33.547 に答える