访问控制列表 ACL(Access Control Lists ACL) ======================== :doc:`Phalcon\\Acl <../api/Phalcon_Acl>` provides an easy and lightweight management of ACLs as well as the permissions attached to them. `Access Control Lists`_ (ACL) allow an application to control access to its areas and the underlying objects from requests. You are encouraged to read more about the ACL methodology so as to be familiar with its concepts. In summary, ACLs have roles and resources. Resources are objects which abide by the permissions defined to them by the ACLs. Roles are objects that request access to resources and can be allowed or denied access by the ACL mechanism. 创建 ACL(Creating an ACL) --------------- This component is designed to initially work in memory. This provides ease of use and speed in accessing every aspect of the list. The :doc:`Phalcon\\Acl <../api/Phalcon_Acl>` constructor takes as its first parameter an adapter used to retriever the information related to the control list. An example using the memory adapter is below: .. code-block:: php ` allows access to action on resources that have not been yet defined. To increase the security level of the access list we can define a "deny" level as a default access level. .. code-block:: php setDefaultAction(Phalcon\Acl::DENY); 添加角色(Adding Roles to the ACL) ----------------------- A role is an object that can or cannot access certain resources in the access list. As an example, we will define roles as groups of people in an organization. The :doc:`Phalcon\\Acl\\Role <../api/Phalcon_Acl_Role>` class is available to create roles in a more structured way. Let's add some roles to our recently created list: .. code-block:: php addRole($roleGuests); // Add "Designers" role to acl without a Phalcon\Acl\Role $acl->addRole("Designers"); As you can see, roles are defined directly without using an instance. 添加资源(Adding Resources) ---------------- Resources are objects where access is controlled. Normally in MVC applications resources refer to controllers. Although this is not mandatory, the :doc:`Phalcon\\Acl\\Resource <../api/Phalcon_Acl_Resource>` class can be used in defining resources. It's important to add related actions or operations to a resource so that the ACL can understand what it should to control. .. code-block:: php addResource($customersResource, "search"); $acl->addResource($customersResource, array("create", "update")); 定义访问控制(Defining Access Controls) ------------------------ Now we've roles and resources. It's time to define the ACL i.e. which roles can access which resources. This part is very important especially taking in consideration your default access level "allow" or "deny". .. code-block:: php allow("Guests", "Customers", "search"); $acl->allow("Guests", "Customers", "create"); $acl->deny("Guests", "Customers", "update"); The allow method designates that a particular role has granted access to a particular resource. The deny method does the opposite. 查询 ACL(Querying an ACL) --------------- Once the list has been completely defined. We can query it to check if a role has a given permission or not. .. code-block:: php isAllowed("Guests", "Customers", "edit"); //Returns 0 $acl->isAllowed("Guests", "Customers", "search"); //Returns 1 $acl->isAllowed("Guests", "Customers", "create"); //Returns 1 角色继承(Roles Inheritance) ----------------- You can build complex role structures using the inheritance that :doc:`Phalcon\\Acl\\Role <../api/Phalcon_Acl_Role>` provides. Roles can inherit from other roles, thus allowing access to supersets or subsets of resources. To use role inheritance, you need to pass the inherited role as the second parameter of the function call, when adding that role in the list. .. code-block:: php addRole($roleGuests); // Add "Administrators" role inheriting from "Guests" its accesses $acl->addRole($roleAdmins, $roleGuests); 序列化 ACL 列表(Serializing ACL lists) --------------------- To improve performance :doc:`Phalcon\\Acl <../api/Phalcon_Acl>` instances can be serialized and stored in APC, session, text files or a database table so that they can be loaded at will without having to redefine the whole list. You can do that as follows: .. code-block:: php isAllowed("Guests", "Customers", "edit")) { echo "Access granted!"; } else { echo "Access denied :("; } ACL 事件(ACL Events) ---------- :doc:`Phalcon\\Acl <../api/Phalcon_Acl>` is able to send events to a :doc:`EventsManager ` if it's present. Events are triggered using the type "acl". Some events when returning boolean false could stop the active operation. The following events are supported: +----------------------+------------------------------------------------------------+---------------------+ | Event Name | Triggered | Can stop operation? | +======================+============================================================+=====================+ | beforeCheckAccess | Triggered before checking if a role/resource has access | Yes | +----------------------+------------------------------------------------------------+---------------------+ | afterCheckAccess | Triggered after checking if a role/resource has access | No | +----------------------+------------------------------------------------------------+---------------------+ The following example demonstrates how to attach listeners to this component: .. code-block:: php attach("acl", function($event, $acl) { if ($event->getType() == "beforeCheckAccess") { echo $acl->getActiveRole(), $acl->getActiveResource(), $acl->getActiveAccess(); } }); $acl = new \Phalcon\Acl\Adapter\Memory(); //Setup the $acl //... //Bind the eventsManager to the acl component $acl->setEventsManager($eventManagers); 自定义适配器(Implementing your own adapters) ------------------------------ The :doc:`Phalcon\\Acl\\AdapterInterface <../api/Phalcon_Acl_AdapterInterface>` interface must be implemented in order to create your own ACL adapters or extend the existing ones. .. _Access Control Lists: http://en.wikipedia.org/wiki/Access_control_list