This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/7327' into develop
- Loading branch information
84 parents
89fab96
+
46f26e3
+
9d09cf8
+
125d1a7
+
7cd78b0
+
0acda84
+
97a573b
+
07cafb7
+
9c547a6
+
fce98b5
+
f5a878b
+
99d2b61
+
0236fd9
+
94d722b
+
9515119
+
3453a81
+
c65c180
+
d9cce25
+
9a04dec
+
c948ef1
+
bb8d758
+
681350e
+
535bf8a
+
6308000
+
43c0375
+
b6882cc
+
5ab9938
+
aee3039
+
d0c6f97
+
def4669
+
1d9336b
+
186c74b
+
06a51e4
+
3c55c64
+
8673a50
+
c5e096f
+
21a2d03
+
63799fd
+
457cc49
+
15924e3
+
7f82fa6
+
4b06447
+
b2bc2d7
+
581d4ea
+
2208cf2
+
782856d
+
af684a7
+
a30a853
+
c39d541
+
323eff0
+
efa008a
+
bc6d3ee
+
b2d0cd0
+
f86fbc9
+
92dce0d
+
4299d35
+
39ca271
+
c515d60
+
7df306c
+
256605f
+
2a58924
+
45a72c8
+
6027334
+
695e48e
+
04ea4de
+
dc96c4e
+
f96c20d
+
9edb640
+
24ccb84
+
7535ce5
+
641daf5
+
d1a34a2
+
22e77d8
+
9c10c4b
+
d60eac9
+
17352c9
+
9a952d8
+
e23343c
+
928cbdc
+
4dbccf4
+
d38894e
+
de0ba9e
+
385cfff
+
0f054d1
commit 65cb65f
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/). | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
namespace Zend\Permissions\Rbac\Assertion; | ||
|
||
use Zend\Permissions\Rbac\AssertionInterface; | ||
use Zend\Permissions\Rbac\Exception\InvalidArgumentException; | ||
use Zend\Permissions\Rbac\Rbac; | ||
|
||
class CallbackAssertion implements AssertionInterface | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
protected $callback; | ||
|
||
/** | ||
* @param callable $callback The assertion callback | ||
*/ | ||
public function __construct($callback) | ||
{ | ||
if (! is_callable($callback)) { | ||
throw new InvalidArgumentException('Invalid callback provided; not callable'); | ||
} | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* Assertion method - must return a boolean. | ||
* | ||
* Returns the result of the composed callback. | ||
* | ||
* @param Rbac $rbac | ||
* @return bool | ||
*/ | ||
public function assert(Rbac $rbac) | ||
{ | ||
return (bool) call_user_func($this->callback, $rbac); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
namespace ZendTest\Permissions\Rbac\Assertion; | ||
|
||
use Zend\Permissions\Rbac; | ||
|
||
class CallbackAssertionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Ensures constructor throws InvalidArgumentException if not callable is provided | ||
*/ | ||
public function testConstructorThrowsExceptionIfNotCallable() | ||
{ | ||
$this->setExpectedException( | ||
'Zend\Permissions\Rbac\Exception\InvalidArgumentException', | ||
'Invalid callback provided; not callable' | ||
); | ||
new Rbac\Assertion\CallbackAssertion('I am not callable!'); | ||
} | ||
|
||
/** | ||
* Ensures callback is set in object | ||
*/ | ||
public function testCallbackIsSet() | ||
{ | ||
$callback = function () { | ||
}; | ||
$assert = new Rbac\Assertion\CallbackAssertion($callback); | ||
$this->assertAttributeSame($callback, 'callback', $assert); | ||
} | ||
|
||
/** | ||
* Ensures assert method provides callback with rbac as argument | ||
*/ | ||
public function testAssertMethodPassRbacToCallback() | ||
{ | ||
$rbac = new Rbac\Rbac(); | ||
$that = $this; | ||
$assert = new Rbac\Assertion\CallbackAssertion(function ($rbacArg) use ($that, $rbac) { | ||
$that->assertSame($rbacArg, $rbac); | ||
return false; | ||
}); | ||
$foo = new Rbac\Role('foo'); | ||
$foo->addPermission('can.foo'); | ||
$this->assertFalse($rbac->isGranted($foo, 'can.foo', $assert)); | ||
} | ||
|
||
/** | ||
* Ensures assert method returns callback's function value | ||
*/ | ||
public function testAssertMethod() | ||
{ | ||
$rbac = new Rbac\Rbac(); | ||
$foo = new Rbac\Role('foo'); | ||
$bar = new Rbac\Role('bar'); | ||
|
||
$assertRoleMatch = function ($role) { | ||
return function ($rbac) use ($role) { | ||
return $role->getName() == 'foo'; | ||
}; | ||
}; | ||
|
||
$roleNoMatch = new Rbac\Assertion\CallbackAssertion($assertRoleMatch($bar)); | ||
$roleMatch = new Rbac\Assertion\CallbackAssertion($assertRoleMatch($foo)); | ||
|
||
$foo->addPermission('can.foo'); | ||
$bar->addPermission('can.bar'); | ||
|
||
$rbac->addRole($foo); | ||
$rbac->addRole($bar); | ||
|
||
$this->assertFalse($rbac->isGranted($bar, 'can.bar', $roleNoMatch)); | ||
$this->assertFalse($rbac->isGranted($bar, 'can.foo', $roleNoMatch)); | ||
$this->assertTrue($rbac->isGranted($foo, 'can.foo', $roleMatch)); | ||
} | ||
} |