One of the things to do in a web application is controlling the access to the different pages. Zend Framework comes with an Acl component for that purpose.
Our task is make a setup of the Acl instance creating the roles, the resources and the permissions of each role over the resources.
One possible solution can be creating a config file with the roles and its allowed routes. You can also use the inheritance system of the Acl for roles.
The problem with that approach is that we will have repeated code in different configuration files: this one for the Acl and the other config files where the routes are defined.
I’ve created an Acl Module for ZF2 that allows you to put your access control information inside the routes. Something like this:
With that configuration only users with the roles ‘admin’ or ‘user’ would be able to access to that route.
The idea is simple, only the user roles defined under the route can access to that route.
If the array ‘roles’ is empty or if it is not defined, then the route becomes public.
TrascastroACL Module
I have created a Factory to setup the default Acl service (‘Zend\Permissions\Acl\Acl’) parsing the routes of the entire application. So now you will have a new Acl Service ready to use with all the roles, resources (routes) and permissions automatically created.
The code
The GitHub repository for this module is: TrascastroACL
The factory takes the default Acl service from the container and also takes the configuration array. Then with the ‘roles’ and the ‘routes’ defined in config it creates all the resources, roles and allow rules in the Acl service.
The only thing that we have to take care is that each ‘route’ can have ‘child routes’, so we have to parse them recursively.
Installation and configuration
Installation of TrascastroACL uses composer. For composer documentation, please refer to getcomposer.org.
Add the module name ‘TrascastroACL’ to your config/application.config.php
Copy the ‘TrascastroACL.global.dist’ from TrascastroACL config directory and paste it to config/autoload folder removing the ‘.dist’ termination. Now add your application roles and also add the ‘controller’ and the ‘action’ where the ACL will redirect unallowed access tries. You also need to add a role provider:
The role provider must implement the interface ‘TrascastroACL\Provider\RoleProviderInterface’:
This is an example of a role provider class:
Where the Factory would be as follows:
Do not forget to add your provider to your module.config.php:
Usage
Now you can manage your application access control from your routes by simply adding a ‘roles’ key like in this example:
Only users with ‘admin’ or ‘moderator’ roles can now access to that route. If you do not create the ‘roles’ key in a route or you left it empty, then the resource will be public.
Accessing the Acl Service
From a Controller
onBootstrap
From Views
This module provides a View Helper to have access to TrascastroACL in your views:
It is also available using the layout() View Helper: