Skip to content

Commit

Permalink
Implement new logic
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Mar 1, 2024
1 parent 0fe1c06 commit f7769de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
33 changes: 17 additions & 16 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/ManagerLogicTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f7769de

Please sign in to comment.