From 3c980f591352e297e1e8e656a40330181574d79e Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 8 Oct 2024 20:33:54 +0500 Subject: [PATCH 1/2] Improve compatibility of item's type with Yii 2 --- src/Item.php | 8 ++++---- src/Permission.php | 2 +- src/Role.php | 2 +- src/SimpleItemsStorage.php | 4 ++-- tests/Common/ManagerLogicTestTrait.php | 5 +++-- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Item.php b/src/Item.php index 88d87b622..bbf22ac7c 100644 --- a/src/Item.php +++ b/src/Item.php @@ -10,8 +10,8 @@ */ abstract class Item { - public const TYPE_ROLE = 'role'; - public const TYPE_PERMISSION = 'permission'; + public const TYPE_ROLE = 1; + public const TYPE_PERMISSION = 2; /** * @var string The item description. @@ -41,9 +41,9 @@ final public function __construct(private string $name) } /** - * @return string Type of the item. + * @return int Type of the item. */ - abstract public function getType(): string; + abstract public function getType(): int; /** * @return string Authorization item name. diff --git a/src/Permission.php b/src/Permission.php index c3d7a4931..fdde75b2d 100644 --- a/src/Permission.php +++ b/src/Permission.php @@ -6,7 +6,7 @@ final class Permission extends Item { - public function getType(): string + public function getType(): int { return self::TYPE_PERMISSION; } diff --git a/src/Role.php b/src/Role.php index 2aef818a8..aef4c55a0 100644 --- a/src/Role.php +++ b/src/Role.php @@ -6,7 +6,7 @@ final class Role extends Item { - public function getType(): string + public function getType(): int { return self::TYPE_ROLE; } diff --git a/src/SimpleItemsStorage.php b/src/SimpleItemsStorage.php index 4646a7965..7e6c946d7 100644 --- a/src/SimpleItemsStorage.php +++ b/src/SimpleItemsStorage.php @@ -239,7 +239,7 @@ private function updateItemName(string $name, Item $item): void * * @psalm-return ($type is Item::TYPE_PERMISSION ? array : array) */ - private function getItemsByType(string $type): array + private function getItemsByType(int $type): array { return array_filter( $this->getAll(), @@ -250,7 +250,7 @@ private function getItemsByType(string $type): array /** * @psalm-param Item::TYPE_* $type */ - private function removeItemsByType(string $type): void + private function removeItemsByType(int $type): void { foreach ($this->getItemsByType($type) as $item) { $this->remove($item->getName()); diff --git a/tests/Common/ManagerLogicTestTrait.php b/tests/Common/ManagerLogicTestTrait.php index 0cf012a65..e1c416c50 100644 --- a/tests/Common/ManagerLogicTestTrait.php +++ b/tests/Common/ManagerLogicTestTrait.php @@ -14,6 +14,7 @@ use Yiisoft\Rbac\Exception\ItemAlreadyExistsException; use Yiisoft\Rbac\Exception\RuleInterfaceNotImplementedException; use Yiisoft\Rbac\Exception\RuleNotFoundException; +use Yiisoft\Rbac\Item; use Yiisoft\Rbac\Permission; use Yiisoft\Rbac\Role; use Yiisoft\Rbac\RuleInterface; @@ -727,7 +728,7 @@ public function testAddRole(): void 'name' => 'new role', 'description' => 'new role description', 'rule_name' => TrueRule::class, - 'type' => 'role', + 'type' => Item::TYPE_ROLE, 'updated_at' => 1_642_026_148, 'created_at' => 1_642_026_147, ], @@ -792,7 +793,7 @@ public function testAddPermission(): void 'name' => 'edit post', 'description' => 'edit a post', 'rule_name' => null, - 'type' => 'permission', + 'type' => Item::TYPE_PERMISSION, 'updated_at' => 1_642_026_148, 'created_at' => 1_642_026_147, ], From e8482bd1804f5defa6856ead025a61b1f3e952ca Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 8 Oct 2024 20:39:15 +0500 Subject: [PATCH 2/2] Fix psalm --- src/Item.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Item.php b/src/Item.php index bbf22ac7c..82e034ab6 100644 --- a/src/Item.php +++ b/src/Item.php @@ -42,6 +42,7 @@ final public function __construct(private string $name) /** * @return int Type of the item. + * @psalm-return Item::TYPE_* */ abstract public function getType(): int; @@ -139,7 +140,7 @@ final public function hasUpdatedAt(): bool * name: string, * description: string, * rule_name: string|null, - * type: string, + * type: Item::TYPE_*, * updated_at: int|null, * created_at: int|null, * }