2013-03-02 20 views
6

Tôi muốn có xác thực bằng Doctrine 2 và ZF2. Để nhận được một số trợ giúp tôi đã sử dụng Zend 2 + doctrine 2 Auth Adapter, nhưng mỗi khi tôi gọi số $authService->authenticate($adapter); tôi đều gặp lỗi khi lớp học 'không tồn tại.Xác thực Zend2 + Doctrine2

Có vẻ như cấu hình từ module.config.php của tôi đã thắng # t được sử dụng. Nó hiển thị như sau:

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

Nhưng nếu tôi thực hiện một var_dump trên authService tất cả các bộ là rỗng. Trong dịch vụ của tôi, nơi tôi muốn làm đăng nhập tôi gọi

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

Một bãi từ $ authAdapter nói với tôi rằng IdentityValue và CredentialValue được thiết lập một cách chính xác.

Những điều khác trong $ authAdabter là:

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 

Các getAuthAdapter thấy như thế này:

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

Vì vậy, có thể một số một cho tôi biết làm thế nào để thiết lập các tùy chọn một cách chính xác?

Trả lời

10

Có vẻ như bạn đang sử dụng sai giá trị cấu hình. Nếu bạn nhìn vào các tài liệu DoctrineORM, họ sử dụng những điều sau đây để thiết lập cấu hình cho adapter xác thực:

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

Vì vậy, thay vì sử dụng authenticationadapter sử dụng authentication trong module.config.php của bạn; thay vì sử dụng objectManager sử dụng object_manager vv


Trong Module.php của bạn, bạn sẽ cần phải thêm này:

use Zend\Authentication\AuthenticationService; 

... 

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

      } 
     ) 
    ); 
} 

Và trong điều khiển của bạn, bạn có thể truy cập vào Adapter sử dụng:

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

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

Vui lòng theo dõi documentation.

+0

Được rồi tôi thay đổi nhưng bây giờ nó cho tôi biết 'Zend \ ServiceManager \ ServiceManager :: get không thể lấy hoặc tạo một cá thể cho doctrine.authentication.orm_default'. Tôi thay đổi getter thành 'hàm public getAuthAdapter() { if (null === $ this-> authAdapter) { $ this-> authAdapter = $ this-> getServiceManager() -> get ('doctrine.authentication .orm_default '); } trở $ this-> authAdapter; ' –

+0

Xem cập nhật câu trả lời – hohner

+0

Nhờ sự giúp đỡ của bạn. Bây giờ chỉ còn một vấn đề. Sau '$ authResult = $ authService-> authenticate();' tôi nhận được một trang trắng tinh. Không có lỗi, không có gì. –

0

Nếu bạn đang sử dụng 'Zend \ Authentication \ AuthenticationService' trong Module.php như đề xuất của Hohner, điều này sẽ không hoạt động với vai trò Mô-đun BjyAuthorize và ACL. BjyAuthorize sẽ mặc định cấu hình mặc định của riêng dịch vụ xác thực sử dụng 'ZfcUser \ Authentication \ Storage \ Db'. Để có được BjyAuthorize sử dụng danh tính thuyết thay thế nó bằng (hoặc thêm) 'zfcuser_auth_service' như sau:

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

Sau đó, bạn cũng sử dụng nó trong bộ điều khiển theo cách tương tự:

$authService = $this->getServiceLocator()->get('zfcuser_auth_service'); 
Các vấn đề liên quan