duannuo7878
2013-04-19 07:14 阅读 19
已采纳

如何将数据附加到Zend_Auth对象

Good Day to All,

I have the following code which appends stores data to Zend_Auth object

    $auth        = Zend_Auth::getInstance();

    $dbAdapter   = Zend_Db_Table::getDefaultAdapter();

    $authAdapter = new Zend_Auth_Adapter_DbTable(
    $dbAdapter,
                'account', 
                'email', 
                'password',
                'delete_flag=0'
    );
    //MD5(?) AND  .. add this along with the prev where condn of delete flag...

    $authAdapter->setIdentity($loginDataArray['email'])
    ->setCredential($loginDataArray['password']);

    $result = $auth->authenticate($authAdapter); 
    var_dump($result);

    if ($result->isValid()) {

        $authStorage = $auth->getStorage();

        // the details you wan to store in the session
        $userDetails = array();         

        $userDetails['account_id']    = $account_id;
        $userDetails['email']      = $loginDataArray['email'];

        $authStorage->write($userDetails);
}

Now, How do i append any more data in the later part of the session. How do i edit the same Zend_Auth object later.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dtysql0586 dtysql0586 2013-04-19 07:54

    Authentication state is stored in the registered Auth Storage. By default this is Zend_Session. You can get session by this

    $namespace = new Zend_Session_Namespace('Zend_Auth');
    

    then do somthing like this

    $namespace->newname = "newvalue";
    
    点赞 评论 复制链接分享
  • douguanya6399 douguanya6399 2013-04-20 08:17

    ok, you don't 'edit' Zend_Auth Identity. You either have an identity or not. You can set, read, write or clear storage through the Zend_Auth object.

    However many of us use this same data for various display purposes so a solution that often works is to set the data to a different session namespace or a registry key when the identity is being stored or just update the session that Zend_Auth creates as chandresh_ cool suggests.

    if ($result->isValid()) {
    
            $authStorage = $auth->getStorage();
    
            // the details you wan to store in the session
            $userDetails = array();         
    
            $userDetails['account_id']    = $account_id;
            $userDetails['email']      = $loginDataArray['email'];
            //add user data to registry
            $user = Zend_Registry::set('user', $userDetails);
    
            $authStorage->write($userDetails);
    }
    

    if you really want to do your own thing you can write your own storage adapter by implementing Zend_Auth_Storage_Interface or you can write your own Auth adapter by implementing Zend_Auth_Adapter_Interface and include the storage component in the adapter.

    Lots of choices, good luck.

    点赞 评论 复制链接分享

相关推荐