0

初心者向けの包括的なドキュメントは事実上存在しないため、私はZend2用のDoctrineModuleのソースを介して自分の道を歩もうとしています。

そこで、カスタム認証アダプターObjectRepositoryを見つけました。このクラスは、 DoctrineModule \ Options\Authenticationのオブジェクトを取ります。credentialCallable値をカスタムのBCryptベースの関数に設定する必要があるのはすべてです。

コントローラーがアダプターをラップするためのクラスを作成しました。

namespace User\Controller\Plugin;

class UserAuthentication extends AbstractPlugin {
    protected $_authAdapter = null;
    protected $_authService = null;

    public function __construct($authAdapter) {
        $this->setAuthAdapter($authAdapter);
    }
    // More setters/getters
}

ここで、この呼び出しによって有効なインスタンスが得られるようにモジュールを構成する必要があります。

$uAuth = $this->getServiceLocator()->get('User\Controller\Plugin\UserAuthentication');

当然のことながら、モジュール構成を使用する必要があります。しかし、クラスのインスタンスを適切に作成する方法についてのヒントが見つからなかったため、ここでは完全に完全に立ち往生しています。これは私がこれまでに思いついたものです:

return array(
    'di' => array(
        'instance' => array(
            'User\Event\Authentication' => array(
                'parameters' => array(
                    'userAuthenticationPlugin' => 'User\Controller\Plugin\UserAuthentication',
                ),
            ),
            'User\Controller\Plugin\UserAuthentication' => array(
                'parameters' => array(
                    'authAdapter' => 'DoctrineModule\Authentication\Adapter\ObjectRepository'
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'DoctrineModule\Authentication\Adapter\ObjectRepository' => function ($sm) {
                /// ????
            },
            'DoctrineModule\Options\Authentication' => function($sm) {                
                /// ????
            },
        ),
    ),
);

だから私はそこに何を記入するのか、あるいはこれが正しい方法でさえあるのかどうかわかりません。これを実行すると、次のようになるため、おそらく私は完全に間違ったパスをたどります。

An abstract factory could not create an instance of usercontrollerpluginuserauthentication(alias: User\Controller\Plugin\UserAuthentication).

アイデアやヒントに感謝します。そして、私に指示したり、同様のものを指示したりしないでくださいZfcUser。私はこれを自分で実装したい/必要があります。

4

1 に答える 1

2

私はDiまだZF2で作業していません。しかし、これが私ObjectRepositoryがZF2でDoctrineModuleを使用する方法です。

私のModule.php中には、AuthenticationServiceがあるのと同じように、AuthenticationServiceのファクトリがあります。ObjectRepositoryファクトリでは、必要なすべての値を使用して新しいものを作成します。

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'AuthService' => function($services) {
                $entityManager = $services->get('doctrine.entitymanager.orm_default');
                $doctrineAuthAdapter = new ObjectRepository(array(
                    'objectManager' => $entityManager,
                    'identityClass' => 'Auth\Entity\User',
                    'identityProperty' => 'username',
                    'credentialProperty' => 'password',
                    'credentialCallable' => function($identity, $credential) {
                        return md5($identity->salt . $credential);
                    }, // this function makes the password hash salted
                       // you could also just use return md5($credential);
                ));

                // my AuthenticationService uses the entity manager
                // and the ObjectRepository
                $authService = new AuthenticationService();
                $authService->setEntityManager($entityManager);
                $authService->setAdapter($doctrineAuthAdapter);

                return $authService;
            },
        ),
    );
}

これAuthenticationServiceは基本的にZendのAuthenticationServiceの拡張であり、いくつかの追加のメソッドがあり、便利だと思いました(そしてZfc、そこから覗いたので使用します)。簡潔にするために、実装を削除しましたが、サービスで役立つと思われるものを確認できるように宣言を残しました。

<?php

namespace Auth\Service;

use Application\EntityManagerAwareInterface;
use Zend\Authentication\AuthenticationService as ZendAuthenticationService;

use Auth\Entity\User;

class AuthenticationService extends ZendAuthenticationService implements EntityManagerAwareInterface
{
    /**
     * This method makes sure that we always get a User-object
     * we can call methods on. In case of a non-authorized user
     * we will receive a dummy object without storage or with
     * session storage. So data might be lost!
     */
    public function getIdentity()
    {
        $storage = $this->getStorage();

        if ($storage->isEmpty()) {
            return new \Auth\Entity\User\Dummy();
        }

        $userid = $storage->read();

        $user = $this->getEntityManager()->find('Auth\Entity\User', $userid);

        if ($user == null) {
            return new \Auth\Entity\User\Dummy();
        } else {
            return $user;
        }
    }

    /**
     * Register a new user to the system. The user password will by hashed before
     * it will be saved to the database.
     */
    public function register(User $user)
    {

    }

    /**
     * Reset the users password to a random value and send an e-mail to the
     * user containing the new password.
     */
    public function resetPassword(User $user)
    {

    }

    /**
     * Delete a users account from the database. This does not really delete the
     * user, as there are too many connections to all other tables, but rather
     * deletes all personal information from the user records.
     */
    public function delete(User $user)
    {

    }

    public function setEntityManager(\Doctrine\ORM\EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function getEntityManager()
    {
        return $this->entityManager;
    }
}

次に、コントローラーで次のコードを使用してユーザーにログインできます。

// getAuthService() just calls $this->getServiceLocator()->get('AuthService')

$this->getAuthService()->getAdapter()
     ->setIdentityValue($username)
     ->setCredentialValue($password);

$result = $this->getAuthService()->authenticate();

foreach($result->getMessages() as $message) {
    $this->flashmessenger()->addMessage($message);
}

if ($result->isValid()) {
     $this->getAuthService()->getStorage()->write($result->getIdentity()->getId());
     return true;
 } else {
     return false;
 }
于 2013-02-24T11:46:06.227 に答える