2012-08-23 14 views
6

私は、Zend 2とDoctrine 2の認証に関するチュートリアルを探しています。 特に、コントローラとアダプタの作成。Zend 2 + doctrine 2認証アダプタ

公式文書はあまりにもグローバルではありません。

はあなたに感謝

編集:私は "教義エンティティ" を使用

(名前空間のユーザー\エンティティ;)

エンティティがmodule.config.phpファイルに登録している:

'doctrine' => array(
    'driver' => array(
     __NAMESPACE__ . '_driver' => array(
      'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 
      'cache' => 'array', 
      'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity') 
     ), 
     'orm_default' => array(
      'drivers' => array(
       __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' 
      ) 
     )   
    ), 
) 

しかし、私のIDClassキーをアダプタに向けるにはどうしたらいいですか? コントローラ:\ SRC \ DoctrineModule \オプション\ AuthenticationAdapter.php上の非オブジェクトで...教義\ドクトリンモジュール上のメンバ関数getRepository(の呼び出し):

use Zend\Mvc\Controller\AbstractActionController, 
    Zend\View\Model\ViewModel, 
    Zend\Authentication\AuthenticationService, 
    Doctrine\ORM\EntityManager, 
    DoctrineModule\Authentication\Adapter\ObjectRepository as DoctrineAdapter,   
    User\Entity\User, 
    User\Form\UserForm; 
class UserController extends AbstractActionController 
{ 
protected $em; 

public function setEntityManager(EntityManager $em) 
{ 
    $this->em = $em; 
} 

public function getEntityManager() 
{ 
    if (null === $this->em) 
     $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); 
    return $this->em; 
} 

public function getRepository() 
{ 
    if (null === $this->em) 
     $this->em = $this->getEntityManager()->getRepository('User\Entity\User'); 
    return $this->em; 
} 

public function loginAction() 
{ 
    .... 
    ???????????? 
    $adapter = new DoctrineAdapter(); 
    $adapter->setIdentityValue($username); 
    $adapter->setCredentialValue($password); 
    $auth = new AuthenticationService();  
    $result=$auth->authenticate($adapter); 
    ???????????? 

} 

} 

私はこのエラーを持っています行132 行123:$ this-> objectManager-> getRepository($ this-> identityClass)を返します。

答えて

15

多くの方法がありますが、DoctrineModule for zf2には、ドクトリンベースの認証アダプタ(DoctrineModule\Authentication\Adapter\ObjectRepository)が付属しています。アダプタを作成するファクトリもあります(DoctrineModule\Service\AuthenticationAdapterFactory)。 DoctrineMongoODMModuleは、これらのサービスを使用するように設定されたmodule.config.phpを持っています。 (工場とアダプタはORMで動作しますが、設定キーがDoctrineORMModuleに追加されているかどうかはまだわかりません - これを読んでいる人がPRを作成したいと思うでしょうか?)これは関連する設定キーです:

'authenticationadapter' => array(
     'odm_default' => array(
      'objectManager' => 'doctrine.documentmanager.odm_default', 
      'identityClass' => 'Application\Model\User', 
      'identityProperty' => 'username', 
      'credentialProperty' => 'password', 
      'credentialCallable' => 'Application\Model\User::hashPassword' 
     ), 
    ), 

identityClassは、認証されたユーザーを表すdoctrineドキュメントです。 identityPropertyは、通常はユーザー名です。 getUsernameがこれにアクセスするためにアダプタによって呼び出されます。 credentialPropertyは通常のパスワードです。 getPasswordがこれにアクセスするためにアダプタによって呼び出されます。 credentialCallableはオプションです。これは、credentialPropertyをハッシュするコール可能な(メソッド、静的メソッド、クロージャ)でなければなりません。これを行う必要はありませんが、通常は良いアイデアです。アダプターは、呼び出し可能コードの形式は次のようになります。function hashPassword($identity, $plaintext)

認証アダプタを使用取得するには、すべてこれが唯一のあなたにautheticationアダプタを与えること

$serviceLocator->get('doctrine.authenticationadapter.odm_default'); 

は注意を、それが実際に認証を行いません。認証は次のように行われます。

$adapter = $serviceLocator->get('doctrine.authenticationadapter.odm_default'); 
$adapter->setIdentityValue($username); 
$adapter->setCredentialValue($password); 
$authService = new Zend\Authentication\AuthenticationService 
$result = $authService->authenticate($adapter); 

これは、認証されたユーザーの教義文書全体をセッションオブジェクトに格納します。セッションオブジェクトにドキュメントIDのみを格納し、残りの自動化されたユーザードキュメントを取得する場合は、DBごとにリクエストを入力してから、DoctrineModule\Authentication\Storage\ObjectRepositoryを参照してください。これにより、Zend\Authentication\AuthenticationServiceの新しいStorageInterfaceが提供されます。

+0

こんにちは、ありがとう、ヒント。 DoctrineAdapterとしてDoctrineModule \ Authentication \ Adapter \ ObjectRepositoryを使用しようとしています。 しかし、$ result = $ adapter-> authenticate()を呼び出すと、このエラーが発生します。非オブジェクト上のメンバ関数getRepository()を呼び出します。どのようにして私のidentityClassを定義しますか? – beweed

+0

あなたのIDクラスはDoctrine Document(ODMの場合)またはDoctrine Entity(ORMの場合)でなければなりません。ドキュメント/エンティティと同じように定義します。 (他のすべてのドキュメント/エンティティの場合と同様に、 'module.config.php'にドライバキーを使ってドキュメント/エンティティを登録することを忘れないでください)。アイデンティティクラスを作成したら、そのアイデンティティクラスを指すために 'identityClass'キーを使います。上記の例では、アイデンティティクラスは 'Application \ Model \ User'ですが、あなたが好きなものを作ることができます。 – superdweebie

+0

本当にありがとうございます。私はちょうど私の問題を見つけた私の投稿を編集する。 – beweed

関連する問題