Zend_Acl Db Storage
I’ve spent the last few hours trying to extend Zend_Acl_Role_Registry and Zend_Acl to support Db storage, when it just hit me what I’m actually trying to do, and a much easier way to do it!
Zend_Acl Storage Mechanism
Zend_Acl doesn’t store your ACL’s and ARO’s it simply holds it at run time and leaves it to the developer to find ways of storing the ACL’s and ARO’s for persistence, the recommendation from ZF is to serialize the ACL object and store it either as a file, cache it or insert it into a DB or some kind of other persistent storage.
What I’m Trying To Do
I’m trying to store this data in multiple tables and retrieve the ACL and ARO rules when a new instance of the Zend_Acl object is called, calls to methods such as ‘addRole’, ‘allow’, ‘isAllowed’ etc would be overwritten by the inherited App_Acl_Role_Registry and App_Acl classes and would call on the database, retrieve data and pass it back to the role registry. Rewriting each method was proving to be very time consuming, and thinking about it, having to tie Zend_Db into a lot of methods is a little bit worrying, as we want it to be loosly coupled, and it would be nice if we could pass our own storage adapters if needed…
__construct and __destruct, ahhhh the penny drops! I can extend Zend_Acl and override the __construct method and create a new __destruct method, this gives me the perfect opertunity to connect to the persistant storage using one of several (hopefully) adapters, and populate the rules within Zend_Acl when a new Zend_Acl/App_Acl object is instantiated. But what about saving the data I hear you say? This is where __destruct comes in, as soon as the object is no longer required, or the script ends, I can use __destruct to store the ACL and ARO rules back into the persistance layer using the adapter, for large web applications where many users are amending ACL’s and ARO’s this isn’t idea, it doesn’t take into consideration race conditions when writing back to the persistance layer, unless you provide table locking on your database or file locks in your files. How’s my aim? Proof of concept to come soon!