Created
June 10, 2011 16:01
-
-
Save zircote/1019146 to your computer and use it in GitHub Desktop.
Redis/Rediska Backed Zend_Acl Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$rediska = new Rediska(); | |
echo SomeAclService::getInstance('BugsService') | |
->isAllowed('someUser', 'someResource') ? 'allowed' : 'denied'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* This could be a mechanism that builds acl lists from the database or file | |
* based storage. | |
* @author zircote | |
* | |
*/ | |
class Model_BugsServiceAcl | |
{ | |
public function getAcl() | |
{ | |
$acl = new RediskaAcl('BugsService'); | |
$acl->removeRoleAll()->removeAll(); | |
$acl->addRole(new Zend_Acl_Role('guest')) | |
->addRole(new Zend_Acl_Role('member'),'guest') | |
->addRole(new Zend_Acl_Role('admin'), 'member') | |
->addRole(new Zend_Acl_Role('someAdmin'), 'admin') | |
->addRole(new Zend_Acl_Role('someMember'), 'member') | |
->addRole(new Zend_Acl_Role('someGuest'), 'guest') | |
->addResource(new Zend_Acl_Resource('someMemberResource')) | |
->addResource(new Zend_Acl_Resource('someGuestResource')) | |
->addResource(new Zend_Acl_Resource('someAdminResource')) | |
->deny() | |
->allow('guest', 'someGuestResource') | |
->allow('member', 'someMemberResource') | |
->allow('admin', 'someAdminResource'); | |
return $acl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RediskaAcl extends Zend_Acl | |
{ | |
/** | |
* | |
* @var Rediska_Key | |
*/ | |
protected $_redis; | |
/** | |
* | |
* @var string | |
*/ | |
protected $_key; | |
/** | |
* | |
* @param string $key | |
*/ | |
public function __construct($key) | |
{ | |
$this->_key = $key; | |
} | |
/** | |
* | |
*/ | |
protected function _getRedis() | |
{ | |
if(!$this->_redis){ | |
$redis = new Rediska_Key($this->_key); | |
$this->setRedis($redis); | |
} | |
return $this->_redis; | |
} | |
/** | |
* | |
* @param Rediska_Key_Abstract $redis | |
*/ | |
public function setRedis(Rediska_Key_Abstract $redis) | |
{ | |
$this->_redis = $redis; | |
} | |
/** | |
* | |
* @param Zend_Acl $acl | |
*/ | |
public function saveAcl() | |
{ | |
$this->_getRedis()->setValue($this); | |
} | |
/** | |
* Rediska_Key::getOrSetValue checks if the keys exists if not it uses the | |
* provided object and executes the chained method to set the value returned | |
* by the method | |
* @param string $key | |
*/ | |
public function getAcl($key) | |
{ | |
$reflection = new ReflectionClass('Model_'.$key.'Acl'); | |
return $this->_getRedis()->getOrSetValue($reflection->newInstance()) | |
->getAcl(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Rediska.php'; | |
require_once '../example/RediskaAcl.php'; | |
require_once '../example/SomeAclService.php'; | |
require_once '../example/model/BugsServiceAcl.php'; | |
require_once 'PHPUnit/Framework/TestCase.php'; | |
/** | |
* RediskaAcl test case. | |
*/ | |
class RediskaAclTest extends PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @var RediskaAcl | |
*/ | |
private $RediskaAcl; | |
/** | |
* Prepares the environment before running a test. | |
*/ | |
protected function setUp () | |
{ | |
parent::setUp(); | |
$rediska = new Rediska(); | |
$this->RediskaAcl = SomeAclService::getInstance('BugsService'); | |
} | |
/** | |
* Cleans up the environment after running a test. | |
*/ | |
protected function tearDown () | |
{ | |
$this->RediskaAcl = null; | |
parent::tearDown(); | |
} | |
/** | |
* | |
*/ | |
public function testAssertions() | |
{ | |
$acl = $this->RediskaAcl; | |
$this->assertTrue($acl->isAllowed('someGuest', 'someGuestResource')); | |
$this->assertTrue($acl->isAllowed('someAdmin', 'someAdminResource')); | |
$this->assertTrue($acl->isAllowed('someMember', 'someMemberResource')); | |
/* verify proper denial */ | |
$this->assertFalse($acl->isAllowed('someGuest', 'someAdminResource')); | |
$this->assertFalse($acl->isAllowed('someGuest', 'someAdminResource')); | |
$this->assertFalse($acl->isAllowed('someGuest', 'someMemberResource')); | |
$this->assertFalse($acl->isAllowed('someMember', 'someAdminResource')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeAclService | |
{ | |
/** | |
* | |
* Enter description here ... | |
* @var unknown_type | |
*/ | |
private static $_instance = array(); | |
/** | |
* | |
* Enforce singleton and prevent access to constructor. | |
*/ | |
private function __construct() | |
{ | |
} | |
/** | |
* | |
* prevent cloning | |
*/ | |
private function __clone() | |
{ | |
} | |
/** | |
* provide name of an acl container being requested | |
* @return RediskaAcl | |
*/ | |
public static function getInstance($key) | |
{ | |
if( | |
!array_key_exists($key, self::$_instance) || | |
!self::$_instance[$key] instanceof RediskaAcl | |
){ | |
$instance = new RediskaAcl($key); | |
$acl = $instance->getAcl($key); | |
if($acl instanceof RediskaAcl){ | |
self::$_instance[$key] = $acl; | |
} | |
} | |
return self::$_instance[$key]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment