From 0f36da7259dd483affe1bba5601b7a8cd1a8aba5 Mon Sep 17 00:00:00 2001 From: Glenn Eggleton Date: Fri, 8 Jan 2016 12:08:30 -0500 Subject: [PATCH] Feature: Override custom IAE handler --- src/AclRepository.php | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/AclRepository.php b/src/AclRepository.php index 12ac33b..ed20cff 100644 --- a/src/AclRepository.php +++ b/src/AclRepository.php @@ -11,6 +11,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Permissions\Acl\Acl; +use Zend\Permissions\Acl\Exception\InvalidArgumentException; use Zend\Permissions\Acl\Role\GenericRole as Role; use Zend\Permissions\Acl\Resource\GenericResource as Resource; @@ -26,6 +27,9 @@ class AclRepository */ protected $role; + + protected $handler; + /** * AclRepository constructor. * @@ -80,6 +84,23 @@ public function __construct(array $role, array $config = []) } } } + + $this->handler = function (ServerRequestInterface $requestInterface) { + $route = $requestInterface->getAttribute('route'); + if (!empty($route)) { + foreach ($this->role as $role) { + if ($this->isAllowed($role, $route->getPattern())) { + return true; + } + } + } + return false; + }; + + } + + public function setCustomHandler(callable $handler) { + $this->handler = $handler; } /** @@ -149,20 +170,15 @@ public function __invoke(ServerRequestInterface $requestInterface, ResponseInter $route = '/' . ltrim($requestInterface->getUri()->getPath(), '/'); - foreach ($this->role as $role) { - if ($this->isAllowed($role, $route)) { - $allowed = true; - } - } - - //This is likely Slim 3 specific code... - $route = $requestInterface->getAttribute('route'); - if (!empty($route)) { + try { foreach ($this->role as $role) { - if ($this->isAllowed($role, $route->getPattern())) { + if ($this->isAllowed($role, $route)) { $allowed = true; } } + } catch (InvalidArgumentException $iae) { + $fn = $this->handler; + $allowed = $fn($requestInterface); } if ($allowed) {