From 6ec29d64b3d0bcb5352e0f575f8ec26e92cdd209 Mon Sep 17 00:00:00 2001 From: huanglonghui <1592328848@qq.com> Date: Fri, 12 Apr 2024 21:23:41 +0800 Subject: [PATCH] fix: Adapt to PHP 8.1 and fix bug 1. Adapt to PHP 8.1 2. Fixed the issue that the same child could not be matched when it appeared multiple times --- .gitattributes | 3 +++ README.md | 4 ++-- composer.json | 4 ++-- src/PolicyNode.php | 11 +++++++---- test/Test.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..7740898 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +/.gitattributes export-ignore +/.gitignore export-ignore +/test export-ignore diff --git a/README.md b/README.md index 76f7ce4..e57f0b8 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Policy(即策略)是在特定模型或者资源中组织授权逻辑的类,用来处理用户授权动作。 ## 安装 ```bash -composer require EasySwoole/Policy +composer require easySwoole/policy ``` ## 使用方法 ``` @@ -59,4 +59,4 @@ if ($node) { var_dump($node->isAllow()); } -``` \ No newline at end of file +``` diff --git a/composer.json b/composer.json index d94cfce..6634b63 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,8 @@ } }, "require": { - "php": ">=7.1.0 || >=8.0.0", - "easyswoole/spl": "^1.1", + "php": ">=8.1.0", + "easyswoole/spl": "^2.0", "ext-json":">=1.0" } } diff --git a/src/PolicyNode.php b/src/PolicyNode.php index f3dac61..bd7eb28 100644 --- a/src/PolicyNode.php +++ b/src/PolicyNode.php @@ -100,9 +100,9 @@ public function setAllow(string $allow): void $this->allow = $allow; } - public function toArray(array $columns = null, $filter = null): array + public function toArray(callable|int|null $filter = null): array { - $list = parent::toArray($columns, $filter); + $list = parent::toArray($filter); foreach ($list['leaves'] as $key => $item){ $list['leaves'][$key] = $item->toArray(); } @@ -120,13 +120,16 @@ public function search(string $path,PolicyNode $parentNode = null):?PolicyNode $path = trim($path,'/'); $list = explode('/',$path); $name = array_shift($list); - if($name == $this->name){ + if($name == $this->name && empty($this->leaves)){ return $this; } if(empty($name) && $this->name == '*'){ return $this; } if(!empty($name) && !empty($parentNode)){ + if ($parentNode->leaves) { + return $parentNode->search($path); + } return $parentNode; } /* @@ -154,4 +157,4 @@ public function search(string $path,PolicyNode $parentNode = null):?PolicyNode } return null; } -} \ No newline at end of file +} diff --git a/test/Test.php b/test/Test.php index db25377..91af70a 100644 --- a/test/Test.php +++ b/test/Test.php @@ -43,3 +43,45 @@ if ($node) { var_dump($node->isAllow()); } + + +$root = new PolicyNode('*'); +$a1 = $root->addChild('A'); +$a2 = $a1->addChild('A'); +$a2->setAllow(PolicyNode::EFFECT_ALLOW); + +$a3 = $a2->addChild('A'); +$a4 = $a3->addChild('*'); +$a4->addChild('A')->setAllow(PolicyNode::EFFECT_ALLOW); +$a4->addChild('B')->setAllow(PolicyNode::EFFECT_DENY); +$node = $root->search('/A/A'); +if ($node) { + var_dump($node->isAllow()); +} + +$node = $root->search('/A/A/A/A/A'); +if ($node) { + var_dump($node->isAllow()); +} + +$node = $root->search('/A/A/A/A/B'); +if ($node) { + var_dump($node->isAllow()); +} + +$node = $root->search('/A/A/A/A/Z'); +if ($node) { + var_dump($node->isAllow()); +} + +$policy = new Policy(); +// 添加节点权限 +$policy->addPath('/Api/Admin/Merchant/*/*', PolicyNode::EFFECT_ALLOW); // 添加允许的单节点 +// 验证权限 +var_dump($policy->check('/Api/Admin/Merchant/Merchant/getMerchantList')); // allow + +$policy = new Policy(); +// 添加节点权限 +$policy->addPath('/Api/Admin/Merchant/Merchant/*', PolicyNode::EFFECT_DENY); // 添加允许的单节点 +// 验证权限 +var_dump($policy->check('/Api/Admin/Merchant/Merchant/getMerchantList')); // deny