Skip to content

Commit

Permalink
Add assert aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
visa4 committed Jan 28, 2017
1 parent d153c69 commit ca7c65f
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 74 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ Finally, our storage configuration.
],
'Resource2' => [
[
'assert' => 'assertAlias',
'assert' => [
'assertAlias' => [
'config' => 'test'
],
],
'allow' => true,
'privilege' => 'view'
]
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"require-dev": {
"phpunit/phpunit": "~4.2",
"zendframework/zend-mvc": "~2.3",
"zendframework/zend-mvc": "~2.5.1",
"satooshi/php-coveralls": "dev-master"
},
"authors": [
Expand Down
78 changes: 71 additions & 7 deletions library/Assertion/AssertionAggregate.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,79 @@
<?php
namespace AclMan\Assertion;

use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Assertion\AssertionAggregate as BaseAssertionAggregate;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\Permissions\Acl\Exception\RuntimeException;
use Zend\Permissions\Acl\Resource\ResourceInterface;
use Zend\Permissions\Acl\Role\RoleInterface;

/**
* Created by PhpStorm.
* User: visa
* Date: 26/01/17
* Time: 12.22
* Class AssertionAggregate
*/
class AssertionAggregate extends BaseAssertionAggregate
{
public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
{
// check if assertions are set
if (!$this->assertions) {
throw new RuntimeException('No assertion have been aggregated to this AssertionAggregate');
}

namespace AclMan\Assertion;
foreach ($this->assertions as $key => $assertions) {
switch (true) {
case is_array($assertions):
if (!$this->getAssertionManager()) {
throw new RuntimeException('No assertion manager is set - cannot look up for assertions');
}

$name = $assertions;
$option = [];

class AssertionAggregate
{
if (!isset($assertions['name'])) {
throw new RuntimeException('Name not set in the assertion');
}

$name = $assertions['name'];
unset($assertions['name']);
$option = $assertions;

$assertion = $this->getAssertionManager()->get($name, $option);
break;
case is_string($assertions) && class_exists($assertions):
$assertion = new $assertions();
if (!($assertion instanceof AssertionInterface)) {
throw new RuntimeException(sprintf('Instace of %s is not an instance of Zend\Permissions\Acl\Assertion\AssertionInterface', get_class($assertion)));
}
break;
case is_string($assertions):
if (!$this->getAssertionManager()) {
throw new RuntimeException('No assertion manager is set - cannot look up for assertions');
}
if ($this->getAssertionManager()->has($assertions)) {
$assertion = $this->getAssertionManager()->get($assertions);
}
break;
default:
throw new RuntimeException('Invalid params in aggregate Assertion');
}
$result = (bool)$assertion->assert($acl, $role, $resource, $privilege);
if ($this->getMode() == self::MODE_ALL && !$result) {
// on false is enough
return false;
}

if ($this->getMode() == self::MODE_AT_LEAST_ONE && $result) {
// one true is enough
return true;
}
}

if ($this->getMode() == self::MODE_ALL) {
// none of the assertions returned false
return true;
} else {
return false;
}
}
}
23 changes: 15 additions & 8 deletions library/Assertion/AssertionManager.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: visa
* Date: 27/01/17
* Time: 17.19
*/

namespace AclMan\Assertion;

use Zend\Permissions\Acl\Assertion\AssertionManager as BaseAssertionManager;

class AssertionManager
/**
* Class AssertionManager
*/
class AssertionManager extends BaseAssertionManager
{
/**
* zend-servicemanager v3 compatibility
* @var bool
*/
protected $shareByDefault = false;

/**
* zend-servicemanager v2 compatibility
* @var bool
*/
protected $sharedByDefault = false;
}
1 change: 0 additions & 1 deletion library/Assertion/AssertionManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
namespace AclMan\Assertion;

use Zend\Permissions\Acl\Assertion\AssertionManager;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand Down
4 changes: 2 additions & 2 deletions library/Permission/PermissionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ interface PermissionInterface extends ResourceInterface, RoleInterface
/**
* Retrieve assertion
*
* @return string
* @return array
*/
public function getAssertion();

/**
* Set assertion
*
* @param string $assert
* @param array $assert
* @return $this
*/
public function setAssertion($assert);
Expand Down
18 changes: 17 additions & 1 deletion library/Service/ServiceAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace AclMan\Service;

use AclMan\Acl\AclAwareTrait;
use AclMan\Assertion\AssertionAggregate;
use AclMan\Assertion\AssertionAwareTrait;
use AclMan\Permission\GenericPermission;
use AclMan\Resource\ResourceCheckTrait;
Expand Down Expand Up @@ -126,8 +127,23 @@ public function loadResource($role = null, $resource = null)
foreach ($permissions as $permission) {
$assert = null;
if ($permission->getAssertion()) {
$assertConfig = $permission->getAssertion();
/** @var $assert AssertionInterface */
$assert = $this->getPluginManager()->get($permission->getAssertion());
if (is_array($assertConfig)) {
if (is_array(current($assertConfig)) || count($assertConfig) > 1) {

/** @var $assert AssertionAggregate */
$assert = new AssertionAggregate();
$assert->setAssertionManager($this->getPluginManager());
foreach ($assertConfig as $item) {
$assert->addAssertion($item);
}
} elseif (count($assertConfig) == 1) {
$assert = $this->getPluginManager()->get(current($assertConfig));
}
} else {
$assert = $this->getPluginManager()->get($permission->getAssertion());
}
}
// When load multiple resource
if ($permission->getResourceId() && !$this->getAcl()->hasResource($permission->getResourceId())) {
Expand Down
162 changes: 131 additions & 31 deletions tests/Assertion/AssertionAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
*/
namespace AclManTest\Assertion;

use AclMan\Assertion\AssertionAggregate;
use AclManTest\AclManTestCase;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\Permissions\Acl\Assertion\AssertionManager;
use Zend\ServiceManager;

/**
* Class AssertionManagerFactoryTest
* Class AssertionAggregateTest
*/
class AssertionManagerFactoryTest extends AclManTestCase
class AssertionAggregateTest extends AclManTestCase
{
/**
* @var \Zend\ServiceManager\ServiceManager
Expand All @@ -38,43 +39,142 @@ public function setUp()
$sm->setService('Config', $config);
}

public function testAssertPluginManager()
/**
* @expectedException Zend\Permissions\Acl\Exception\RuntimeException
*/
public function testNotAssertionException()
{
$pluginManager = $this->serviceManager->get('assertManager');
$this->assertInstanceOf('Zend\Permissions\Acl\Assertion\AssertionManager', $pluginManager);
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->assert($this->getMock('Zend\Permissions\Acl\Acl'), $this->getMock('Zend\Permissions\Acl\Role\RoleInterface'));
}

public function testAssetPluginManagerConfig()
/**
* @expectedException Zend\Permissions\Acl\Exception\RuntimeException
*/
public function testNotAssertionManagerWithStringException()
{
$config = [
'factories' => [
'assertManager' => 'AclMan\Assertion\AssertionManagerFactory',
],
'aclman-assertion-manager' => [
'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1' =>
'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1',
'invokables' => [
'assert' => 'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1',
]
]
];
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions(['test']);
$assertionAggregate->assert($this->getMock('Zend\Permissions\Acl\Acl'), $this->getMock('Zend\Permissions\Acl\Role\RoleInterface'));
}

$sm = new ServiceManager\ServiceManager(
new ServiceManagerConfig($config)
);
/**
* @expectedException Zend\Permissions\Acl\Exception\RuntimeException
*/
public function testNotAssertionManagerWithArrayException()
{
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions(['test' => ['test']]);
$assertionAggregate->assert($this->getMock('Zend\Permissions\Acl\Acl'), $this->getMock('Zend\Permissions\Acl\Role\RoleInterface'));
}

$sm->setService('Config', $config);
/**
* @expectedException Zend\Permissions\Acl\Exception\RuntimeException
*/
public function testNotAssertionManagerWithArrayExceptionNoName()
{
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions(['test' => ['test' => 'test']]);
$assertionAggregate->setAssertionManager($this->getMock('Zend\Permissions\Acl\Assertion\AssertionManager'));
$assertionAggregate->assert($this->getMock('Zend\Permissions\Acl\Acl'), $this->getMock('Zend\Permissions\Acl\Role\RoleInterface'));
}


/**
*
*/
public function testNameAssert()
{
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions(['test' => ['name' => 'nametest', 'test' => 'test']]);

$mockService = $this->getMock('Zend\Permissions\Acl\Assertion\AssertionInterface');

$mock = $this->getMock('Zend\Permissions\Acl\Assertion\AssertionManager');
$mock->method('get')->willReturn($mockService);

/** @var $pluginManager AssertionManager */
$pluginManager = $sm->get('assertManager');
$this->assertInstanceOf('Zend\Permissions\Acl\Assertion\AssertionManager', $pluginManager);
$this->assertInstanceOf(
'Zend\Permissions\Acl\Assertion\AssertionInterface',
$pluginManager->get('AclManTest\Assertion\TestAsset\Assertion\MockAssertion1')

$assertionAggregate->setAssertionManager($mock);
$this->assertFalse($assertionAggregate->assert($this->getMock('Zend\Permissions\Acl\Acl'), $this->getMock('Zend\Permissions\Acl\Role\RoleInterface')));
}

/**
* @expectedException Zend\Permissions\Acl\Exception\RuntimeException
*/
public function testNotAssertionManagerWithNameClassException()
{
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions(["stdClass"]);

$this->assertFalse($assertionAggregate->assert($this->getMock('Zend\Permissions\Acl\Acl'), $this->getMock('Zend\Permissions\Acl\Role\RoleInterface')));
}

/**
* @expectedException Zend\Permissions\Acl\Exception\RuntimeException
*/
public function testNotAssertionManagerWithClassException()
{
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions([new \stdClass()]);

$assertionAggregate->assert($this->getMock('Zend\Permissions\Acl\Acl'), $this->getMock('Zend\Permissions\Acl\Role\RoleInterface'));
}

/**
*
*/
public function testNameClassAssert()
{
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions(['AclManTest\Integration\Service\TestAsset\Assertion\Assertion1']);

$this->assertFalse($assertionAggregate->assert($this->getMock('Zend\Permissions\Acl\Acl'), $this->getMock('Zend\Permissions\Acl\Role\RoleInterface')));
}

/**
*
*/
public function testNameAssertFalse()
{
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions(['test']);

$mockService = $this->getMock('Zend\Permissions\Acl\Assertion\AssertionInterface');

$mock = $this->getMock('Zend\Permissions\Acl\Assertion\AssertionManager');
$mock->method('get')->willReturn($mockService);
$mock->method('has')->willReturn(true);

$assertionAggregate->setAssertionManager($mock);
$this->assertFalse(
$assertionAggregate->assert(
$this->getMock('Zend\Permissions\Acl\Acl'),
$this->getMock('Zend\Permissions\Acl\Role\RoleInterface')
)
);
$this->assertInstanceOf(
'Zend\Permissions\Acl\Assertion\AssertionInterface',
$pluginManager->get('assert')
}

/**
*
*/
public function testNameAssertTrue()
{
$assertionAggregate = new AssertionAggregate();
$assertionAggregate->addAssertions(['test']);

$mockService = $this->getMock('Zend\Permissions\Acl\Assertion\AssertionInterface');
$mockService->method('assert')->willReturn(true);

$mock = $this->getMock('Zend\Permissions\Acl\Assertion\AssertionManager');
$mock->method('get')->willReturn($mockService);
$mock->method('has')->willReturn(true);

$assertionAggregate->setAssertionManager($mock);
$this->assertTrue(
$assertionAggregate->assert(
$this->getMock('Zend\Permissions\Acl\Acl'),
$this->getMock('Zend\Permissions\Acl\Role\RoleInterface')
)
);
}
}
Loading

0 comments on commit ca7c65f

Please sign in to comment.