【问题标题】:Restrict actions for some users with Sonata with a custom strategy使用自定义策略限制某些使用 Sonata 的用户的操作
【发布时间】:2019-08-18 10:29:50
【问题描述】:

当当前用户不是当前对象的所有者但确实具有编辑它的 ADMIN 角色时,我想删除一些操作,例如“删除”表单。

我想将此行为应用于列表(复选框)或编辑对象时。

我目前使用我在 Admin 类中使用的投票器,例如:

protected function configureFormFields(FormMapper $formMapper){
        if($this->isCurrentRoute('edit') && !$this->getConfigurationPool()->getContainer()->get('security.authorization_checker')->isGranted('edit', $this->getSubject()))
            throw new AccessDeniedHttpException();
...
}

我用自己的逻辑检查。但我不知道如何使用我的选民来删除删除操作。

我首先尝试使用自己的逻辑删除 configureRoutes 上的操作,但没有成功。另外,我读到由于缓存问题,这是一个糟糕的选择。

【问题讨论】:

    标签: symfony sonata


    【解决方案1】:

    我终于成功了,但我不确定这是最好的解决方案。

    我使用我的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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-07
      • 2017-04-22
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      相关资源
      最近更新 更多