Zend framework2 zend身份验证-为加载rbac角色的自定义对象设置标识
在ZF2项目中,我使用AuthenticationService验证用户登录凭据。这工作正常,只是它只在会话中存储一个包含用户名的字符串 我希望对AuthenticationService::getIdentity的后续调用返回一个自定义标识对象,该对象由用户数据库id、角色和权限(从RBAC服务popualted)填充,因此会话中的对象更有用 我能够创建此对象,但不确定在会话中保持此对象的最佳方式;理想情况下,我希望用Zend_Auth键覆盖条目,但这似乎不起作用 到目前为止,我的代码是:Zend framework2 zend身份验证-为加载rbac角色的自定义对象设置标识,zend-framework2,zend-auth,Zend Framework2,Zend Auth,在ZF2项目中,我使用AuthenticationService验证用户登录凭据。这工作正常,只是它只在会话中存储一个包含用户名的字符串 我希望对AuthenticationService::getIdentity的后续调用返回一个自定义标识对象,该对象由用户数据库id、角色和权限(从RBAC服务popualted)填充,因此会话中的对象更有用 我能够创建此对象,但不确定在会话中保持此对象的最佳方式;理想情况下,我希望用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');
}
}
签出并
您可以将AuthenticatedIdentity
对象直接写入存储器,如下所示:
$this->authenticationService->getStorage()->write($identity);
然而,我建议不要这样做,因为:
如果用户的权限/角色在会话期间发生更改,他/她将不得不注销并重新登录,以查看不太方便用户的任何更改
您的AuthenticatedIdentity
对象及其包含的所有对象都需要可序列化,这可能会给维护带来问题
需要时,我会(并且确实)从DB或某种形式的缓存中获取用户对象和/或角色,但不将其存储在会话中。太好了,谢谢。在此应用程序中,用户角色不会在会话中期更改,如果进行了此类更改,则在会话实例中重置身份将非常简单。由于Identity对象(取自ZF2文档的想法)只是一个简单的容器/值对象,所以序列化不是问题。谢谢你的回答!