如何在 Phalcon PHP 中访问 SecurityPlugin (Acl) 中的模型数据?

How to access model data in SecurityPlugin (Acl) in Phalcon PHP?

我正在做一个必须实现 Acl 的小项目,所以我尝试像在示例 invo 项目中那样实现它并且效果很好。但是,我想添加它从名为 Role 的现有模型获取其角色的功能。每当我添加行 $roles = Role::find(); 时,应用程序就会出错。无法从插件访问此数据吗?或者如果可能的话如何访问它? 这是我的 SecurityPlugin 文件

<?php

use Phalcon\Acl;
use Phalcon\Acl\Role;
use Phalcon\Acl\Resource;
use Phalcon\Events\Event;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Acl\Adapter\Memory as AclList;

/**
 * SecurityPlugin
 *
 * This is the security plugin which controls that users only have access to the modules they're assigned to
 */
class SecurityPlugin extends Plugin
{

    /**
     * Returns an existing or new access control list
     *
     * @returns AclList
     */
    public function getAcl()
    {


        if (!isset($this->persistent->acl)) {

            $acl = new AclList();

            $acl->setDefaultAction(Acl::DENY);

            //Register roles **this is the line that causes the problem**

            $roles = Role::find();

            $acl->addRole(new Role('Guest'));
            $acl->addRole(new Role('User'));


            //Public area resources
            $publicResources = array(
                'index'      => array('index'),
                'User'       => array('new'),
                'Errors'     => array('show401'),
                'Session'    => array('index', 'register', 'start', 'end')
                );
            foreach ($publicResources as $resource => $actions) {
                $acl->addResource(new Resource($resource), $actions);
            }

            foreach ($publicResources as $resource => $actions) {
                foreach ($actions as $action) {
                    $acl->allow('Guest', $resource, $action);
                    $acl->allow('User', $resource, $action);
                }
            }

            //Grant access to public areas to both users and guests

            //The acl is stored in session, APC would be useful here too
            $this->persistent->acl = $acl;
        }

        return $this->persistent->acl;
    }

    /**
     * This action is executed before execute any action in the application
     *
     * @param Event $event
     * @param Dispatcher $dispatcher
     */
    public function beforeDispatch(Event $event, Dispatcher $dispatcher)
    {

        $auth = $this->session->get('auth');
        if (!$auth){
            $role = 'Guest';
        } else {
            $role = 'User';
        }

        $controller = $dispatcher->getControllerName();
        $action = $dispatcher->getActionName();

        if ($auth['username'] == 'Admin') {
            return;
        }

        $acl = $this->getAcl();

        $allowed = $acl->isAllowed($role, $controller, $action);
        if ($allowed != Acl::ALLOW) {
            $dispatcher->forward(array(
                'controller' => 'Errors',
                'action'     => 'show401'
                ));
            $this->session->destroy();
            return false;
        }
    }
}

这里是services.php

中的相关代码
$di->set('dispatcher', function() use ($di) {

    $eventsManager = new EventsManager;

    /**
     * Check if the user is allowed to access certain action using the SecurityPlugin
     */
    $eventsManager->attach('dispatch:beforeDispatch', new SecurityPlugin);

    $dispatcher = new Dispatcher;
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

看起来你的 Role 模型和 Phalcon\Acl\Role 正在碰撞。

我会将您的 Role 模型别名为 RoleModel

<?php
use Phalcon\Acl\Role;
use \Role as RoleModel;
...
class SecurityPlugin extends Plugin {
  public function getAcl() {
    ...
    $roles = RoleModel::find();
    ...