简体   繁体   English

Phalcon PhP-如何在.volt视图中使用$ this-> acl-> is

[英]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. 我的权限系统位于数据库中,因为管理员可以为角色,用户组或特定用户设置权限。

  1. When the user logs in the system, I keep all his permissions in session 当用户登录系统时,我将其所有权限保留在会话中
  2. Instead of using the acl (I was doing it before) I made a function that checks if the user has permission in the controller/action (My permissions are based on controller and action, like the ACL). 我没有使用acl(我之前做过),而是创建了一个函数来检查用户在控制器/操作中是否具有权限(我的权限基于控制器和操作,例如ACL)。
  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM