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
Conditions for Guards #268
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1eae206
Add condition option to permission route guard
davidwindell 1c55f88
Fix if statement, add more tests
davidwindell be64fe5
Use strict
davidwindell 29b754f
Fix CS errors, make code harder to read!
davidwindell c4e629d
Add doc
davidwindell 7382d6c
Correct typo
davidwindell 41ce33f
Implement changes suggested by @danizord
davidwindell f56b0b8
CS
davidwindell 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
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 |
---|---|---|
|
@@ -18,7 +18,6 @@ | |
namespace ZfcRbac\Guard; | ||
|
||
use Zend\Mvc\MvcEvent; | ||
use ZfcRbac\Exception; | ||
use ZfcRbac\Service\AuthorizationServiceInterface; | ||
|
||
/** | ||
|
@@ -101,12 +100,30 @@ public function isGranted(MvcEvent $event) | |
return true; | ||
} | ||
|
||
foreach ($allowedPermissions as $permission) { | ||
if (!$this->authorizationService->isGranted($permission)) { | ||
$permissions = isset($allowedPermissions['permissions']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably happen before the wildcard check |
||
? $allowedPermissions['permissions'] | ||
: $allowedPermissions; | ||
|
||
$condition = isset($allowedPermissions['condition']) | ||
? $allowedPermissions['condition'] | ||
: GuardInterface::CONDITION_AND; | ||
|
||
foreach ($permissions as $permission) { | ||
if ($condition === GuardInterface::CONDITION_OR | ||
&& $this->authorizationService->isGranted($permission) | ||
) { | ||
return true; | ||
} elseif ($condition === GuardInterface::CONDITION_AND | ||
&& !$this->authorizationService->isGranted($permission) | ||
) { | ||
return false; | ||
} | ||
} | ||
|
||
if ($condition === GuardInterface::CONDITION_OR) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm I'm trying to read the code and I'm not sure to understand this. |
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -326,6 +326,26 @@ public function routeDataProvider() | |
'isGranted' => true, | ||
'policy' => GuardInterface::POLICY_DENY | ||
], | ||
[ | ||
'rules' => ['route' => [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to align when the value is array |
||
'permissions' => ['post.edit', 'post.read'], | ||
'condition' => GuardInterface::CONDITION_OR | ||
]], | ||
'matchedRouteName' => 'route', | ||
'identityPermissions' => [['post.edit', null, true]], | ||
'isGranted' => true, | ||
'policy' => GuardInterface::POLICY_DENY | ||
], | ||
[ | ||
'rules' => ['route' => [ | ||
'permissions' => ['post.edit', 'post.read'], | ||
'condition' => GuardInterface::CONDITION_AND | ||
]], | ||
'matchedRouteName' => 'route', | ||
'identityPermissions' => [['post.edit', null, true]], | ||
'isGranted' => false, | ||
'policy' => GuardInterface::POLICY_DENY | ||
] | ||
]; | ||
} | ||
|
||
|
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.
Doc is wrong here :p.