我正在尝试通过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');
很自然地,我将不得不使用模块condifguration。但是在这里我完全陷入困境,因为我找不到任何关于如何正确创建类实例的提示。这是我到目前为止所提出的:
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
或类似的,我想/需要自己实施。
答案 0 :(得分:2)
我还没有在ZF2中使用Di
。但这是我如何在ZF2中使用DoctrineModule ObjectRepository
。
在我的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;
}
}
然后在Controller中我可以使用以下代码登录用户:
// 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;
}