2013-03-02 11 views
6

私はDoctrine 2とZF2を使って認証したいと思っています。助けを得るためにZend 2 + doctrine 2 Auth Adapterを使用しましたが、$authService->authenticate($adapter);と呼び出すたびに、クラス 'が存在しないというエラーが表示されます。Zend2 + Doctrine2認証

私のmodule.config.phpの設定は#t使用されたようです。

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

ただし、authServiceでvar_dumpを作成した場合、すべてのセットがnullになります。私は私が行います$ authAdapterからダンプ

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

を呼び出して、ログインをしたい私のサービスで は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のドキュメントを見れば、彼らは認証アダプタの構成を設定するには、次の使用:代わりにmodule.config.phpにauthenticationadapter使用authenticationを使用しての、だから、

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

。代わりにあなたのModule.phpでなどobjectManager使用object_manager


を使用して、あなたはこれを追加する必要があります。

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

オーケーIの変更を今は 'Zend \ ServiceManager \ ServiceManager :: getはdoctrine.authentication.orm_default'のインスタンスを取得または作成できませんでした。 getterをpublic function getAuthAdapter()に変更します。 { if(null === $ this-> authAdapter){ $ this-> authAdapter = $ this-> getServiceManager() -> get( 'doctrine.authentication 。orm_default '); } return $ this-> authAdapter; ' –

+0

更新された回答を参照 – hohner

+0

ありがとうございました。今問題が残っているのは1つだけです。 '$ authResult = $ authService-> authenticate();の後に空のページがあります。エラーなし、何もありません。 –

0

Hohnerが提案したようにModule.phpで 'Zend \ Authentication \ AuthenticationService'を使用している場合、これはBjyAuthorizeモジュールの役割とACLでは機能しません。 BjyAuthorizeはデフォルトで、 'ZfcUser \ Authentication \ Storage \ Db'を使用する認証サービスのデフォルト設定になります。 BjyAuthorizeはと交換してください(または追加)Doctrineのアイデンティティを使用して取得するには「zfcuser_auth_service」次のように:

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

その後も同様の方法で、コントローラでそれを使用します。

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