-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds delegator to set ACL for helpers
The delegator allows the setting a ACL for navigation helpers by using a registered `Laminas\Permissions\Acl\AclInterface` service. Signed-off-by: Frank Brückner <[email protected]>
- Loading branch information
1 parent
3651e2a
commit 2afc10f
Showing
2 changed files
with
212 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,57 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/laminas/laminas-navigation for the canonical source repository | ||
* @copyright https://github.com/laminas/laminas-navigation/blob/master/COPYRIGHT.md | ||
* @license https://github.com/laminas/laminas-navigation/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Navigation\View; | ||
|
||
use Laminas\Permissions\Acl\AclInterface; | ||
use Laminas\View\Helper\Navigation\AbstractHelper; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class PermissionAclDelegatorFactory | ||
{ | ||
/** @var string */ | ||
private $aclName; | ||
|
||
public function __construct(string $aclName = AclInterface::class) | ||
{ | ||
$this->aclName = $aclName; | ||
} | ||
|
||
public static function __set_state(array $state): self | ||
{ | ||
return new self($state['aclName'] ?? AclInterface::class); | ||
} | ||
|
||
public function __invoke( | ||
ContainerInterface $container, | ||
string $name, | ||
callable $callback, | ||
array $options = null | ||
) { | ||
/** @var AbstractHelper|mixed $instance */ | ||
$helper = $callback(); | ||
|
||
if (! $helper instanceof AbstractHelper) { | ||
return $helper; | ||
} | ||
if (! $container->has($this->aclName)) { | ||
return $helper; | ||
} | ||
|
||
$acl = $container->get($this->aclName); | ||
if (! $acl instanceof AclInterface) { | ||
return $helper; | ||
} | ||
|
||
$helper->setAcl($acl); | ||
|
||
return $helper; | ||
} | ||
} |
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,155 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/laminas/laminas-navigation for the canonical source repository | ||
* @copyright https://github.com/laminas/laminas-navigation/blob/master/COPYRIGHT.md | ||
* @license https://github.com/laminas/laminas-navigation/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Navigation\View; | ||
|
||
use Laminas\Navigation\View\PermissionAclDelegatorFactory; | ||
use Laminas\Permissions\Acl\AclInterface; | ||
use Laminas\View\Helper\Navigation; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use stdClass; | ||
|
||
class PermissionAclDelegatorFactoryTest extends TestCase | ||
{ | ||
public function testAclShouldBeSetForHelper(): void | ||
{ | ||
$container = $this->createMock(ContainerInterface::class); | ||
$container->expects($this->once()) | ||
->method('has') | ||
->with(AclInterface::class) | ||
->willReturn(true); | ||
$container->expects($this->once()) | ||
->method('get') | ||
->with(AclInterface::class) | ||
->willReturn($this->createMock(AclInterface::class)); | ||
|
||
/** @var Navigation $result */ | ||
$result = (new PermissionAclDelegatorFactory())( | ||
$container, | ||
'name', | ||
static function () { | ||
return new Navigation(); | ||
} | ||
); | ||
|
||
$this->assertInstanceOf( | ||
AclInterface::class, | ||
$result->getAcl() | ||
); | ||
} | ||
|
||
public function testAclWithCustomNameShouldBeSetForHelper(): void | ||
{ | ||
$customName = 'alternate-acl'; | ||
|
||
$container = $this->createMock(ContainerInterface::class); | ||
$container->expects($this->once()) | ||
->method('has') | ||
->with($customName) | ||
->willReturn(true); | ||
$container->expects($this->once()) | ||
->method('get') | ||
->with($customName) | ||
->willReturn($this->createMock(AclInterface::class)); | ||
|
||
/** @var Navigation $result */ | ||
$result = (new PermissionAclDelegatorFactory($customName))( | ||
$container, | ||
'name', | ||
static function () { | ||
return new Navigation(); | ||
} | ||
); | ||
|
||
$this->assertInstanceOf( | ||
AclInterface::class, | ||
$result->getAcl() | ||
); | ||
} | ||
|
||
public function testNoneNavigationHelperPassedAsGiven(): void | ||
{ | ||
$class = new stdClass(); | ||
|
||
/** @var Navigation $result */ | ||
$result = (new PermissionAclDelegatorFactory())( | ||
$this->createMock(ContainerInterface::class), | ||
'name', | ||
static function () use ($class) { | ||
return $class; | ||
} | ||
); | ||
|
||
$this->assertSame($class, $result); | ||
} | ||
|
||
public function testNoneExistingAclWillHelperPassedAsGiven(): void | ||
{ | ||
$container = $this->createMock(ContainerInterface::class); | ||
$container->expects($this->once()) | ||
->method('has') | ||
->with(AclInterface::class) | ||
->willReturn(false); | ||
|
||
/** @var Navigation $result */ | ||
$result = (new PermissionAclDelegatorFactory())( | ||
$container, | ||
'name', | ||
static function () { | ||
return new Navigation(); | ||
} | ||
); | ||
|
||
$this->assertNull($result->getAcl()); | ||
} | ||
|
||
public function testWrongAclWillHelperPassedAsGiven(): void | ||
{ | ||
$container = $this->createMock(ContainerInterface::class); | ||
$container->expects($this->once()) | ||
->method('has') | ||
->with(AclInterface::class) | ||
->willReturn(true); | ||
$container->expects($this->once()) | ||
->method('get') | ||
->with(AclInterface::class) | ||
->willReturn(null); | ||
|
||
/** @var Navigation $result */ | ||
$result = (new PermissionAclDelegatorFactory())( | ||
$container, | ||
'name', | ||
static function () { | ||
return new Navigation(); | ||
} | ||
); | ||
|
||
$this->assertNull($result->getAcl()); | ||
} | ||
|
||
public function testMagicMethodSetStateShouldContainAclClassName(): void | ||
{ | ||
$this->assertStringContainsString( | ||
'Laminas\\\Permissions\\\Acl\\\AclInterface', | ||
var_export(new PermissionAclDelegatorFactory(), true) | ||
); | ||
} | ||
|
||
public function testMagicMethodSetStateShouldContainCustomNameIfSet(): void | ||
{ | ||
$customName = 'alternate-name'; | ||
|
||
$this->assertStringContainsString( | ||
$customName, | ||
var_export(new PermissionAclDelegatorFactory($customName), true) | ||
); | ||
} | ||
} |