Skip to content

Commit

Permalink
Remove allowedPrivileges from Resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bukarinov committed Dec 3, 2012
1 parent 709a94b commit a48ad82
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 55 deletions.
16 changes: 0 additions & 16 deletions library/GeometriaLab/Permissions/Assertion/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ abstract class AbstractResource implements ResourceInterface
* @var string
*/
protected $name;
/**
* Array of privileges which always allowed for all
*
* @var array
*/
protected $allowedPrivileges = array();

/**
* @param string $name
Expand All @@ -34,14 +28,4 @@ public function getName()
{
return $this->name;
}

/**
* Get array of privileges which always allowed for all
*
* @return array
*/
public function getAllowedPrivileges()
{
return $this->allowedPrivileges;
}
}
40 changes: 24 additions & 16 deletions library/GeometriaLab/Permissions/Assertion/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,9 @@ public function addResource(ResourceInterface $resource)
*/
public function hasResource($resource)
{
if ($resource instanceof ResourceInterface) {
$resourceId = $resource->getName();
} else {
$resourceId = (string) $resource;
}
$resourceName = self::getResourceName($resource);

return isset($this->resources[$resourceId]);
return isset($this->resources[$resourceName]);
}

/**
Expand All @@ -73,11 +69,7 @@ public function hasResource($resource)
*/
public function getResource($resource)
{
if ($resource instanceof ResourceInterface) {
$resourceName = $resource->getName();
} else {
$resourceName = $resource;
}
$resourceName = self::getResourceName($resource);

if (!$this->hasResource($resource)) {
throw new Exception\InvalidArgumentException("Resource '$resourceName' not found");
Expand All @@ -97,7 +89,11 @@ public function getResource($resource)
*/
public function removeResource($resource)
{
$resourceName = $this->getResource($resource)->getName();
$resourceName = self::getResourceName($resource);

if (!$this->hasResource($resource)) {
throw new Exception\InvalidArgumentException("Resource '$resourceName' not found");
}

unset($this->resources[$resourceName]);

Expand All @@ -119,10 +115,6 @@ public function assert($resource, $privilege, $arg1 = null, $arg2 = null, $argN
{
$resource = $this->getResource($resource);

if (in_array($privilege, $resource->getAllowedPrivileges())) {
return true;
}

$methodName = self::DYNAMIC_ASSERT_PREFIX . ucfirst($privilege);
if (!method_exists($resource, $methodName)) {
throw new Exception\RuntimeException("No rules for privilege '{$privilege}'");
Expand All @@ -136,4 +128,20 @@ public function assert($resource, $privilege, $arg1 = null, $arg2 = null, $argN

return call_user_func_array(array($resource, $methodName), $funcArgs);
}

/**
* Get Resource name
* The $resource parameter can either be a Resource or a Resource identifier.
*
* @param ResourceInterface|string $resource
* @return string
*/
protected static function getResourceName($resource)
{
if ($resource instanceof ResourceInterface) {
return $resource->getName();
}

return (string) $resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ interface ResourceInterface
* @return string
*/
public function getName();

/**
* Get array of privileges which always allowed for all
*
* @return array
*/
public function getAllowedPrivileges();
}
2 changes: 0 additions & 2 deletions library/GeometriaLab/Permissions/Assertion/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace GeometriaLab\Permissions\Assertion;

use GeometriaLab\Api\Mvc\Router\Http\Api;

use Zend\Stdlib\Glob as ZendGlob,
Zend\ServiceManager\FactoryInterface as ZendFactoryInterface,
Zend\ServiceManager\ServiceLocatorInterface as ZendServiceLocatorInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,6 @@ public function testAssertWithoutPrivilege()
$assertion->assert('Bar', 'privilege');
}

public function testAssertWithAllowedPrivilege()
{
$assertion = new Assertion();
$fooResource = new Sample\Foo('Foo');
$assertion->addResource($fooResource);

$this->assertTrue($assertion->assert('Foo', 'allowedForAll'));
}

public function testWithDynamicAssert()
{
$assertion = new Assertion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ public function testCreate()
$resource = new Sample\Foo('Foo');

$this->assertEquals('Foo', $resource->getName());
$this->assertEquals(array('allowedForAll'), $resource->getAllowedPrivileges());
}
}
4 changes: 0 additions & 4 deletions tests/GeometriaLabTest/Permissions/Assertion/Sample/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

class Foo extends AbstractResource
{
protected $allowedPrivileges = array(
'allowedForAll',
);

public function canDynamicAssert(Assertion $assertion, \stdClass $obj, array $array)
{
return !empty($obj) && !empty($array);
Expand Down

0 comments on commit a48ad82

Please sign in to comment.