Since few days I am playing with Zend Framework Access control list(Zend_Acl).
The way Zend has implemented it is fabulous. You cannot even imagine how easy, efficient and flexible it is to implement roles in your application.
Zend_Acl doesn’t require any backend technology such as database, xml etc. You can store data were ever you like. Storing Acl data is totally left on developers.
If you truly like to see the power of Zend_Acl, see the following scenarios.
Suppose you want to develop an application for office, forget about web application for now, and you have employees who can access your contents only at the office timings e.g 9:00 to 5:00.
Or you have a web application for a specific country- user in that specified country can access your application. You want to block all other users from the globe.
Zend Framework acl provide very easy and convenient way of handling such type of situations.
Although this situation would be difficult to handle in simple php, however Zend Framework provides very simple and easy way to handle these situations.
To implement the first scenario, create My/Controller/Helper/TimeAssertion.php and add the following code.
One thing I would like to say is that please read my first article about Zend Acl before implementing what I have explained here.
<?phpClass My_Controller_Helper_TimeAssertion implements Zend_Acl_Assert_Interface
{
public function assert(Zend_Acl $acl,Zend_Acl_Role_Interface $role=null, Zend_Acl_Resource_Interface $resource=null,$privilages=null)
{
return this->_isTime($date);
}
protected function _isTime($date)
{
$timeFrom = new Zend_Date();
$timeFrom->set(“09:00:00″,Zend_Date::TIME);
$timeTo = new Zend_Date();
$timeTo->set(“06:00:00″,Zend_Date::TIME);
if($date > $timeFrom && $dateTo < $timeTo){
return true;
} else {
return false;
}
}
}
?>
In the code above, we first define our class implementing Zend_Acl_Assert_Interface. This interface provides method “assert” function which is compulsory to be implemented when Zend_Acl_Assert_Interface is implemented.
In “asset” we simply call our protected method that return either true or false. The only parameter we are passing is the current date.
In our protected function _isTime, we first define two times. And then check if current date is between those two date. If condition is true, we return true, otherwise false.
Once you define your own class, the only change you will need to do is
$acl->allow(‘employee’,null,’view’,new My_Controller_Helper_TimeAssertion());
That’s it, you don’t need to worry about the other things, because zend will do the magic for you.
Important: Read my first post on Zend_Acl.




