Skip to content

Instantly share code, notes, and snippets.

@jujhars13
Created May 10, 2013 10:14
Show Gist options
  • Save jujhars13/5553581 to your computer and use it in GitHub Desktop.
Save jujhars13/5553581 to your computer and use it in GitHub Desktop.
zend framework 1 acl using json config file
{
"resources": [
"error::error",
"auth::login",
"auth::logout",
"index::index",
"video::index"
],
"acl": {
"guest": [
"error::error",
"auth::login"
],
"user": [
"index::index",
"video::index",
"auth::logout"
],
"super": [],
"admin": []
}
}
/**
* create the ACL and Auth objects
* get ACL list from /application/config/acl.json
* uses lifetime_very_long memcache in production env (not shown here)
* @link http://www.ens.ro/2012/03/20/zend-authentication-and-authorization-tutorial-with-zend_auth-and-zend_acl/
*/
protected function _initAclAuth()
{
$zend_acl = new Zend_Acl(); //add the roles
$zend_acl->addRole(new Zend_Acl_Role('guest'));
$zend_acl->addRole(new Zend_Acl_Role('user'), array('guest')); //inherits from guest
$zend_acl->addRole(new Zend_Acl_Role('super'), array('user')); //inherits from user
$zend_acl->addRole(new Zend_Acl_Role('administrator'), array('super')); //inherits from super
$zend_acl->allow('administrator'); //by default allow admin access to eveything all resources and all priviledges - mega overlord
try {
$acl_config = new \Zend_Config_Json(APPLICATION_PATH . '/application/config/acl.json');
foreach ($acl_config->resources as $resource) {
$zend_acl->addResource(new Zend_Acl_Resource($resource));
}
foreach ($acl_config->acl as $user => $resources) {
$resource_array = $resources->toArray();
foreach ($resource_array as $resource) {
$zend_acl->allow($user, $resource);
}
}
} catch (\Buto\Exception\Exception $e) {
vd($e);
}
//save to registry
self::$registry->acl = $zend_acl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment