diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48b8bf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..745b946 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "authority", + "description": "An Authorization system for PHP", + "authors": [ + { + "name": "Matthew Machuga", + "email": "machuga@gmail.com" + }, + { + "name": "Koen Schmeets", + "email": "hello@koenschmeets.nl" + } + ], + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "mockery/mockery": "0.7.*" + }, + "minimum-stability": "dev", + "autoload": { + "psr-0": { + "Authority": "src/" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..f7dd28f --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/src/Authority/Rule.php b/src/Authority/Rule.php new file mode 100644 index 0000000..7b1c148 --- /dev/null +++ b/src/Authority/Rule.php @@ -0,0 +1,47 @@ +behavior = $behavior; + $this->action = $action; + $this->setResource($resource); + } + + public function matchesAction($action) + { + return $this->action === $action; + } + + public function matchesResource($resource) + { + $resource = is_object($resource) ? get_class($resource) : $resource; + return $this->resource === $resource; + } + + public function relevant($action, $resource) + { + return $this->matchesAction($action) && $this->matchesResource($resource); + } + + public function setResource($resource) + { + $this->resource = is_object($resource) ? get_class($resource) : $resource; + } + + public function getBehavior() + { + return $this->behavior; + } + + public function getResource() + { + return $this->resource; + } + +} diff --git a/tests/RuleTest.php b/tests/RuleTest.php new file mode 100644 index 0000000..f498bde --- /dev/null +++ b/tests/RuleTest.php @@ -0,0 +1,45 @@ +rule = new Rule(true, 'read', m::mock('Obj')); + } + + public function testCanBeSetToAllowOrDeny() + { + $allowed_rule = new Rule(true, 'read', m::mock('Obj')); + $denied_rule = new Rule(false, 'write', m::mock('Obj')); + $this->assertTrue($allowed_rule->getBehavior()); + $this->assertFalse($denied_rule->getBehavior()); + } + + public function testCanMatchAction() + { + $this->assertTrue($this->rule->matchesAction('read')); + $this->assertFalse($this->rule->matchesAction('write')); + } + + public function testCanMatchResource() + { + $this->assertTrue($this->rule->matchesResource(m::mock('Obj'))); + $this->assertTrue($this->rule->matchesResource('Mockery\\Mock')); + $this->assertFalse($this->rule->matchesResource('Duck')); + } + + public function testCanDetermineRelevance() + { + $this->assertTrue($this->rule->relevant('read', 'Mockery\\Mock')); + $this->assertFalse($this->rule->relevant('write', 'Mockery\\Mock')); + } + + + public function testThis() + { + $this->assertTrue(true); + } +}