【发布时间】:2017-03-11 10:36:21
【问题描述】:
我正在使用OctoberCMS,它使用Twig。
我获取用户权限的方法是在 php 中获取信息,然后传递一个 twig 变量。
有没有办法只使用 Twig 来做到这一点?
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