Phalcon教程之保护INVO(翻译)

今天在看中文版的Phalcon教程的时候发现有一章的后半截没有翻译完,遂将其翻译一下,就当学习了。

 

后端安全(Securing the Backend)

后端是一个私有区域,只有已经注册并登录的用户才可以访问。因此,只有登录用户才能访问控制器这样的检验是有必要的。如果你没有登录到应用中并试图访问,例如 products 控制器 (这是私有的) 你将会看到如下屏幕:

 

每次有人试图访问任何 controller/action,应用将会验证当前角色 (在session中) 是否能够访问它,否则就会显示一个像上面那样的消息并转发到首页。

我们来看看应用程序如何实现这一点。 首先要知道的是,有一个名为Dispatcher的组件。 他的功能是,接收Router组件传输过来的Controller和Action信息,然后它负责加载适当的控制器并执行相应的操作方法。通常,Phalcon框架会自动创建Dispatcher对象,但在我们的例子中,我们希望在执行所需操作之前执行验证,检查用户是否有权限去访问该方法。

<?php

use Phalcon\Mvc\Dispatcher;

// ...

/**
 * MVC dispatcher
 */
$di->set('dispatcher', function () {

    // ...

    $dispatcher = new Dispatcher();

    return $dispatcher;
});

我们现在可以完全控制应用程序中使用的Dispatcher组件。框架中的许多触发事件可以改变组件中的程序操作流程。 由于依赖注入组件充当组件之间的粘合剂,而名为EventsManager的新组件允许我们拦截组件生成的事件,将事件路由到侦听器。

事件管理(Events Management)

EventsManager允许我们将事件路由到对应的监听器上。现在我们需要监听“排遣”事件,以下代码监听Dispatcher组件生成的所有事件

<?php

use Phalcon\Mvc\Dispatcher;
use Phalcon\Events\Manager as EventsManager;

$di->set('dispatcher', function () {

    // Create an events manager
    $eventsManager = new EventsManager();

    // Listen for events produced in the dispatcher using the Security plugin
    $eventsManager->attach('dispatch:beforeExecuteRoute', new SecurityPlugin);

    // Handle exceptions and not-found exceptions using NotFoundPlugin
    $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin);

    $dispatcher = new Dispatcher();

    // Assign the events manager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

当程序触发了一个beforeExecuteRoute事件,以下插件将会被通知:

<?php

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

当触发beforeException事件,会通知以下插件:

<?php

/**
 * Handle exceptions and not-found exceptions using NotFoundPlugin
 */
$eventsManager->attach('dispatch:beforeException', new NotFoundPlugin);

SecurityPlugin是一个位于(app / plugins / SecurityPlugin.php)的类。 该类实现方法“beforeExecuteRoute”, 这与Dispatcher中生成的事件之一相同:

<?php

use Phalcon\Events\Event;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Mvc\Dispatcher;

class SecurityPlugin extends Plugin
{
    // ...

    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
    {
        // ...
    }
}

钩子事件总是接收第一个参数,该参数包含所生成事件的上下文信息($ event),第二个参数是生成事件本身的对象($ dispatcher)。扩展插件Phalcon \ Mvc \ User \ Plugin并不是必须的,但通过这样做,程序可以更轻松地访问应用程序中可用的服务。现在,我们将验证当前会话中的角色,检查用户是否具有使用ACL列表的访问权限。 如果用户没有访问权限,我们会重定向到主屏幕,如前所述:

<?php

use Phalcon\Acl;
use Phalcon\Events\Event;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Mvc\Dispatcher;

class SecurityPlugin extends Plugin
{
    // ...

    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
    {
        // Check whether the "auth" variable exists in session to define the active role
        $auth = $this->session->get('auth');
        if (!$auth) {
            $role = 'Guests';
        } else {
            $role = 'Users';
        }

        // Take the active controller/action from the dispatcher
        $controller = $dispatcher->getControllerName();
        $action = $dispatcher->getActionName();

        // Obtain the ACL list
        $acl = $this->getAcl();

        // Check if the Role have access to the controller (resource)
        $allowed = $acl->isAllowed($role, $controller, $action);
        if ($allowed != Acl::ALLOW) {

            // If he doesn't have access forward him to the index controller
            $this->flash->error("You don't have access to this module");
            $dispatcher->forward(
                array(
                    'controller' => 'index',
                    'action'     => 'index'
                )
            );

            // Returning "false" we tell to the dispatcher to stop the current operation
            return false;
        }
    }
}

提供 ACL 列表(Providing an ACL list)

在上面的示例中,我们使用$ this-> getAcl()方法获取了ACL。 此方法也在插件中实现。 现在我们将逐步解释如何构建访问控制列表(ACL):

<?php

use Phalcon\Acl;
use Phalcon\Acl\Role;
use Phalcon\Acl\Adapter\Memory as AclList;

// Create the ACL
$acl = new AclList();

// The default action is DENY access
$acl->setDefaultAction(Acl::DENY);

// Register two roles, Users is registered users
// and guests are users without a defined identity
$roles = array(
    'users'  => new Role('Users'),
    'guests' => new Role('Guests')
);

foreach ($roles as $role) {
    $acl->addRole($role);
}

现在,我们分别为每个区域定义资源。 控制器名称是资源,其操作是对资源的访问:

<?php

use Phalcon\Acl\Resource;

// ...

// Private area resources (backend)
$privateResources = array(
  'companies'    => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
  'products'     => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
  'producttypes' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
  'invoices'     => array('index', 'profile')
);
foreach ($privateResources as $resource => $actions) {
    $acl->addResource(new Resource($resource), $actions);
}

// Public area resources (frontend)
$publicResources = array(
    'index'    => array('index'),
    'about'    => array('index'),
    'register' => array('index'),
    'errors'   => array('show404', 'show500'),
    'session'  => array('index', 'register', 'start', 'end'),
    'contact'  => array('index', 'send')
);
foreach ($publicResources as $resource => $actions) {
    $acl->addResource(new Resource($resource), $actions);
}

ACL现在了解现有控制器及其相关操作。 “用户”可以访问前端和后端的所有资源,而 “客人”角色只能访问公共区域:

<?php

// Grant access to public areas to both users and guests
foreach ($roles as $role) {
    foreach ($publicResources as $resource => $actions) {
        $acl->allow($role->getName(), $resource, '*');
    }
}

// Grant access to private area only to role Users
foreach ($privateResources as $resource => $actions) {
    foreach ($actions as $action) {
        $acl->allow('Users', $resource, $action);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值