From fb19c35f7d6801bcd606dd7a8fd3bb12ae9d2dd8 Mon Sep 17 00:00:00 2001 From: core23 Date: Mon, 24 Jun 2024 18:24:45 +0200 Subject: [PATCH] Extract methods --- src/Security/Handler/RoleSecurityHandler.php | 48 +++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/Security/Handler/RoleSecurityHandler.php b/src/Security/Handler/RoleSecurityHandler.php index 9640870966..c12e8c9377 100644 --- a/src/Security/Handler/RoleSecurityHandler.php +++ b/src/Security/Handler/RoleSecurityHandler.php @@ -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 { @@ -125,4 +116,39 @@ private function isAnyGranted(array $attributes, ?object $subject = null): bool return false; } + + /** + * @param array $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 $attributes + * @param AdminInterface $admin + * + * @return array + */ + 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; + } }