Skip to content

Commit

Permalink
Extract methods
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jul 5, 2024
1 parent 9ecd32d commit fb19c35
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/Security/Handler/RoleSecurityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,8 @@ public function isGranted(AdminInterface $admin, $attributes, ?object $object =
$attributes = [$attributes];
}

// NEXT_MAJOR: Change the foreach to a single check.
$useAll = false;
foreach ($attributes as $pos => $attribute) {
// If the attribute is not already a ROLE_ we generate the related role.
if (\is_string($attribute) && !str_starts_with($attribute, 'ROLE_')) {
$attributes[$pos] = sprintf($this->getBaseRole($admin), $attribute);
// All the admin related role are available when you have the `_ALL` role.
$useAll = true;
}
}

$useAll = $this->hasAllRole($attributes);
$attributes = $this->mapAttributes($attributes, $admin);
$allRole = sprintf($this->getBaseRole($admin), 'ALL');

try {
Expand Down Expand Up @@ -125,4 +116,39 @@ private function isAnyGranted(array $attributes, ?object $subject = null): bool

return false;
}

/**
* @param array<string|Expression> $attributes
*/
private function hasAllRole(mixed $attributes): bool
{
// NEXT_MAJOR: Change the foreach to a single check.
foreach ($attributes as $attribute) {
// If the attribute is not already a ROLE_ we generate the related role.
if (\is_string($attribute) && !str_starts_with($attribute, 'ROLE_')) {
return true;
}
}

return false;
}

/**
* @param array<string|Expression> $attributes
* @param AdminInterface<object> $admin
*
* @return array<string|Expression>
*/
private function mapAttributes(mixed $attributes, AdminInterface $admin): array
{
// NEXT_MAJOR: Change the foreach to a single check.
foreach ($attributes as $pos => $attribute) {
// If the attribute is not already a ROLE_ we generate the related role.
if (\is_string($attribute) && !str_starts_with($attribute, 'ROLE_')) {
$attributes[$pos] = sprintf($this->getBaseRole($admin), $attribute);
}
}

return $attributes;
}
}

0 comments on commit fb19c35

Please sign in to comment.