我终于找到了一种方法,它可能不是最有效的方法,但它有效,并且是我知道的唯一方法,因为到目前为止没有人知道如何实现这一点。
首先,我为每个组都有一个默认用户,该用户无法登录(具有该组默认权限的虚拟用户) - 我获得了默认用户的安全 ID:
$defaultUser = $this->getDoctrine()
->getRepository('TdfUserBundle:User')
->findOneByUsername('-default-'.$group->getCode());
$sid = UserSecurityIdentity::fromAccount($defaultUser);
我创建了一个权限数组来检查并设置一些空数组,并加载有问题的.acl_manager
$permissionsToCheck = array('VIEW', 'EDIT', 'CREATE', 'DELETE', 'OPERATOR', 'MASTER', 'OWNER');
$aclManager = $this->get('problematic.acl_manager');
然后我遍历我想要检查权限的对象,并检查我之前在 $permissionsToCheck 变量中设置的权限。我检查默认用户的权限。结果被放入我发送到模板的数组中。
foreach($forumCategories as $forumCategory) :
$permissionArray[] = $this->checkPermissions($sid, $forumCategory, $permissionsToCheck, '');
endforeach;
checkPermissions 函数从给定的对象返回权限数组和我需要的一些东西。
private function checkPermissions($sid, $object, $permissionsToCheck, $type)
{
$aclProvider = $this->get('security.acl.provider');
$oid = ObjectIdentity::fromDomainObject($object);
try {
$acl = $aclProvider->createAcl($oid);
}catch(\Exception $e) {
$acl = $aclProvider->findAcl($oid);
}
$aclProvider->updateAcl($acl);
foreach ($permissionsToCheck as $permissionCode):
$permissionVar = 'can'.$permissionCode;
$builder = new MaskBuilder();
$builder->add($permissionCode);
$mask = $builder->get();
try {
$$permissionVar = $acl->isGranted(array($mask),array($sid));
} catch(\Exception $e) {
$$permissionVar = false;
}
$tempPermissionsArray[$permissionCode] = $$permissionVar;
endforeach;
$returnArray = array('id' => $object->getId(),'title' => $object->getTitle(),'slug' => $object->getSlug(),'type' => $type, 'permissions' => $tempPermissionsArray);
return $returnArray;
}
在表单的 POST 之后,我检查哪些对象的权限发生了变化,如果是这样,我会遍历组中的所有用户。对于每个用户,撤销权限,然后获取所有组(组的默认用户)。检查每个组(默认用户)权限,检查要激活的权限并为用户提供正确的权限。
在这里,我将所有权限设置为 false,然后遍历所有角色/组(默认用户),看看是否应该设置权限。
foreach($array['permissions'] as $permissionCode => $test ):
$$permissionCode = false;
endforeach;
foreach($user->getRoles() as $role):
$role = str_replace('ROLE_', '', $role);
$defaultUser = $this->getDoctrine()
->getRepository('TdfUserBundle:User')
->findOneByUsername('-default-'.$role);
$sid = UserSecurityIdentity::fromAccount($defaultUser);
// See all permissions
foreach($array['permissions'] as $permissionCode => $test ):
$builder = new MaskBuilder();
$builder->add($permissionCode);
$mask = $builder->get();
try {
$isGranted = $acl->isGranted(array($mask),array($sid));
if($isGranted):
$$permissionCode = true;
endif;
} catch(\Exception $e) {
}
endforeach;
endforeach;
在此之后,我知道用户应该拥有什么权利,然后赋予该帐户所有权利:
$aclManager = $this->get('problematic.acl_manager');
$aclManager->revokeAllObjectPermissions($object, $user);
$mapping = array(
'VIEW' => MaskBuilder::MASK_VIEW,
'EDIT' => MaskBuilder::MASK_EDIT,
'CREATE' => MaskBuilder::MASK_CREATE,
'UNDELETE' => MaskBuilder::MASK_UNDELETE,
'DELETE' => MaskBuilder::MASK_DELETE,
'OPERATOR' => MaskBuilder::MASK_OPERATOR,
'MASTER' => MaskBuilder::MASK_MASTER,
'OWNER' => MaskBuilder::MASK_OWNER,
);
foreach($array['permissions'] as $permissionCode => $test ):
if($$permissionCode):
$mask = $mapping[$permissionCode];
$aclManager->addObjectPermission($object, $mask, $user);
endif;
endforeach;