【发布时间】:2016-11-28 14:29:03
【问题描述】:
我已经实现了克隆操作,就像在文档中一样。如何将克隆操作的访问权限限制为创建对象的用户?
我已经在我的操作中进行了拒绝访问异常检查,但是如果用户不是该对象的作者,我现在如何隐藏列表视图中的按钮。用户应该仍然能够列出订单并显示它。
这是我的路线:
protected function configureRoutes(RouteCollection $collection)
{
$collection->add('clone', $this->getRouterIdParameter().'/clone');
}
还有我的列表字段:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('_action', 'actions', array(
'actions' => array(
'show' => array(),
'edit' => array(),
'clone' => array(
'template' => 'AppBundle:Sonata/Button:clone_button.html.twig'
),
), 'label' => 'Actions'
))
;
}
还有我的克隆动作:
public function cloneAction($id = null)
{
$object = $this->admin->getSubject();
if (!$object) {
throw new NotFoundHttpException(sprintf('Unable to find the object with id : %s', $id));
}
If (!$object->isAuthor($this->getUser())) {
throw new AccessDeniedException();
}
$clonedObject = clone $object;
$this->admin->create($clonedObject);
$this->addFlash('sonata_flash_success', 'Cloned successfully');
return new RedirectResponse($this->admin->generateUrl('edit', array('id' => $clonedObject->getId())));
}
正如您在我的克隆操作中看到的,我检查了用户是否是订单的作者。但是如何通过检查我的isAuthor 函数来完全删除列表中的按钮?
因为现在用户可以看到该按钮,但如果他未经授权克隆订单并单击该按钮,他会收到拒绝访问异常。所以我根本不想显示按钮。 编辑按钮同样重要。
我想过这样的事情:
protected function configureRoutes(RouteCollection $collection)
{
$user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')
->getToken()->getUser();
If (!$object->isAuthor($user)) {
$collection->remove('edit');
$collection->remove('clone');
}
}
但显然这是做不到的。
有人知道怎么做吗?
【问题讨论】:
标签: php symfony sonata-admin symfony-sonata sonata