From f7769def0d662120859782398161ee3018479757 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Fri, 1 Mar 2024 18:05:06 +0500 Subject: [PATCH] Implement new logic --- src/Manager.php | 33 +++++++++++++------------- tests/Common/ManagerLogicTestTrait.php | 2 +- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Manager.php b/src/Manager.php index 3631eb82..4a950add 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -79,28 +79,29 @@ public function userHasPermission( ? [$guestRole->getName()] : $this->assignmentsStorage->filterUserItemNames((string) $userId, $itemNames); - $userItems = []; - foreach ($userItemNames as $itemName) { - $userItems[$itemName] = $hierarchy[$itemName]['item']; - foreach ($hierarchy[$itemName]['children'] ?? [] as $item) { - $userItems[$item->getName()] = $item; + foreach ($hierarchy as $data) { + if ( + !in_array($data['item']->getName(), $userItemNames, strict: true) || + !$this->executeRule($userId === null ? $userId : (string) $userId, $data['item'], $parameters) + ) { + continue; } - } - if ( - empty($userItems) || - ($guestRole !== null && count($userItems) === 1 && array_key_exists('guest', $userItems)) - ) { - return false; - } + $hasPermission = true; + foreach ($data['children'] as $childItem) { + if (!$this->executeRule($userId === null ? $userId : (string) $userId, $childItem, $parameters)) { + $hasPermission = false; - foreach ($userItems as $item) { - if (!$this->executeRule($userId === null ? $userId : (string) $userId, $item, $parameters)) { - return false; + break; + } + } + + if ($hasPermission) { + return true; } } - return true; + return false; } public function canAddChild(string $parentName, string $childName): bool diff --git a/tests/Common/ManagerLogicTestTrait.php b/tests/Common/ManagerLogicTestTrait.php index e11c9c35..a14cfdb1 100644 --- a/tests/Common/ManagerLogicTestTrait.php +++ b/tests/Common/ManagerLogicTestTrait.php @@ -310,7 +310,7 @@ public function testUserHasPermissionTemp(): void ->assign(itemName: 'Role 1', userId: 'User') ->assign(itemName: 'Role 2', userId: 'User'); - $this->assertFalse($manager->userHasPermission('User', 'Permission')); + $this->assertTrue($manager->userHasPermission('User', 'Permission')); } public function testCanAddExistingChild(): void