2012-08-02 61 views
0

我打算在未来的项目中使用ZF2,所以我现在尝试使用Zend Framework 2 RC1。我开始使用身份验证步骤,并注意到当我为会话存储名称空间选择了不同于'Zend_Auth'的名称时,我无法访问存储在会话中的对象(AuthenticationService类'hasIdentity方法返回false,尽管会话中的数据集设置了会话)。使用不同的会话命名空间和Zend Framework 2身份验证组件

<?php 
namespace Application\Controller; 

use Zend\Authentication\Adapter\DbTable as AuthAdapter; 
use Zend\Authentication\AuthenticationService; 
use Zend\Authentication\Storage\Session as SessionStorage; 
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Application\Model\User; 
use Application\Form\LoginForm; 

class LoginController extends AbstractActionController 
{ 
    public function indexAction() 
    { 
     $auth = new AuthenticationService(); 

     if ($auth->hasIdentity()) { 
      return $this->redirect()->toRoute('application'); 
     } 

     $form = new LoginForm(); 
     return array('form' => $form); 
    } 

    public function loginAction() 
    { 
     $auth = new AuthenticationService(); 

     $form = new LoginForm(); 
     $form->get('submit')->setAttribute('value', 'Add'); 

     $request = $this->getRequest(); 

     if ($request->isPost()) { 
      $user = new User(); 
      $form->setInputFilter($user->getInputFilter('login')); 

      $form->setData($request->getPost()); 
      if ($form->isValid()) { 
       $data = $form->getData(); 

       // Configure the instance with constructor parameters... 
       $sm   = $this->getServiceLocator(); 
       $dbAdapter = $sm->get('db-adapter'); 
       $authAdapter = new AuthAdapter($dbAdapter, 'users', 'username', 'password'); 

       $authAdapter 
        ->setIdentity($data['username']) 
        ->setCredential(sha1($data['password'])); 

       // Use 'users' instead of 'Zend_Auth' 
       $auth->setStorage(new SessionStorage('users')); 

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

       if ($result->isValid()) { 
        // store the identity as an object where only the username and 
        // real_name have been returned 
        $storage = $auth->getStorage(); 

        // store the identity as an object where the password column has 
        // been omitted 
        $storage->write($authAdapter->getResultRowObject(
         null, 
         'password' 
        )); 

        // Redirect to list of application 
        return $this->redirect()->toRoute('application'); 
       } 
      }  
     } 

     // processed if form is not valid 
     return array('form' => $form); 
    } 
} 

在该代码中,当我改变了下面线,

$auth->setStorage(new SessionStorage('users')); 

这样的:

$auth->setStorage(new SessionStorage()); 

hasIdentity方法返回真。

我检查了两个类Zend \ Authentication \ AuthenticationService和Zend \ Authentication \ Storage \ Session,但没有看到访问具有不同于默认会话名称空间的会话数据的方法。

我需要了解的是如何访问具有不同命名空间的会话数据,并且如果现在没有办法执行此操作,应该将其定义为一个错误吗?

我可以更新问题是否需要其他信息。

回答

2

我们有点遗憾你的代码的一部分,你尝试并接收用户身份的部分。即时通讯猜测你忘记了将SessionStorage对象与相同的命名空间一起传递。

另外认证对象的配置应该移动到工厂,以便这些问题不会出现。

这就是我的五美分atleast :)

+0

安托万hedgecock,对不起,我迟到的答复。我尝试和接收用户身份的代码是indexAction代码。是的,当我尝试使用不同的名称空间时,我没有传递SessionStorage对象。现在我想我们必须传递SessionStorage对象才能使用与默认_Zend_Auth_命名空间不同的名称空间(正如您在答案中所述),这是真的吗?有没有其他简单的方法来做到这一点,也许将名字空间字符串传递给AuthenticationService类? – azer 2012-08-07 07:35:48