【问题标题】:Symfony 2 Access Control to Objects in ViewSymfony 2 对视图中对象的访问控制
【发布时间】:2011-12-24 04:37:13
【问题描述】:

在视图中,我可以使用

if ($view['security']->isGranted('ROLE_ADMIN')) :

检查用户是否具有特定角色。但是对象呢?

例如:当用户查看他/她的帖子时,我想显示一个编辑/删除按钮

【问题讨论】:

    标签: view symfony access-control


    【解决方案1】:

    在树枝模板中,您可以使用is_granted 函数,有关详细信息,请参阅Access control in templates

    要将其应用于 ACL,您可以这样做:

    {% if is_granted('EDIT', post) %}
        Show buttons here
    {% endif %}
    

    【讨论】:

    • 我想它也可以应用于PHP?另外,如果我想检查权限而不是角色,我想我可以做$view['security']->isGranted('VIEW', $post)?
    • 是的,抱歉,我没有意识到您使用的是 PHP 视图而不是 Twig。 SecurityContext 上的 isGranted 方法采用 ACL 参数,如您所说的 isGranted('VIEW', $post)
    猜你喜欢
    • 2012-10-01
    • 2016-07-22
    • 1970-01-01
    • 2010-12-06
    • 2018-04-03
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    相关资源
    最近更新 更多