This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
add permissions capacity to RouteGuard and ControllerGuard #243
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d580bff
add permissions capacity to RouteGuard
jmleroux 2819ed0
add permissions capacity to ControllerGuard
jmleroux 2b40a1b
more tests
jmleroux 68d4dce
$context parameter
jmleroux 74e84a7
don't use roles and permissions for the same route
jmleroux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
* @author Michaël Gallego <[email protected]> | ||
* @author Aeneas Rekkas | ||
* @author Daniel Gimenes <[email protected]> | ||
* @author JM Leroux <[email protected]> | ||
* @licence MIT | ||
*/ | ||
interface AssertionInterface | ||
|
@@ -36,8 +37,8 @@ interface AssertionInterface | |
* @TODO: for v3, update the interface to typehint to AuthorizationServiceInterface instead | ||
* | ||
* @param AuthorizationService $authorizationService | ||
* @param mixed $context | ||
* @param mixed|null $context | ||
* @return bool | ||
*/ | ||
public function assert(AuthorizationService $authorizationService); | ||
public function assert(AuthorizationService $authorizationService, $context = null); | ||
} |
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
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 |
---|---|---|
|
@@ -21,12 +21,14 @@ | |
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\MutableCreationOptionsInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use Zend\ServiceManager\ServiceManager; | ||
use ZfcRbac\Guard\RouteGuard; | ||
|
||
/** | ||
* Create a route guard | ||
* | ||
* @author Michaël Gallego <[email protected]> | ||
* @author JM Leroux <[email protected]> | ||
* @licence MIT | ||
*/ | ||
class RouteGuardFactory implements FactoryInterface, MutableCreationOptionsInterface | ||
|
@@ -50,6 +52,7 @@ public function setCreationOptions(array $options) | |
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
/** @var ServiceManager $parentLocator */ | ||
$parentLocator = $serviceLocator->getServiceLocator(); | ||
|
||
/* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ | ||
|
@@ -58,7 +61,10 @@ public function createService(ServiceLocatorInterface $serviceLocator) | |
/* @var \ZfcRbac\Service\RoleService $roleService */ | ||
$roleService = $parentLocator->get('ZfcRbac\Service\RoleService'); | ||
|
||
$routeGuard = new RouteGuard($roleService, $this->options); | ||
/* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ | ||
$authorizationService = $parentLocator->get('ZfcRbac\Service\AuthorizationService'); | ||
|
||
$routeGuard = new RouteGuard($roleService, $authorizationService, $this->options); | ||
$routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); | ||
|
||
return $routeGuard; | ||
|
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
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 |
---|---|---|
|
@@ -20,12 +20,14 @@ | |
|
||
use Zend\Mvc\MvcEvent; | ||
use ZfcRbac\Exception; | ||
use ZfcRbac\Service\AuthorizationServiceInterface; | ||
use ZfcRbac\Service\RoleService; | ||
|
||
/** | ||
* A route guard can protect a route or a hierarchy of routes (using simple wildcard pattern) | ||
* | ||
* @author Michaël Gallego <[email protected]> | ||
* @author JM Leroux <[email protected]> | ||
* @licence MIT | ||
*/ | ||
class RouteGuard extends AbstractGuard | ||
|
@@ -37,6 +39,11 @@ class RouteGuard extends AbstractGuard | |
*/ | ||
protected $roleService; | ||
|
||
/** | ||
* @var AuthorizationServiceInterface | ||
*/ | ||
protected $authorizationService; | ||
|
||
/** | ||
* Route guard rules | ||
* | ||
|
@@ -50,11 +57,16 @@ class RouteGuard extends AbstractGuard | |
* Constructor | ||
* | ||
* @param RoleService $roleService | ||
* @param array $rules | ||
* @param AuthorizationServiceInterface $authorizationService | ||
* @param array $rules | ||
*/ | ||
public function __construct(RoleService $roleService, array $rules = []) | ||
{ | ||
public function __construct( | ||
RoleService $roleService, | ||
AuthorizationServiceInterface $authorizationService, | ||
array $rules = [] | ||
) { | ||
$this->roleService = $roleService; | ||
$this->authorizationService = $authorizationService; | ||
$this->setRules($rules); | ||
} | ||
|
||
|
@@ -69,16 +81,51 @@ public function setRules(array $rules) | |
$this->rules = []; | ||
|
||
foreach ($rules as $key => $value) { | ||
if (is_int($key)) { | ||
$routeRegex = $value; | ||
$roles = []; | ||
$result = $this->parseOneRule($key, $value); | ||
|
||
$routePattern = $result['routePattern']; | ||
$this->rules[$routePattern]['roles'] = $result['roles']; | ||
$this->rules[$routePattern]['permissions'] = $result['permissions']; | ||
} | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param string|array $value | ||
* @throws \InvalidArgumentException | ||
* @return string[] | ||
*/ | ||
private function parseOneRule($key, $value) | ||
{ | ||
if (is_int($key)) { | ||
$routePattern = $value; | ||
$roles = []; | ||
$permissions = []; | ||
} else { | ||
$routePattern = $key; | ||
$roles = []; | ||
$permissions = []; | ||
if (isset($value['roles']) && isset($value['permissions'])) { | ||
throw new \InvalidArgumentException("You cannot use roles AND permissions for a route."); | ||
} | ||
if (!isset($value['roles']) && !isset($value['permissions'])) { | ||
$roles = (array)$value; | ||
$permissions = []; | ||
} else { | ||
$routeRegex = $key; | ||
$roles = (array) $value; | ||
if (isset($value['roles'])) { | ||
$roles = (array)$value['roles']; | ||
} | ||
if (isset($value['permissions'])) { | ||
$permissions = (array)$value['permissions']; | ||
} | ||
} | ||
|
||
$this->rules[$routeRegex] = $roles; | ||
} | ||
|
||
return [ | ||
'routePattern' => $routePattern, | ||
'roles' => $roles, | ||
'permissions' => $permissions, | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -87,24 +134,72 @@ public function setRules(array $rules) | |
public function isGranted(MvcEvent $event) | ||
{ | ||
$matchedRouteName = $event->getRouteMatch()->getMatchedRouteName(); | ||
$allowedRoles = null; | ||
|
||
foreach (array_keys($this->rules) as $routeRule) { | ||
if (fnmatch($routeRule, $matchedRouteName, FNM_CASEFOLD)) { | ||
$allowedRoles = $this->rules[$routeRule]; | ||
break; | ||
} | ||
// check roles first | ||
$allowedRoles = $this->getAllowedRoles($matchedRouteName); | ||
|
||
if (in_array('*', (array)$allowedRoles)) { | ||
return true; | ||
} | ||
|
||
if (!empty($allowedRoles)) { | ||
return $this->roleService->matchIdentityRoles($allowedRoles); | ||
} | ||
|
||
// if no roles in rule, check permissions | ||
$allowedPermissions = $this->getAllowedPermissions($matchedRouteName); | ||
|
||
// If no rules apply, it is considered as granted or not based on the protection policy | ||
if (null === $allowedRoles) { | ||
if (null === $allowedPermissions) { | ||
return $this->protectionPolicy === self::POLICY_ALLOW; | ||
} | ||
|
||
if (in_array('*', $allowedRoles)) { | ||
if (in_array('*', (array)$allowedPermissions)) { | ||
return true; | ||
} | ||
|
||
return $this->roleService->matchIdentityRoles($allowedRoles); | ||
foreach ($allowedPermissions as $permission) { | ||
if (!$this->authorizationService->isGranted($permission)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param string $matchedRouteName | ||
* @return array | ||
*/ | ||
private function getAllowedRoles($matchedRouteName) | ||
{ | ||
$allowedRoles = null; | ||
|
||
foreach (array_keys($this->rules) as $routeRule) { | ||
if (fnmatch($routeRule, $matchedRouteName, FNM_CASEFOLD)) { | ||
$allowedRoles = $this->rules[$routeRule]['roles']; | ||
break; | ||
} | ||
} | ||
|
||
return $allowedRoles; | ||
} | ||
|
||
/** | ||
* @param string $matchedRouteName | ||
* @return array | ||
*/ | ||
private function getAllowedPermissions($matchedRouteName) | ||
{ | ||
$allowedPermissions = null; | ||
|
||
foreach (array_keys($this->rules) as $routeRule) { | ||
if (fnmatch($routeRule, $matchedRouteName, FNM_CASEFOLD)) { | ||
$allowedPermissions = $this->rules[$routeRule]['permissions']; | ||
break; | ||
} | ||
} | ||
|
||
return $allowedPermissions; | ||
} | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not clear to me, that
'roles' => 'foo'
override (AND operation)'permission' => 'bar'
you would actually need a setting for that. Take this config for example:I think it is preferable to do something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like discussed in #238, it will be another PR.
I keep it simple first.