Phalcon教程之保护INVO(翻译)

Phalcon教程之保护INVO(翻译)今天在看中文版的 Phalcon 教程的时候发现有一章的后半截没有翻译完 遂将其翻译一下 就当学习了 后端安全 Securing the Backend 后端是一个私有区域 只有已经注册并登录的用户才可以访问 因此 只有登录用户才能访问控制器这样的检验是有必要的

大家好,我是讯享网,很高兴认识大家。

今天在看中文版的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); } }

 

小讯
上一篇 2025-02-23 16:31
下一篇 2025-03-28 15:55

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/15985.html