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 pull request zendframework/zendframework#7328 from kanellov/fea…
…ture/acl_callback_assertion Acl callback assertion
- Loading branch information
118 parents
ffd80ee
+
f7a5853
+
6efd1b9
+
4425956
+
598e6a5
+
6e40a22
+
90bd82f
+
61e86cc
+
2755e28
+
666e28e
+
7c5abb8
+
d753388
+
e33d218
+
b0d3cc2
+
05539b9
+
56091aa
+
c47d6e1
+
dcee015
+
6c132b6
+
7065efb
+
e63c913
+
b34a30f
+
f6898d8
+
9d8ce42
+
e8926e5
+
5f59463
+
30b6149
+
843dd21
+
6d04042
+
98ed105
+
ea632d6
+
c2c0c90
+
4d8678c
+
b5fe867
+
dd6dd90
+
fe69d86
+
7e4c1bb
+
c718944
+
032f2c6
+
e587c4a
+
afb2df9
+
4ea8d76
+
9dfff7d
+
c88317e
+
2be5985
+
9490459
+
8e68e61
+
b111673
+
18367f4
+
f312673
+
d7f78c7
+
82f434d
+
9355d07
+
9cc8996
+
202d5af
+
4dff9cb
+
8834dfc
+
81b48da
+
480b4d7
+
df42df9
+
3ee9c5e
+
b7556e5
+
4c81c23
+
9c8309f
+
35ab5f0
+
85e1f91
+
3cb23c1
+
16f6088
+
d32c68c
+
df5b9c3
+
1936d7e
+
c478319
+
9fafd09
+
e102ab1
+
a90bdee
+
2f9fa9f
+
aad5075
+
4127f0d
+
eda7358
+
25538ec
+
ff2c116
+
ee7f5d8
+
60fd2f8
+
33976d4
+
446a94e
+
fbb88bb
+
78a9131
+
d4d4e35
+
444b3aa
+
51abe83
+
af16339
+
d531e0a
+
46c7d78
+
25d96a9
+
71d7e50
+
03600cd
+
f2f56b8
+
d78b598
+
a6922a1
+
40b06f5
+
7d1ac75
+
f0465d5
+
3f7be91
+
27b402a
+
1345451
+
e9e88fa
+
648f655
+
11f8f36
+
8197fbb
+
2a471ea
+
fe80897
+
f6f2e50
+
0b0660c
+
bc76836
+
14ee647
+
1248d3c
+
fd14880
+
7c9a3ec
commit db38482
Showing
2 changed files
with
140 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,60 @@ | ||
<?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\Acl\Assertion; | ||
|
||
use Zend\Permissions\Acl\Acl; | ||
use Zend\Permissions\Acl\Exception\InvalidArgumentException; | ||
use Zend\Permissions\Acl\Resource\ResourceInterface; | ||
use Zend\Permissions\Acl\Role\RoleInterface; | ||
|
||
class CallbackAssertion implements AssertionInterface | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
protected $callback; | ||
|
||
/** | ||
* Class constructor. | ||
* | ||
* @param callable $callback the autentication callback | ||
*/ | ||
public function __construct($callback) | ||
{ | ||
if (!is_callable($callback)) { | ||
throw new InvalidArgumentException('Invalid callback provided; not callable'); | ||
} | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* Returns true if and only if the assertion conditions are met. | ||
* | ||
* This method is passed the ACL, Role, Resource, and privilege to which the | ||
* authorization query applies. If the | ||
* $role, $resource, or $privilege parameters are null, it means that the | ||
* query applies to all Roles, Resources, or privileges, respectively. | ||
* | ||
* @param Acl $acl | ||
* @param RoleInterface $role | ||
* @param ResourceInterface $resource | ||
* @param string $privilege | ||
* | ||
* @return bool | ||
*/ | ||
public function assert( | ||
Acl $acl, | ||
RoleInterface $role = null, | ||
ResourceInterface $resource = null, | ||
$privilege = null | ||
) { | ||
return (bool) call_user_func($this->callback, $acl, $role, $resource, $privilege); | ||
} | ||
} |
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,80 @@ | ||
<?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\Acl\Assertion; | ||
|
||
use Zend\Permissions\Acl; | ||
|
||
class CallbackAssertionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Ensures constructor throws InvalidArgumentException if not callable is provided | ||
*/ | ||
public function testConstructorThrowsExceptionIfNotCallable() | ||
{ | ||
$this->setExpectedException( | ||
'Zend\Permissions\Acl\Exception\InvalidArgumentException', | ||
'Invalid callback provided; not callable' | ||
); | ||
new Acl\Assertion\CallbackAssertion('I\'m not callable!'); | ||
} | ||
|
||
/** | ||
* Ensures callback is set in object | ||
*/ | ||
public function testCallbackIsSet() | ||
{ | ||
$callback = function () {}; | ||
$assert = new Acl\Assertion\CallbackAssertion($callback); | ||
$this->assertAttributeSame($callback, 'callback', $assert); | ||
} | ||
|
||
/** | ||
* Ensures assert method provides callback with its arguments | ||
*/ | ||
public function testAssertMethodPassArgsToCallback() | ||
{ | ||
$acl = new Acl\Acl(); | ||
$that = $this; | ||
$assert = new Acl\Assertion\CallbackAssertion( | ||
function ($aclArg, $roleArg, $resourceArg, $privilegeArg) use ($that, $acl) { | ||
$that->assertSame($acl, $aclArg); | ||
$that->assertInstanceOf('Zend\Permissions\Acl\Role\RoleInterface', $roleArg); | ||
$that->assertEquals('guest', $roleArg->getRoleId()); | ||
$that->assertInstanceOf('Zend\Permissions\Acl\Resource\ResourceInterface', $resourceArg); | ||
$that->assertEquals('area1', $resourceArg->getResourceId()); | ||
$that->assertEquals('somePrivilege', $privilegeArg); | ||
return false; | ||
} | ||
); | ||
|
||
$acl->addRole('guest'); | ||
$acl->addResource('area1'); | ||
$acl->allow(null, null, null, $assert); | ||
$this->assertFalse($acl->isAllowed('guest', 'area1', 'somePrivilege')); | ||
} | ||
|
||
/** | ||
* Ensures assert method returns callback's function value | ||
*/ | ||
public function testAssertMethod() | ||
{ | ||
$acl = new Acl\Acl(); | ||
$roleGuest = new Acl\Role\GenericRole('guest'); | ||
$assertMock = function ($value) { | ||
return function ($aclArg, $roleArg, $resourceArg, $privilegeArg) use ($value) { | ||
return $value; | ||
}; | ||
}; | ||
$acl->addRole($roleGuest); | ||
$acl->allow($roleGuest, null, 'somePrivilege', new Acl\Assertion\CallbackAssertion($assertMock(true))); | ||
$this->assertTrue($acl->isAllowed($roleGuest, null, 'somePrivilege')); | ||
$acl->allow($roleGuest, null, 'somePrivilege', new Acl\Assertion\CallbackAssertion($assertMock(false))); | ||
$this->assertFalse($acl->isAllowed($roleGuest, null, 'somePrivilege')); | ||
} | ||
} |