Download the PHP package jkardynia/acl-annot without Composer
On this page you can find all versions of the php package jkardynia/acl-annot. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jkardynia/acl-annot
More information about jkardynia/acl-annot
Files in jkardynia/acl-annot
Package acl-annot
Short Description Acl annotations for Zend Framewrok 2
License GPL
Informations about the package acl-annot
acl-annotations
Annotations for ACL with Zend Framework 2. This package allows you to write ACL rules just in your Zend controllers using annotations.
Installation
You can install it via composer. Just add this to your dependencies in composer.json:
"jkardynia/acl-annot": "*"
Usage
Using ACL annotations is very simple. All you need to do is to use @Acl annotation to define access rule for user roles in place where you are defining your controllers.
Adding rules in controllers
On the snippnet below I will show you how to use ACL annotations with AlbumController - example form Zend Framework 2 tutorial Getting Started with Zend Framework 2 - Create the controller
There is not many changes. I only imported ACL annotation class by use keyword and use this annotations. As you can see I gave access only to indexAction for guest, and to all actions for admin.
This was defining ACL rules but before this will start working your module must collect all rules.
Building Access Controll List
ACL should be filled just at the begining, when aplication is initialized. With this in mind you should preapare your module by attaching new closures to MvcEvent. You can do it like that:
Now we have list of access controlls and we can use it to check access for current user. Notice that I added private field \Zend\Permissions\Acl\Acl $acl which is initialized in ACL initialization callback. We will need this later.
Checking access
Checking if user has access to our controller is main purpose for ACL system. We should check access in ActionController dispach. To do that, first we must add new event subscriber (callback) in Module.
What happens there? We are just attaching callback to proper event. The most important thing is:
There we get Acl plugin from ControllerPluginManager and use it to check access. If access is denied exception will be thrown so I catch it and do redirect to access-denied info page.
There is last thing to do. We must register Acl Plugin.
Registering ACL plugin
To register any new plugin in your module you must just add a line to your module.config.php file:
Now everything is set - you can use annotations to provide and check access to your controllers :).
Performance
To be honest annotations are slow and if you want to use them in real applications you should turn on caching. Fortunately there is nice caching system proveded by Doctrine Annotations which I use in this project. Thanks of Doctrine dvelopers you can use one of provided cache strategies, for example: APC, Memcache, Files etc. You can easly configure cache in this ACL annotations package, right from your Module. All you need to do is initialize AclItemsCollector with Reader that supports caching. This is example for filesystem cache:
You can find more information about annotation cache in great [Doctrine Annotations documentation] (http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html#setup-and-configuration).
Final thoughts
This is very basic implementation of ACL annotations package and there is still a lot of thing to do but it could be used in developement environment. Feel free to involve. :)
TODO
There is a lot of things still to do. To mention the most important:
- using more than one acl annotaion for one action
- more flexible addition of resource class
- defining privileges
All versions of acl-annot with dependencies
zendframework/zend-permissions-acl Version 2.2.*
zendframework/zend-mvc Version 2.2.*
doctrine/common Version 2.3.0