Previous topic

Next topic

This Page

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.

This component is designed to initially work in memory. This provides ease of use and speed in accessing every aspect of the list. The Phalcon\Acl constructor takes as its first parameter an adapter used to retrieve the information related to the control list. An example using the memory adapter is below:

<?phpusePhalcon\Acl\Adapter\MemoryasAclList;$acl=newAclList();

By default Phalcon\Acl allows access to action on resources that have not yet been defined. To increase the security level of the access list we can define a “deny” level as a default access level.

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 Phalcon\Acl\Role class is available to create roles in a more structured way. Let’s add some roles to our recently created list:

<?phpusePhalcon\Acl\Role;// Create some roles.// The first parameter is the name, the second parameter is an optional description.$roleAdmins=newRole("Administrators","Super-User role");$roleGuests=newRole("Guests");// Add "Guests" role to ACL$acl->addRole($roleGuests);// Add "Designers" role to ACL without a Phalcon\Acl\Role$acl->addRole("Designers");

Resources are objects where access is controlled. Normally in MVC applications resources refer to controllers. Although this is not mandatory, the 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.

<?phpusePhalcon\Acl\Resource;// Define the "Customers" resource$customersResource=newResource("Customers");// Add "customers" resource with a couple of operations$acl->addResource($customersResource,"search");$acl->addResource($customersResource,["create","update",]);

Now that we have 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 into consideration your default access level “allow” or “deny”.

<?php// Set access level for roles into resources$acl->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.

Also you can add as 4th parameter your custom function which must return boolean value. It will be called when you use isAllowed() method. You can pass parameters as associative array to isAllowed() method as 4th argument where key is parameter name in our defined function.

<?php// Set access level for role into resources with custom function$acl->allow("Guests","Customers","search",function($a){return$a%2===0;});// Check whether role has access to the operation with custom function// Returns true$acl->isAllowed("Guests","Customers","search",["a"=>4,]);// Returns false$acl->isAllowed("Guests","Customers","search",["a"=>3,]);

Also if you don’t provide any parameters in isAllowed() method then default behaviour will be Acl::ALLOW. You can change it by using method setNoArgumentsDefaultAction().

use Phalcon\Acl;<?php// Set access level for role into resources with custom function$acl->allow("Guests","Customers","search",function($a){return$a%2===0;});// Check whether role has access to the operation with custom function// Returns true$acl->isAllowed("Guests","Customers","search");// Change no arguments default action$acl->setNoArgumentsDefaultAction(Acl::DENY);// Returns false$acl->isAllowed("Guests","Customers","search");

<?phpusePhalcon\Acl\RoleAware;// Create our class which will be used as roleNameclassUserRoleimplementsRoleAware{protected$id;protected$roleName;publicfunction__construct($id,$roleName){$this->id=$id;$this->roleName=$roleName;}publicfunctiongetId(){return$this->id;}// Implemented function from RoleAware InterfacepublicfunctiongetRoleName(){return$this->roleName;}}

And our ModelResource class

<?phpusePhalcon\Acl\ResourceAware;// Create our class which will be used as resourceNameclassModelResourceimplementsResourceAware{protected$id;protected$resourceName;protected$userId;publicfunction__construct($id,$resourceName,$userId){$this->id=$id;$this->resourceName=$resourceName;$this->userId=$userId;}publicfunctiongetId(){return$this->id;}publicfunctiongetUserId(){return$this->userId;}// Implemented function from ResourceAware InterfacepublicfunctiongetResourceName(){return$this->resourceName;}}

You can build complex role structures using the inheritance that 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 method call, when adding that role in the list.

<?phpusePhalcon\Acl\Role;// ...// Create some roles$roleAdmins=newRole("Administrators","Super-User role");$roleGuests=newRole("Guests");// Add "Guests" role to ACL$acl->addRole($roleGuests);// Add "Administrators" role inheriting from "Guests" its accesses$acl->addRole($roleAdmins,$roleGuests);

To improve performance 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:

Phalcon\Acl is able to send events to a 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:

<?phpusePhalcon\Acl\Adapter\MemoryasAclList;usePhalcon\Events\Event;usePhalcon\Events\ManagerasEventsManager;// ...// Create an event manager$eventsManager=newEventsManager();// Attach a listener for type "acl"$eventsManager->attach("acl:beforeCheckAccess",function(Event$event,$acl){echo$acl->getActiveRole();echo$acl->getActiveResource();echo$acl->getActiveAccess();});$acl=newAclList();// Setup the $acl// ...// Bind the eventsManager to the ACL component$acl->setEventsManager($eventsManager);