I have a Controller called "TestController" and several actions in it. For each action I receive a get Parameter ("id"). Something like:
test/action1/1
test/action1/2
test/action2/1
test/action3/1
...
Since this parameter can easily be modified I want to check the permissions for this id. Do I have to include this check method in each single action or is there another way? I know that I can't receive params in the constructor but a way like this would be nice.
My solution at the moment would be to have a check method in a plugin and call it in every action like this:
if(!$this->access()->checkAccess(..., $this->params()->fromRoute('id')) {
//redirect...
}
You can use ACL's (or RBAC) to make these checks.
With ACL's you can (must) declare the resources of your application, the roles that use the application and how the roles access the resources.
You can start by attaching a listener to controllers, in the application's
Module.php
As you can see, we attached the plugin
Application\Controller\Plugin\Acl
to thedispatch
event of everyZend\Mvc\Controller\AbstractActionController
.Then you have to implement your ACLS.
Here is a simple exemple. By default, I prefer to deny access to all resources and then, one by one, allow access to them. You can also allow access to everything and then deny access to single resources, but this way you have to be more careful. If you deny all and forget something, an user will not be able to access something that it should be. If you allow all and forget something, user could see something that they shouldn't. Better be safe than sorry ;)
The method
allow
allows you to define these parameters:Finally, after declaring which roles can access a specific action of a specific resource, you can also tell the ACL a "rule", like "access the action only if it this condition is met". These rules are specified through
Assertions
:Hope this will help you!