0

Zend_AuthZend Frameworkを使用して認証モジュールを実行しています。認証が成功した後、次を使用してユーザーデータを保存していgetStorage()ます。

例 :

メソッド_processで私はこのように書いています:

if ($result->isValid()) {                        
    $user = $adapter->getResultRowObject();             
    $auth->getStorage()->write($user);
    return true;
}

print_r($user)結果を表示する:

stdClass Object ( [id] => 1 [username] => admin [password] => cb3aefbdffbc81588f3d43c394428b16d4346b44 [salt] => ce8d96d579d389e783f95b3772785783ea1a9854 [role] => administrator [date_created] => 2012-12-29 11:04:40) 

Logoutここで、ユーザーが正常にログインした場合にリンクを表示したいと考えています。

LoggedInAs.php

    class Zend_View_Helper_LoggedInAs extends Zend_View_Helper_Abstract {

    public function loggedInAs() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $username = $auth->getIdentity()->username;
            $logoutUrl = $this->view->url(array('controller' => 'auth',
                'action' => 'logout'), null, true);
            return 'Welcome ' . $username . '. <a href="' . $logoutUrl . '">Logout</a>';
        }

        $request = Zend_Controller_Front::getInstance()->getRequest();
        $controller = $request->getControllerName();
        $action = $request->getActionName();
        if ($controller == 'auth' && $action == 'index') {
            return '';
        }
        $loginUrl = $this->view->url(array('controller' => 'login', 'action' => 'index'));
        return '<a href="' . $loginUrl . '">Login</a>';
    }

}

しかし、メソッドを呼び出していhasIdentityないため、ifブロックに移動せず、

print_r($auth)次のような出力を表示します。

Zend_Auth Object ( [_storage:protected] => )

Bootstrap.php

protected function _initSession() {
    Zend_Session::start();
    if (!Zend_Registry::isRegistered('session')) {
        $session = new Zend_Session_Namespace('userIdentity');
        Zend_Registry::set('session', $session);
    }
}

どんな助けでも大歓迎です。

4

1 に答える 1

0

$myNamespace = new Zend_Session_Namespace();あなたが書いているアクションにセッションを追加しましたか$auth->getStorage()->write($user);、それはおそらく最初の行になるはずです

例1セッション名前空間の変更

Zend_Auth_Storage_Sessionは、「Zend_Auth」のセッション名前空間を使用します。この名前空間は、Zend_Auth_Storage_Sessionのコンストラクターに別の値を渡すことでオーバーライドでき、この値はZend_Session_Namespaceのコンストラクターに内部的に渡されます。Zend_Auth :: authenticate()はIDの自動保存を実行するため、これは認証が試行される前に発生する必要があります。

// Use 'someNamespace' instead of 'Zend_Auth'
$auth->setStorage(new Zend_Auth_Storage_Session('userIdentity'));
$result = $auth->authenticate($authAdapter);

http://framework.zend.com/manual/1.12/en/zend.auth.introduction.html

于 2012-12-29T08:40:19.900 に答える