FOSUserBundle和ACL业务角色

use*_*848 6 acl yii symfony fosuserbundle

我本周末开始学习Symfony 2.我没有遇到任何问题,因为我认为框架已有详细记录.

我正在使用FOSUserBundle包进行ACL.我想知道是否有可能使它类似于Yii框架:

$bizRule='return Yii::app()->user->id==$params["post"]->authID;';
$task=$auth->createTask('updateOwnPost','update a post by author himself',$bizRule);
$task->addChild('updatePost');
Run Code Online (Sandbox Code Playgroud)

您可能会在上面的代码段中看到所有详细信息.

如何实现与Symfony 2类似的功能?这可能吗?

Pro*_*tic 22

Symfony2有一个开箱即用的ACL系统.为了完整起见,我包含相关代码(修改后Post代替Comment文档中的内容):

public function addPostAction()
{
    $post = new Post();

    // setup $form, and bind data
    // ...

    if ($form->isValid()) {
        $entityManager = $this->get('doctrine.orm.default_entity_manager');
        $entityManager->persist($post);
        $entityManager->flush();

        // creating the ACL
        $aclProvider = $this->get('security.acl.provider');
        $objectIdentity = ObjectIdentity::fromDomainObject($post);
        $acl = $aclProvider->createAcl($objectIdentity);

        // retrieving the security identity of the currently logged-in user
        $securityContext = $this->get('security.context');
        $user = $securityContext->getToken()->getUser();
        $securityIdentity = UserSecurityIdentity::fromAccount($user);

        // grant owner access
        $acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
        $aclProvider->updateAcl($acl);
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,您将为Post实体提供当前登录的用户所有权(包括编辑权限).然后检查当前用户是否有权编辑:

public function editPostAction(Post $post)
{
    $securityContext = $this->get('security.context');

    // check for edit access
    if (false === $securityContext->isGranted('EDIT', $post))
    {
        throw new AccessDeniedException();
    }

    // retrieve actual post object, and do your editing here
    // ...
}
Run Code Online (Sandbox Code Playgroud)

强烈建议您阅读访问控制列表高级ACL概念菜谱配方以获取更多信息.如上所示,ACL的实际创建非常冗长,我一直致力于开源ACL管理器以缓解痛苦......它"有点有用"; 它是早期测试版,需要很多爱,所以使用风险自负.

  • 如果我的回答对您有帮助,请随时使用答案顶部附近的箭头和复选标记进行投票并接受.谢谢. (4认同)