Skip to content

Commit

Permalink
Merge pull request #7 from XueSiLf/master
Browse files Browse the repository at this point in the history
fix: Adapt to PHP 8.1 and fix bug
  • Loading branch information
kiss291323003 authored Apr 16, 2024
2 parents d8116ff + 6ec29d6 commit 5eae9d4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/test export-ignore
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Policy(即策略)是在特定模型或者资源中组织授权逻辑的类,用来处理用户授权动作。
## 安装
```bash
composer require EasySwoole/Policy
composer require easySwoole/policy
```
## 使用方法
```
Expand Down Expand Up @@ -59,4 +59,4 @@ if ($node) {
var_dump($node->isAllow());
}
```
```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
11 changes: 7 additions & 4 deletions src/PolicyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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;
}
/*
Expand Down Expand Up @@ -154,4 +157,4 @@ public function search(string $path,PolicyNode $parentNode = null):?PolicyNode
}
return null;
}
}
}
42 changes: 42 additions & 0 deletions test/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5eae9d4

Please sign in to comment.