2013-03-02 48 views
6

我想使用Doctrine 2和ZF2進行身份驗證。爲了得到一些幫助,我使用了Zend 2 + doctrine 2 Auth Adapter,但是每次我打電話給$authService->authenticate($adapter);時,都會收到類「'不存在的錯誤。Zend2 + Doctrine2身份驗證

看來,我的module.config.php配置不會被使用。它顯示是這樣的:

'authenticationadapter' => array(
     'orm_default' => array(
      'objectManager' => 'doctrine.entitymanager.orm_default', 
      'identityClass' => 'Profile\Entity\User', 
      'identityProperty' => 'username', 
      'credentialProperty' => 'password', 
     ), 
    ), 

但是,如果我在authService上做了var_dump,所有集都是空的。 在我的服務,我想要做的登錄我打電話

$authAdapter = $this->getAuthAdapter(); 
$authAdapter->setIdentityValue($username); 
$authAdapter->setCredentialValue($password); 

從$ authAdapter轉儲告訴我,IdentityValue和CredentialValue 設置正確。

在$ authAdabter其他的事情是:

protected 'options' => 
    object(DoctrineModule\Options\Authentication)[281] 
     protected 'objectManager' => 
     object(Doctrine\ORM\EntityManager)[248] 
      private 'config' => 
      object(Doctrine\ORM\Configuration)[250] 
       ... 
      private 'conn' => 
      object(Doctrine\DBAL\Connection)[252] 
       ... 
      private 'metadataFactory' => 
      object(Doctrine\ORM\Mapping\ClassMetadataFactory)[266] 
       ... 
      private 'repositories' => 
      array (size=0) 
       ... 
      private 'unitOfWork' => 
      object(Doctrine\ORM\UnitOfWork)[267] 
       ... 
      private 'eventManager' => 
      object(Doctrine\Common\EventManager)[242] 
       ... 
      private 'hydrators' => 
      array (size=0) 
       ... 
      private 'proxyFactory' => 
      object(Doctrine\ORM\Proxy\ProxyFactory)[268] 
       ... 
      private 'expressionBuilder' => null 
      private 'closed' => boolean false 
      private 'filterCollection' => null 
     protected 'objectRepository' => null 
     protected 'identityClass' => null 
     protected 'identityProperty' => null 
     protected 'credentialProperty' => null 
     protected 'credentialCallable' => null 
     protected 'classMetadata' => null 
     protected 'storage' => null 
     protected '__strictMode__' => boolean true 
    protected 'authenticationResultInfo' => null 

的getAuthAdapter這樣表示:

public function getAuthAdapter() 
{ 
    if (null === $this->authAdapter) { 
     $this->authAdapter = $this->getServiceManager() 
      ->get('doctrine.authenticationadapter.ormdefault'); 
    } 
    return $this->authAdapter; 
} 

這樣可以有一個人告訴我如何正確設置的選項?

回答

10

看起來您正在使用錯誤的配置值。如果你看一下DoctrineORM文檔,它們使用下列設置配置了認證適配器:

'doctrine' => array(
    'authentication' => array(
     'orm_default' => array(
      'object_manager' => 'Doctrine\ORM\EntityManager', 
      'identity_class' => 'Application\Entity\User', 
      'identity_property' => 'email', 
      'credential_property' => 'password', 
     ), 
    ), 
) 

因此,而不是使用您的module.config.php authenticationadapter使用authentication;而是採用objectManager使用object_manager


在你Module.php,你將需要補充一點:

use Zend\Authentication\AuthenticationService; 

... 

public function getServiceConfig() 
{ 
    return array(
     'factories' => array(
      'Zend\Authentication\AuthenticationService' => function($serviceManager) { 
       return $serviceManager->get('doctrine.authenticationservice.orm_default'); 

      } 
     ) 
    ); 
} 

而在你的控制器,可以使用訪問適配器:

$authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); 

$adapter = $authService->getAdapter(); 
$adapter->setIdentityValue($data['login']); 
$adapter->setCredentialValue($data['password']); 

請按照documentation

+0

好吧,我改:

'zfcuser_auth_service' => function ($serviceManager) { return $authenticationService = $serviceManager->get('doctrine.authenticationservice.orm_default'); }, 

然後你也以類似的方式使用它在控制器但現在它告訴我'Zend \ ServiceManager \ ServiceManager :: get無法爲doctrine.authentication.orm_default提取或創建一個實例。如果(null === $ this-> authAdapter){this-> authAdapter = $ this-> getServiceManager() - > get('doctrine.authentication()),則將getter改爲'public function getAuthAdapter() { 。orm_default'); } return $ this-> authAdapter;' – 2013-03-02 15:28:01

+0

查看更新的答案 – hohner 2013-03-02 15:32:15

+0

感謝您的幫助。現在只剩下一個問題了。在'$ authResult = $ authService-> authenticate();'後我得到一個空白頁面。沒有錯誤,沒有。 – 2013-03-02 15:56:32

0

如果按照Hohner的建議在Module.php中使用'Zend \ Authentication \ AuthenticationService',這將不適用於BjyAuthorize模塊角色和ACL。 BjyAuthorize將默認爲使用'ZfcUser \ Authentication \ Storage \ Db'的身份驗證服務的默認配置。爲了得到BjyAuthorize使用學說的身份與更換(或增加)「zfcuser_auth_service」如下:

$authService = $this->getServiceLocator()->get('zfcuser_auth_service');