This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
[ZF3] Rbac component cleaning #5231
Closed
Closed
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
46a754a
Update Rbac for ZF3
bakura10 f232a10
CS fix
bakura10 76a8b4b
Ensure role only have roleinterface instances and more tests
bakura10 aeb9f75
Not throw exception if cannot find role
bakura10 db3774f
Rewrite
bakura10 9523e9b
Store permission
bakura10 ddb7b91
Allow to add role by name
bakura10 131f8f1
no no, it must
bakura10 a6a8d7b
Fix tests and add new ones
bakura10 7382608
Remove parent
bakura10 d655040
Please everyone
bakura10 73648d2
Revert naming
bakura10 d2da093
Add tests
bakura10 703b6a0
Add optimization
bakura10 687566d
Fix
bakura10 726a18e
Remove features
bakura10 e238abc
update comment
bakura10 50b64e4
no need that now
bakura10 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -9,30 +9,39 @@ | |
|
||
namespace Zend\Permissions\Rbac; | ||
|
||
use RecursiveArrayIterator; | ||
use RecursiveIteratorIterator; | ||
|
||
class Rbac extends AbstractIterator | ||
class Rbac | ||
{ | ||
/** | ||
* flag: whether or not to create roles automatically if | ||
* they do not exist. | ||
* List of roles | ||
* | ||
* @var array|RoleInterface[] | ||
*/ | ||
protected $roles = array(); | ||
|
||
/** | ||
* flag: whether or not to create roles automatically if they do not exist. | ||
* | ||
* @var bool | ||
*/ | ||
protected $createMissingRoles = false; | ||
|
||
/** | ||
* @param bool $createMissingRoles | ||
* @return \Zend\Permissions\Rbac\Rbac | ||
* Set whether or not to create roles automatically if they do not exist | ||
* | ||
* @param bool $createMissingRoles | ||
* @return void | ||
*/ | ||
public function setCreateMissingRoles($createMissingRoles) | ||
{ | ||
$this->createMissingRoles = $createMissingRoles; | ||
|
||
return $this; | ||
$this->createMissingRoles = (bool) $createMissingRoles; | ||
} | ||
|
||
/** | ||
* Get whether or not to create roles automatically if they do not exist | ||
* | ||
* @return bool | ||
*/ | ||
public function getCreateMissingRoles() | ||
|
@@ -41,62 +50,62 @@ public function getCreateMissingRoles() | |
} | ||
|
||
/** | ||
* Add a child. | ||
* Add a child | ||
* | ||
* @param string|RoleInterface $child | ||
* @param array|RoleInterface|null $parents | ||
* @return self | ||
* @param string|RoleInterface $child | ||
* @param array|RoleInterface[]|null $parents | ||
* @return void | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function addRole($child, $parents = null) | ||
{ | ||
if (is_string($child)) { | ||
$child = new Role($child); | ||
} | ||
|
||
if (!$child instanceof RoleInterface) { | ||
throw new Exception\InvalidArgumentException( | ||
'Child must be a string or implement Zend\Permissions\Rbac\RoleInterface' | ||
); | ||
} | ||
|
||
if ($parents) { | ||
if (null !== $parents) { | ||
if (!is_array($parents)) { | ||
$parents = array($parents); | ||
} | ||
|
||
foreach ($parents as $parent) { | ||
if ($this->createMissingRoles && !$this->hasRole($parent)) { | ||
$this->addRole($parent); | ||
} | ||
|
||
$this->getRole($parent)->addChild($child); | ||
} | ||
} | ||
|
||
$this->children[] = $child; | ||
|
||
return $this; | ||
$this->roles[] = $child; | ||
} | ||
|
||
/** | ||
* Is a child with $name registered? | ||
* | ||
* @param \Zend\Permissions\Rbac\RoleInterface|string $objectOrName | ||
* @param RoleInterface|string $objectOrName | ||
* @return bool | ||
*/ | ||
public function hasRole($objectOrName) | ||
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. I prefer a more explicit interface: public function hasRole(RoleInterface $role);
public function hasRoleName($roleName); |
||
{ | ||
try { | ||
$this->getRole($objectOrName); | ||
|
||
return true; | ||
} catch (Exception\InvalidArgumentException $e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Get a child. | ||
* Get a child | ||
* | ||
* @param \Zend\Permissions\Rbac\RoleInterface|string $objectOrName | ||
* @param RoleInterface|string $objectOrName | ||
* @return RoleInterface | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
|
@@ -108,8 +117,10 @@ public function getRole($objectOrName) | |
); | ||
} | ||
|
||
$it = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::CHILD_FIRST); | ||
foreach ($it as $leaf) { | ||
$iterator = new RecursiveArrayIterator($this->roles); | ||
$iteratorIterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST); | ||
|
||
foreach ($iteratorIterator as $leaf) { | ||
if ((is_string($objectOrName) && $leaf->getName() == $objectOrName) || $leaf == $objectOrName) { | ||
return $leaf; | ||
} | ||
|
@@ -127,6 +138,7 @@ public function getRole($objectOrName) | |
* @param RoleInterface|string $role | ||
* @param string $permission | ||
* @param AssertionInterface|Callable|null $assert | ||
* @throws Exception\InvalidArgumentException | ||
* @return bool | ||
*/ | ||
public function isGranted($role, $permission, $assert = null) | ||
|
@@ -147,10 +159,6 @@ public function isGranted($role, $permission, $assert = null) | |
} | ||
} | ||
|
||
if ($this->getRole($role)->hasPermission($permission)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
return $this->getRole($role)->hasPermission($permission); | ||
} | ||
} |
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.
space after cast :D