[英]Phalcon PhP - how to use $this->acl->isAllowed in .volt view
I'm working in a Phalcon 2.0.13 Project using the ACL to control permissions. 我正在使用ACL控制权限的Phalcon 2.0.13项目中。 My Question is: is there a way for me to use the isAllowed in a .volt view?
我的问题是:我可以在.volt视图中使用isAllowed吗?
My goal is to control some options I show in the .volt view based on user's permissions. 我的目标是根据用户权限控制在.volt视图中显示的某些选项。
Just to clarify to which function I mean: 只是为了澄清我的意思是:
if( !$this->acl->isAllowed( $userRole, ucfirst( $this->dispatcher->getControllerName() ), $this->dispatcher->getActionName() ) ){
$this->response->redirect( $this->url->get(['for' => 'admin-index-login']) );
}
This is a piece of code I run inside my base controller, I would like to check for permissions in the .volt view, something like {{ if isAllowed("User", "New") }} xxxx {{ endif }}
这是我在基本控制器中运行的一段代码,我想在.volt视图中检查权限,例如
{{ if isAllowed("User", "New") }} xxxx {{ endif }}
Thanks for any help 谢谢你的帮助
Here is what I did to solve. 这是我要做的解决。 My permission system is in the database, because the admin can set permissions to roles, user groups or a particular user.
我的权限系统位于数据库中,因为管理员可以为角色,用户组或特定用户设置权限。
I created a custom function to use in the .volt views: 我创建了一个自定义函数以在.volt视图中使用:
$compiler = $volt->getCompiler(); //Custom volt functions $compiler->addFunction('has_permission', function($resolvedArgs, $exprArgs) { return '\\HelperFunctions::UserHasPermission(' . $resolvedArgs . ')'; });
UserHasPermission
receives the controller and action names to check if the user has permission. UserHasPermission
接收控制器名称和操作名称,以检查用户是否具有权限。 I'm using it like this: 我正在这样使用它:
{% if has_permission("user", "*") %}
<li>
<a href="#" title="Users"><i class="fa fa-lg fa-fw fa-user"></i> <span class="menu-item-parent">Users</span></a>
<ul>
<li>
<a href="{{ url(['for': 'admin-user-index']) }} " title="Users"><span class="menu-item-parent">Users</span></a>
</li>
</ul>
</li>
{% endif %}
*
in the action name means if the user has permission in any of the UserController actions. 动作名称中的
*
表示用户是否有权在任何UserController动作中使用。
So far I'm happy with the solution and if at some point I want to switch back to Phalcon's ACL it is pretty easy. 到目前为止,我对解决方案感到满意,并且如果要在某个时候切换回Phalcon的ACL,这非常简单。
If acl is added as service then just do: 如果将acl添加为服务,则只需执行以下操作:
{% if acl.isAllowed("User", "New") %}
{% endif %}
No need to custom function in volt. 无需定制伏特功能。 Phalcon ACL Memory based have a lot more options than database and it's faster anyway.
基于Phalcon ACL Memory的数据库比数据库具有更多的选择,并且无论如何它都更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.