zend-authentication - 将身份设置为加载了 rbac 角色的自定义对象

zend-authentication - setting identity to custom object with rbac roles loaded

在 ZF2 项目中,我使用 AuthenticationService 来验证用户登录凭据。这工作正常,除了它只在会话中存储一个包含用户名的字符串。

我希望后续调用 AuthenticationService::getIdentity 到 return 自定义身份对象,该对象填充有用户数据库 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');
    }
}

查看 https://github.com/zendframework/zend-authentication/blob/master/src/AuthenticationService.php#L75 and https://github.com/zendframework/zend-authentication/blob/master/src/Storage/StorageInterface.php

您可以像这样将 AuthenticatedIdentity 对象直接写入存储:

$this->authenticationService->getStorage()->write($identity);

但是,我建议不要这样做,因为:

  1. 如果用户 permissions/roles 在会话期间进行更改 he/she 则必须注销并重新登录才能看到任何用户不友好的更改。
  2. 您的 AuthenticatedIdentity 对象及其包含的所有对象都需要是可序列化的,这在维护时可能会出现问题。

我会(并且确实)在需要时从数据库或某种形式的缓存中获取用户对象 and/or 角色,但不会将其存储在会话中。