Skip to content

Commit

Permalink
Feature: Override custom IAE handler
Browse files Browse the repository at this point in the history
  • Loading branch information
geggleto committed Jan 8, 2016
1 parent 74973db commit 0f36da7
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/AclRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,6 +27,9 @@ class AclRepository
*/
protected $role;


protected $handler;

/**
* AclRepository constructor.
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0f36da7

Please sign in to comment.