gpt4 book ai didi

symfony - FOSUserBundle 和 ACL 业务角色

转载 作者:行者123 更新时间:2023-12-02 20:55:47 27 4
gpt4 key购买 nike

这个周末我开始学习 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');

您可能会看到上面代码片段的所有详细信息。

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

最佳答案

Symfony2 有一个 ACL system开箱即用即可执行此操作。为了完整起见,我添加了相关代码(修改为 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);
}
}

本质上,您向当前登录的用户授予 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
// ...
}

强烈建议您仔细阅读Access Control ListAdvanced ACL Concepts食谱食谱以获取更多信息。如上所示的 ACL 的实际创建非常冗长,我一直在研究 open-source ACL manager减轻疼痛……它“有点作用”;它处于早期测试阶段,需要大量的喜爱,因此使用时需要您自担风险。

关于symfony - FOSUserBundle 和 ACL 业务角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7002637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com