在ZF2项目中,我使用AuthenticationService来验证用户登录凭据。这工作正常,除了它只在会话中存储包含用户名的字符串。
我希望后续调用AuthenticationService :: getIdentity来返回一个自定义Identity对象,该对象填充了用户数据库ID,角色和权限(从RBAC服务中提供),以便该对象在会话更有用。
我能够创建这个对象,但不确定将其保留在会话中的最佳方法;理想情况下,我想用键Zend_Auth覆盖该条目,但这似乎不起作用。
到目前为止我的代码:
<?php
namespace Authentication\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
use Authentication\Form\Login\LoginForm;
use Zend\Form\Form;
use Authentication\Model\Identity\AuthenticatedIdentity;
class AuthenticationController extends AbstractActionController
{
/**
*
* @var AuthenticationService
*/
protected $authenticationService;
/**
*
* @var LoginForm
*/
protected $loginForm;
/**
*
* @param AuthenticationService $authenticationService
* @param LoginForm $loginForm
*/
public function __construct(AuthenticationService $authenticationService, LoginForm $loginForm){
$this->authenticationService = $authenticationService;
$this->loginForm = $loginForm;
}
public function indexAction(){
$form = $this->loginForm;
$viewModel = new ViewModel();
$viewModel->setVariables([
'loginForm' => $form
]);
if($this->getRequest()->isPost() === false){
return $viewModel;
}
$form->setData($this->getRequest()->getPost());
if($form->isValid() === false){
return $viewModel;
}
$data = $form->getData();
$authenticationAdapter = $this->authenticationService->getAdapter();
$authenticationAdapter->setIdentity($data['credentials']['username'])
->setCredential($data['credentials']['password']);
$authenticationResult = $this->authenticationService->authenticate($authenticationAdapter);
if($authenticationResult->isValid() === false){
$viewModel->setVariable('validCredentials', false);
return $viewModel;
}
/**
* Create a user model and save it to the session.
*/
$authenticationResultRow = $authenticationAdapter->getResultRowObject(null, ['password']);
$permissions = $this->rbacService->getPermissionsForUser($authenticationResultRow->user_id);
$roles = $this->rbacService->getRolesForUser($authenticationResultRow->user_id);
$identity = new AuthenticatedIdentity(
$authenticationResult->getIdentity(),
'admin',
$permissions,
$roles
);
$identity->setUserId($authenticationResultRow->user_id);
//how to store this Identity object in session so AuthenticationService will return it?
return $this->redirect()->toRoute('dashboard');
}
}
答案 0 :(得分:1)
结帐https://github.com/zendframework/zend-authentication/blob/master/src/AuthenticationService.php#L75和https://github.com/zendframework/zend-authentication/blob/master/src/Storage/StorageInterface.php
您可以将AuthenticatedIdentity
对象直接写入存储,如下所示:
$this->authenticationService->getStorage()->write($identity);
但是,我建议不要这样做,因为:
AuthenticatedIdentity
对象及其包含的所有对象都需要序列化,这可能会对维护造成问题。我会(并且确实)在需要时从DB或某种形式的缓存中获取用户对象和/或角色,但不要将其存储在会话中。