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.
In short, ACLs have two objects: The object that needs access, and the object that we need access to. In the programming world, these are usually referred to as Roles and Resources. In the Phalcon world, we use the terminology Role and Component.
An accounting application needs to have different groups of users have access to various areas of the application.
Role - Administrator Access - Accounting Department Access - Manager Access - Guest Access
As seen above in the use case, a Role is defined as who needs to access a particular Component i.e. an area of the application. A Component is defined as the area of the application that needs to be accessed.
Using the Phalcon\Acl component, we can tie those two together, and strengthen the security of our application, allowing only specific roles to be bound to specific components.
Phalcon\Acl uses adapters to store and work with roles and components. The only adapter available right now is Phalcon\Acl\Adapter\Memory. Having the adapter use the memory, significantly increases the speed that the ACL is accessed but also comes with drawbacks. The main drawback is that memory is not persistent, so the developer will need to implement a storing strategy for the ACL data, so that the ACL is not generated at every request. This could easily lead to delays and unnecessary processing, especially if the ACL is quite big and/or stored in a database or file system.
The Phalcon\Acl constructor takes as its first parameter an adapter used to retrieve the information related to the control list.
The default action is Phalcon\Acl\Enum::DENY for any Role or Component. This is on purpose to ensure that only the developer or application allows access to specific components and not the ACL component itself.
To see this in action, using the example outlined above, we will add the relevant Phalcon\Acl\Role objects in our list.
Role objects. The first parameter is the name of the role, the second the description
<?phpusePhalcon\Acl\Adapter\Memory;usePhalcon\Acl\Role;$acl=newMemory();$roleAdmins=newRole('admins','Administrator Access');$roleAccounting=newRole('accounting','Accounting Department Access');$acl->addRole($roleAdmins);$acl->addRole($roleAccounting);
Strings. Add the role with just the name directly to the ACL:
A Component is the area of the application where access is controlled. In an MVC application, this would be a Controller. Although not mandatory, the Phalcon\Acl\Component class can be used to define components in the application. Also, it is important to add related actions to a component so that the ACL can understand what it should control.
There are two ways of adding components to our list.
After both the Roles and Components have been defined, we need to tie them together so that the access list can be created. This is the most important step in the role since a small mistake here can allow access to roles for components that the developer does not intend to. As mentioned earlier, the default access action for Phalcon\Acl is Phalcon\Acl\Enum::DENY, following the white list approach.
To tie Roles and Components together we use the allow() and deny() methods exposed by the Phalcon\Acl\Memory class.
For the manager role, allow access to the admin component and users action. To bring this into perspective with an MVC application, the above line says that the group manager is allowed to access the admin controller and users action.
You can also pass an array as the action parameter when invoking the allow() command. The above means that for the manager role, allow access to the reports component and list and add actions. Again to bring this into perspective with an MVC application, the above line says that the group manager is allowed to access the reports controller and list and add actions.
Wildcards can also be used to do mass matching for roles, components or actions. In the above example, we allow every role to access every action in the session component. This command will give access to the manager, accounting and guest roles, access to the session component and to the login and logout actions.
Similarly, the above gives access to any role, any component that has the view action. In an MVC application, the above is the equivalent of allowing any group to access any controller that exposes a viewAction.
NOTE: Please be VERY careful when using the * wildcard. It is very easy to make a mistake and the wildcard, although it seems convenient, it may allow users to access areas of your application that they are not supposed to. The best way to be 100% sure is to write tests specifically to test the permissions and the ACL. These can be done in the unit test suite by instantiating the component and then checking the isAllowed() if it is true or false.
Codeception is the chosen testing framework for Phalcon and there are plenty of tests in our GitHub repository (tests folder) to offer guidance and ideas.
For the guest role, we deny access to all components with the view action. Despite the fact that the default access level is Acl\Enum::DENY in our example above, we specifically allowed the view action to all roles and components. This includes the guest role. We want to allow the guest role access only to the session component and the login and logout actions, since guests are not logged into our application.
This gives access to the view access to everyone, but we want the guest role to be excluded from that so the following line does what we need.
Once the list has been defined, we can query it to check if a particular role has access to a particular component and action. To do so, we need to use the isAllowed() method.
Depending on the needs of your application, you might need another layer of calculations to allow or deny access to users through the ACL. The method isAllowed() accepts a 4th parameter which is a callable such as an anonymous function.
To take advantage of this functionality, you will need to define your function when calling the allow() method for the role and component you need. Assume that we need to allow access to all manager roles to the admin component except if their name is ‘Bob’ (Poor Bob!). To achieve this we will register an anonymous function that will check this condition.
03: Set access level for role into components with custom function
04: Returns true
05: Returns false
NOTE:The fourth parameter must be an array. Each array element represents a parameter that your anonymous function accepts. The key of the element is the name of the parameter, while the value is what will be passed as the value of that the parameter of to the function.
You can also omit to pass the fourth parameter to isAllowed() if you wish. The default action for a call to isAllowed() without the last parameter is Acl\Enum::DENY. To change this behavior, you can make a call to setNoArgumentsDefaultAction():
03: Now tie them all together with a custom function. The ManagerRole and ModelSubject parameters are necessary for the custom function to work
04: Create the custom objects
05: id - name - userId
06: Check whether our user objects have access. Returns false
07: Returns true
08: Returns false
The second call for $levelTwo evaluates true since the getUserId() returns 2 which in turn is evaluated in our custom function. Also note that in the custom function for allow() the objects are automatically bound, providing all the data necessary for the custom function to work. The custom function can accept any number of additional parameters. The order of the parameters defined in the function() constructor does not matter, because the objects will be automatically discovered and bound.
To remove duplication and increase efficiency in your application, the ACL offers inheritance in roles. This means that you can define one Phalcon\Acl\Role as a base and after that inherit from it offering access to supersets or subsets of components. To use role inheritance, you need, you need to pass the inherited role as the second parameter of the method call, when adding that role in the list.
Whatever access guests have will be propagated to accounting and in turn accounting will be propagated to manager. You can also pass an array of roles as the second parameter of addRole offering more flexibility.
Based on the application design, you might prefer to add first all the roles and then define the relationship between them.
Phalcon\Acl can be serialized and stored in a cache system to improve efficiency. You can store the serialized object in APC, session, file system, database, Redis etc. This way you can retrieve the ACL quickly without having to read the underlying data that create the ACL nor will you have to compute the ACL in every request.
05: Restore the ACL object from the serialized file
06: Use the ACL list as needed
It is a good practice to not use serialization of the ACL during development, to ensure that your ACL is rebuilt with every request, while other adapters or means of serializing and storing the ACL in production.
Phalcon\Acl can work in conjunction with the Events Manager if present, to fire events to your application. Events are triggered using the type acl. Events that return false can stop the active role. The following events are available:
Can stop role?
Triggered after checking if a role/component has access
Triggered before checking if a role/component has access
The following example demonstrates how to attach listeners to the ACL: