dongtun3328 2012-12-29 06:30
浏览 7

too long

I am doing an authentication module using Zend_Auth in Zend Framework. After successful authentication I am storing users data using getStorage();

Example :

In method _process I have written like this :

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

print_r($user) showing the result :

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

Now, I want to display Logout link if user successfully logged in.

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>';
    }

}

But its not calling the method hasIdentity thats why its not going to the if block and

print_r($auth) showing output like this:

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);
    }
}

Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dongyuan1902 2012-12-29 08:40
    关注

    have you added session$myNamespace = new Zend_Session_Namespace(); to the action where you are writing $auth->getStorage()->write($user);,it should be the first line probably

    Example #1 Modifying the Session Namespace

    Zend_Auth_Storage_Session uses a session namespace of 'Zend_Auth'. This namespace may be overridden by passing a different value to the constructor of Zend_Auth_Storage_Session, and this value is internally passed along to the constructor of Zend_Session_Namespace. This should occur before authentication is attempted, since Zend_Auth::authenticate() performs the automatic storage of the identity.

    // 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

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)