diff --git a/composer.json b/composer.json index e8866dd..23c6e91 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "require": { "php": "^8.1", "yiisoft/rbac": "dev-master", - "yiisoft/var-dumper": "^1.0" + "yiisoft/var-dumper": "^1.7" }, "require-dev": { "ext-uopz": "*", diff --git a/src/AssignmentsStorage.php b/src/AssignmentsStorage.php index ffed31e..0da3044 100644 --- a/src/AssignmentsStorage.php +++ b/src/AssignmentsStorage.php @@ -12,6 +12,8 @@ * * It is suitable for authorization data that is not too big (for example, the authorization data for a personal blog * system). + * + * @psalm-import-type RawAssignment from SimpleAssignmentsStorage */ final class AssignmentsStorage extends SimpleAssignmentsStorage { @@ -86,25 +88,28 @@ public function clear(): void private function loadAssignments(): void { - /** @psalm-var array $assignments */ + /** @psalm-var list $assignments */ $assignments = $this->loadFromFile($this->assignmentFile); $modifiedTime = @filemtime($this->assignmentFile); - foreach ($assignments as $userId => $roles) { - foreach ($roles as $role) { - /** @psalm-suppress InvalidPropertyAssignmentValue */ - $this->assignments[$userId][$role] = new Assignment((string)$userId, $role, $modifiedTime); - } + foreach ($assignments as $assignment) { + /** @psalm-suppress InvalidPropertyAssignmentValue */ + $this->assignments[$assignment['user_id']][$assignment['item_name']] = new Assignment( + userId: $assignment['user_id'], + itemName: $assignment['item_name'], + createdAt: $assignment['created_at'] ?? $modifiedTime + ); } } private function saveAssignments(): void { $assignmentData = []; - foreach ($this->assignments as $userId => $assignments) { - foreach ($assignments as $assignment) { - $assignmentData[$userId][] = $assignment->getItemName(); + foreach ($this->assignments as $userAssignments) { + foreach ($userAssignments as $userAssignment) { + $assignmentData[] = $userAssignment->getAttributes(); } } + $this->saveToFile($assignmentData, $this->assignmentFile); } } diff --git a/src/ItemsStorage.php b/src/ItemsStorage.php index e4b1822..e5efa24 100644 --- a/src/ItemsStorage.php +++ b/src/ItemsStorage.php @@ -94,13 +94,16 @@ private function save(): void { $items = []; foreach ($this->getAll() as $name => $item) { - $items[$name] = array_filter($item->getAttributes()); + $data = array_filter($item->getAttributes()); if ($this->hasChildren($name)) { foreach ($this->getDirectChildren($name) as $child) { - $items[$name]['children'][] = $child->getName(); + $data['children'][] = $child->getName(); } } + + $items[] = $data; } + $this->saveToFile($items, $this->itemFile); } @@ -116,19 +119,17 @@ private function loadItems(): void /** @psalm-var array $items */ $items = $this->loadFromFile($this->itemFile); $itemsMtime = @filemtime($this->itemFile); - foreach ($items as $name => $item) { - $this->items[$name] = $this + foreach ($items as $item) { + $this->items[$item['name']] = $this ->getInstanceFromAttributes($item) - ->withCreatedAt($itemsMtime) - ->withUpdatedAt($itemsMtime); + ->withCreatedAt($item['created_at'] ?? $itemsMtime) + ->withUpdatedAt($item['updated_at'] ?? $itemsMtime); } - foreach ($items as $name => $item) { - if (isset($item['children'])) { - foreach ($item['children'] as $childName) { - if ($this->hasItem($childName)) { - $this->children[$name][$childName] = $this->items[$childName]; - } + foreach ($items as $item) { + foreach ($item['children'] ?? [] as $childName) { + if ($this->hasItem($childName)) { + $this->children[$item['name']][$childName] = $this->items[$childName]; } } } @@ -161,7 +162,7 @@ private function getInstanceFromAttributes(array $attributes): Permission|Role $item = $item->withDescription($description); } - $ruleName = $attributes['ruleName'] ?? null; + $ruleName = $attributes['rule_name'] ?? null; if ($ruleName !== null) { $item = $item->withRuleName($ruleName); } diff --git a/tests/AssignmentsStorageTest.php b/tests/AssignmentsStorageTest.php index d79e93e..356710f 100644 --- a/tests/AssignmentsStorageTest.php +++ b/tests/AssignmentsStorageTest.php @@ -37,4 +37,9 @@ protected function createAssignmentsStorage(): AssignmentsStorageInterface { return new AssignmentsStorage($this->getDataPath()); } + + protected function getAssignmentsStorageForModificationAssertions(): AssignmentsStorageInterface + { + return $this->createAssignmentsStorage(); + } } diff --git a/tests/ItemsStorageTest.php b/tests/ItemsStorageTest.php index bc90565..2e71cc2 100644 --- a/tests/ItemsStorageTest.php +++ b/tests/ItemsStorageTest.php @@ -66,6 +66,11 @@ protected function createItemsStorage(): ItemsStorageInterface return new ItemsStorage($this->getDataPath()); } + protected function getItemsStorageForModificationAssertions(): ItemsStorageInterface + { + return $this->createItemsStorage(); + } + private function getTempDirectory(): string { return __DIR__ . '/temp';