Skip to content

Commit

Permalink
making requested changes for pull request #44
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Brown <[email protected]>
  • Loading branch information
JacobBrownAustin committed Feb 1, 2023
1 parent efff891 commit 2224633
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
/phpcs.xml export-ignore
/phpunit.xml.dist export-ignore
/test/ export-ignore
/benchmarks/ export-ignore
/phpbench.json export-ignore
5 changes: 4 additions & 1 deletion benchmarks/AclBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function __construct()
$this->acl = new Acl();
}

public function benchSetService(): void
/**
* Benchmarks setting up ACL with roles and resources, and then adding allow/deny rules to them
*/
public function benchAclRolesResourcesAllowDeny(): void
{
$acl = new Acl();
$role = new GenericRole('A');
Expand Down
42 changes: 28 additions & 14 deletions src/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class Acl implements AclInterface
* Resources by resourceId plus a null element
* Used to speed up setRule()
*
* @var (ResourceInterface|null)[]
* @var array<int|string, ResourceInterface|null>
*/
protected $resourcesById = [null];
private $resourcesById = [null];

/** @var Role\RoleInterface|null */
protected $isAllowedRole;
Expand Down Expand Up @@ -568,10 +568,10 @@ public function setRule(
}
}
unset($rolesTemp);
/** var (ResourceInterface|null)[] */
$resourcesArray = [];
/** var array<int|string, ResourceInterface|null> */
$resourcesToApplyRules = [];
if (null === $resources && $this->resources) {
$resourcesArray = $this->resourcesById;
$resourcesToApplyRules = $this->resourcesById;
} else {
// ensure that all specified Resources exist; normalize input to array of Resource objects or null
if (! is_array($resources)) {
Expand All @@ -583,10 +583,10 @@ public function setRule(
if (null !== $resource) {
$resourceObj = $this->getResource($resource);
$resourceId = $resourceObj->getResourceId();
$this->getChildResources($resourceObj, $resourcesArray);
$resourcesArray[$resourceId] = $resourceObj;
$this->getChildResourcesIntoReference($resourceObj, $resourcesToApplyRules);
$resourcesToApplyRules[$resourceId] = $resourceObj;
} else {
$resourcesArray[] = null;
$resourcesToApplyRules[] = null;
}
}
unset($resources);
Expand All @@ -602,7 +602,7 @@ public function setRule(
switch ($operation) {
// add to the rules
case self::OP_ADD:
foreach ($resourcesArray as $resource) {
foreach ($resourcesToApplyRules as $resource) {
foreach ($roles as $role) {
$rules =& $this->getRules($resource, $role, true);
if (! $privileges) {
Expand All @@ -623,7 +623,7 @@ public function setRule(

// remove from the rules
case self::OP_REMOVE:
foreach ($resourcesArray as $resource) {
foreach ($resourcesToApplyRules as $resource) {
foreach ($roles as $role) {
$rules =& $this->getRules($resource, $role);
if (null === $rules) {
Expand Down Expand Up @@ -674,20 +674,34 @@ public function setRule(
/**
* Returns all child resources from the given resource.
*
* @param (ResourceInterface|null)[] $return
* @return (ResourceInterface|null)[]
* @param array<int|string, ResourceInterface|null> $return
* @return array<int|string, ResourceInterface|null>
*/
protected function getChildResources(ResourceInterface $resource, array &$return = [])
private function getChildResourcesIntoReference(ResourceInterface $resource, array &$return = [])
{
$id = $resource->getResourceId();
$children = $this->resources[$id]['children'];
foreach ($children as $child) {
$this->getChildResources($child, $return);
$this->getChildResourcesIntoReference($child, $return);
$return[$child->getResourceId()] = $child;
}
return $return;
}

/**
* Returns all child resources from the given resource.
*
* @deprecated
*
* @see getChildResourcesIntoReference()
*
* @return array<int|string, ResourceInterface|null>
*/
protected function getChildResources(ResourceInterface $resource)
{
return $this->getChildResourcesIntoReference($resource);
}

/**
* Returns true if and only if the Role has access to the Resource
*
Expand Down

0 comments on commit 2224633

Please sign in to comment.