Php Symfony 2 ACL和角色层次结构
我有点困惑,找不到答案 在我的应用程序测试中,我创建了两个实体,用户和注释都正确映射 我已经创建了一个小型控制器,如果我以标准用户的身份创建我的注释,并与“ROLE\u user”的for关联,并尝试以“ROLE\u ADMIN”角色的用户身份访问它,则该控制器将根据用户的不同,将注释和数据添加到ACL表中,它似乎完全忽略了security.yml层次结构 我知道这是通过添加用户ID而不是角色\用户等来实现的,但我不想这样做 下面是我的代码示例 CommentControllerPhp Symfony 2 ACL和角色层次结构,php,symfony,acl,hierarchy,roles,Php,Symfony,Acl,Hierarchy,Roles,我有点困惑,找不到答案 在我的应用程序测试中,我创建了两个实体,用户和注释都正确映射 我已经创建了一个小型控制器,如果我以标准用户的身份创建我的注释,并与“ROLE\u user”的for关联,并尝试以“ROLE\u ADMIN”角色的用户身份访问它,则该控制器将根据用户的不同,将注释和数据添加到ACL表中,它似乎完全忽略了security.yml层次结构 我知道这是通过添加用户ID而不是角色\用户等来实现的,但我不想这样做 下面是我的代码示例 CommentController <
<?php
namespace ACL\TestBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use ACL\TestBundle\Forms\Type\commentType;
use ACL\TestBundle\Entity\Comment;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
class DefaultController extends Controller
{
/**
* @Route("/", name="_default")
* @Template()
*/
public function indexAction()
{
die('success');
}
/**
* @Route("/comment/new/")
* @Template()
*/
public function newAction(Request $request)
{
$comment = new Comment();
$form = $this->createForm(new commentType(), $comment);
$form->handleRequest($request);
if ($form->isValid()) {
$comment->setUsers($this->getUser());
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
// creating the ACL
$aclProvider = $this->get('security.acl.provider');
$objectIdentity = ObjectIdentity::fromDomainObject($comment);
$acl = $aclProvider->createAcl($objectIdentity);
// retrieving the security identity of the currently logged-in user
$securityIdentity = UserSecurityIdentity::fromAccount($this->getUser());
// grant owner access
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
$aclProvider->updateAcl($acl);
}
return array(
'form' => $form->createView(),
);
}
/**
* @Route("/comment/{id}/", requirements={"id":"\d+"})
* @Template()
*/
public function editAction(Request $request,$id)
{
$em = $this->getDoctrine()->getManager();
$comment = $em->find('ACLTestBundle:Comment', $id);
$securityContext = $this->get('security.context');
// check for edit access
if (false === $securityContext->isGranted('EDIT',$comment)) {
throw new AccessDeniedException();
}
$form = $this->createForm(new commentType(), $comment);
$form->handleRequest($request);
if($form->isValid()){
$em->persist($comment);
$em->flush();
}
return array('form' => $form->createView());
}
}
谢谢你的建议 问题在于,您正在添加基于UserIdentity的ACL,并希望检查RoleIdentity上的gran库。如果要执行此操作,请按如下所示更改创建ACL的角色库
// creating the ACL
$aclProvider = $this->get('security.acl.provider');
$objectIdentity = ObjectIdentity::fromDomainObject($comment);
$acl = $aclProvider->createAcl($objectIdentity);
// retrieving the security identity of the currently logged-in user
$securityIdentity = UserSecurityIdentity::fromAccount($this->getUser());
// grant owner access
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
// grant EDIT access to ROLE_ADMIN
$securityIdentity = new RoleSecurityIdentity('ROLE_ADMIN');
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_EDIT);
$aclProvider->updateAcl($acl);
如您所见,我保留了特定用户的所有者访问权限,然后添加了角色\ u ADMIN的编辑访问权限。您可以保持控制器的原样
如果您不想让它成为角色库,但只想为管理员用户提供一个例外,您可以将控制器更改为
// check for edit access
if (false === $securityContext->isGranted('EDIT',$comment) && false === $securityContext->isGranted('ROLE_ADMIN') ) {
throw new AccessDeniedException();
}
这一行(
$securityContext->isgrated('EDIT',$comment)
)对我来说似乎有点奇怪。我通常只看到角色的调用,EDIT
从哪里来,在哪里设置?如果你检查那里的角色会发生什么?@nietonfir问题是关于ACL和角色层次结构的,所以这不是重复EDIT
mask是OWNER
mask的一部分,它设置为创建时的实体。那么这是否意味着ACL不支持role\u hierarchy
?@它支持,但您将ACL分配给的是对象用户库而不是角色库,那么您如何检查它是否被授予了特定角色?看看这个,希望能得到角色库用户库的概念[它表示每个角色或用户都有自己的安全标识。就我个人而言,如果role\u用户
帐户可以访问一个实体,并且role\u管理员
由role\u层次结构
包含role\u用户
,则他应该自动访问所有实体,因此您不需要为其添加RoleSecurityIdentity
你能提供一个例子说明ACL如何支持role\u层次结构
,因为我真的不认为有任何可能的方法可以让role\u管理员
在不插入单独的rolesesecurityidentity
或硬编码的情况下访问role\u用户
管理的实体('ROLE_ADMIN')
在控制器中。这就是我试图实现的,这可能吗?@VisioN这是完全正确的,如果用户拥有角色\u用户
,并且如果所有实体对象都被授予此角色,那么角色\u管理员
将继承将具有访问权限的对象。在这种情况下,问题是他将掩码分配给UserIdentity,而不是角色和我s检查基于该用户身份($securityContext->isgrated('EDIT',$comment)
)现在,您如何确保具有角色的用户X已授予$comment
。这就是为什么Symfony将ACL进程分隔到角色库或用户库,并表示每个角色或用户都有自己的安全身份。
// check for edit access
if (false === $securityContext->isGranted('EDIT',$comment) && false === $securityContext->isGranted('ROLE_ADMIN') ) {
throw new AccessDeniedException();
}