我终于成功了,但我不确定这是最好的解决方案。
我使用我的voter 来管理我自己的逻辑。在我的管理实体中,我覆盖了“编辑”的模板。
class ProjectAdmin extends AbstractAdmin
{
...
public function getTemplate($name)
{
switch ($name) {
case 'edit':
return 'Sonata/ProjectAdmin/edit.html.twig';
break;
default:
return parent::getTemplate($name);
break;
}
}
...
}
我在templates/Sonata/ProjectAdmin/edit.html.twig中创建了我的模板
包含:
{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
{% use 'SonataExtends/base_edit_form.html.twig' with form as parentForm %}
{% block form %}
{{ block('parentForm') }}
{% endblock %}
然后我将vendor/sonata-project/admin-bundle/src/Ressources/views/CRUD/base_edit_form.html.twig复制/粘贴到templates/SonataExtends/base_edit_form.html.twig中
我换了块:
{% if admin.hasRoute('delete') and admin.hasAccess('delete', object) %}
{{ 'delete_or'|trans({}, 'SonataAdminBundle') }}
<a class="btn btn-danger" href="{{ admin.generateObjectUrl('delete', object) }}">
<i class="fa fa-minus-circle" aria-hidden="true"></i> {{ 'link_delete'|trans({}, 'SonataAdminBundle') }}</a>
{% endif %}
给我的选民打电话:
{% if admin.hasRoute('delete') and is_granted('delete', object) %}
{{ 'delete_or'|trans({}, 'SonataAdminBundle') }}
<a class="btn btn-danger" href="{{ admin.generateObjectUrl('delete', object) }}">
<i class="fa fa-minus-circle" aria-hidden="true"></i> {{ 'link_delete'|trans({}, 'SonataAdminBundle') }}</a>
{% endif %}
唯一的区别是is_granted('delete', object) 而不是admin.hasAccess('delete', object)
正如我所说,这可能不是最好的方法,所以感谢您纠正我。但是我没有设法覆盖 admin.hasAccess('delete', object) 的逻辑。
对于其他管理类,我只需要使用我的getTemplate 函数来使用这个逻辑。
PS:
我还在我的管理类中添加了这段代码来管理删除操作:
public function preRemove($project){
if (false === $this->getConfigurationPool()->getContainer()->get('security.authorization_checker')->isGranted('delete', $project)) {
throw new AccessDeniedHttpException();
}
}