-
Notifications
You must be signed in to change notification settings - Fork 23
Conversation
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.
I think you need more tests, to cover the various different scenarios:
- Retrieving a role added as a child of another role.
- Retrieving a grandchild role, where the child role was added explicitly, but the grandchild added implicitly.
- How does
hasRole()
act in these scenarios?
See also the questions I provided on #48, as I think there are some fundamental problems with how roles are added; if you can figure out how to resolve those, let us know!
$child = new TestAsset\RoleTest('child'); | ||
$parent->addChild($child); | ||
$this->rbac->addRole($parent); | ||
$this->assertTrue($this->rbac->hasRole('child')); |
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.
This should be a separate test entirely.
|
||
// need to make sure the children are arrays (meaning they are added correctly) | ||
if (! is_array($children)) { | ||
return $rv ? true : false; |
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.
There is zero possibility of $rv
being anything but 0
in this scenario.
$rv++; | ||
} | ||
} | ||
$rv += $this->roleSearchIncludingChildren($role, $needle); |
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.
This section could be condensed dramatically:
if ((is_string($needle) && $roleName === $needle)
|| (is_object($needle) && $needle instanceof Role && $roleName === $needle->getName())
) {
return true; // no need to do anything else, because method returns based on non-zero $rv value
}
return $this->roleSearchIncludingChildren($role, $needle); // Same rationale here
} | ||
$rv += $this->roleSearchIncludingChildren($child, $needle); | ||
} | ||
} |
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.
Just like the other block, we could do the following:
if (! is_array($children)) {
return false;
}
if (! count($children)) {
return is_object($needle) && $needle instanceof Role && $obje->getName() === $needle->getName();
}
foreach ($children as $child) {
$roleName = $hcild->getName();
if (is_string($needle) && $roleName === $needle) {
return true;
}
if (is_object($needle) && $needle instanceof Role && $roleName === $needle->getName()) {
return true;
}
if ($this->roleSearchIncludingChildren($child, $needle)) {
return true;
}
}
return false;
Making these changes and the previous ones I suggested would eliminate unnecessary cycles when we've already determined a matching child is present.
foreach ($this->roles as $role) { | ||
if ($role->getName() == $needle) { | ||
return $role; | ||
} else { |
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.
Since your if
block always returns, there's no need for an else
; if the condition does not match, the next code must always execute. Promote the else
block:
if ($role->getName() === $needle) {
return $role;
}
$role = $this->getRoleSearchingChildren($role, $needle);
if ($role !== null) {
return $role;
}
{ | ||
if (($obj instanceof RoleInterface) && ($obj->getName() == $needle)) { | ||
return $obj; | ||
} else { |
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.
Same comment as for getRole()
- promote the body of your else
block, as you always return from the if
block.
foreach ($children as $child) { | ||
$result = $this->getRoleSearchingChildren($child, $needle); | ||
} | ||
return $result; |
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.
This part is confusing - do you want to return the first match, or the last? If the last, why?
This repository has been closed and moved to laminas/laminas-permissions-rbac; a new issue has been opened at laminas/laminas-permissions-rbac#1. |
This repository has been moved to laminas/laminas-permissions-rbac. If you feel that this patch is still relevant, please re-open against that repository, and reference this issue. To re-open, we suggest the following workflow:
|
Thanks for your suggestions and comments! All issues I think are now resolved.