Zend_Auth
Zend 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);
}
}
どんな助けでも大歓迎です。