【问题标题】:OctoberCMS Backend User Permissions with Twig使用 Twig 的 OctoberCMS 后端用户权限
【发布时间】:2017-03-11 10:36:21
【问题描述】:

我正在使用OctoberCMS,它使用Twig

我获取用户权限的方法是在 php 中获取信息,然后传递一个 twig 变量。

有没有办法只使用 Twig 来做到这一点?

Permissions Docs

PHP

if (BackendAuth::check()) {
    # Get User Info
    $backend_user = BackendAuth::getUser();

    # Superuser
    $this['is_superuser'] = $backend_user->is_superuser;

    # Permissions
    $this['permissions_allow_edit'] = $backend_user->hasPermission(
    [
        'mycomponent.gallery.allow_edit'
    ]);
}

树枝

{% if is_superuser or permissions_allow_edit %}
    //edit button
{% endif %}

树枝(独立)

//another way to backendauth check in twig?
if (BackendAuth::check()) {
    $this['backend_user'] = BackendAuth::getUser(); 
}

//////

{% if backend_user.is_superuser or backend_user.permissions.mycomponent.gallery.allow_edit %}
    //edit button
{% endif %}

{{ backend_user }}给出用户信息数组

{"id":2,"first_name":"Matt","last_name":"","login":"matt","email":"user@example.com","permissions":{"mycomponent.gallery.allow_edit":1},"is_activated":false,"activated_at":null,"last_login":"2017-03-11 09:59:48","created_at":"2017-02-11 10:32:14","updated_at":"2017-03-11 10:03:45","is_superuser":0}

{{ backend_user.permissions }} 抛出“数组到字符串转换”错误。

{{ backend_user.permissions.mycomponent.gallery.allow_edit }} 什么都不显示。

如何获取权限数组中的值?

【问题讨论】:

    标签: php twig octobercms


    【解决方案1】:

    我还没有测试过,但是你应该可以使用hasPermission 方法来检查后端用户是否有特定的权限:

    {% if backend_user.is_superuser or backend_user.hasPermission('mycomponent.gallery.allow_edit') %}
        //edit button
    {% endif %}
    

    【讨论】:

    • 它似乎不起作用,它返回 null。但我的权限已设置,树枝变量“permissions_allow_edit”返回 1。
    猜你喜欢
    • 2021-01-29
    • 2017-07-23
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多