Skip to content

Commit

Permalink
Merge pull request #2 from xiaocaigua/master
Browse files Browse the repository at this point in the history
add readme
  • Loading branch information
kiss291323003 authored Jul 3, 2019
2 parents fe9847d + a38f58c commit c23903e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
composer.lock
composer.lock
.idea/
55 changes: 32 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# EasySwoole Policy
用于验证、解析Policy结构和语法
## 简介
Policy(即策略)是在特定模型或者资源中组织授权逻辑的类,用来处理用户授权动作。
## 安装
```bash
composer require EasySwoole/Policy
Expand All @@ -10,44 +10,53 @@ composer require EasySwoole/Policy
use EasySwoole\Policy\PolicyNode;
use EasySwoole\Policy\Policy;
$policy = new Policy();
//授权动作
//PolicyNode::EFFECT_ALLOW 允许
//PolicyNode::EFFECT_DENY 拒绝
//PolicyNode::EFFECT_UNKNOWN 未知
$policy = new Policy();
//添加节点授权
$policy->addPath('/user/add',PolicyNode::EFFECT_ALLOW);
$policy->addPath('/user/update',PolicyNode::EFFECT_ALLOW);
$policy->addPath('/user/delete',PolicyNode::EFFECT_DENY);
$policy->addPath('/user/*',PolicyNode::EFFECT_DENY);
var_dump($policy->check('user/asdasd'));
var_dump($policy->check('user/add'));
var_dump($policy->check('user/update'));
//验证节点权限
var_dump($policy->check('user/asdasd'));//deny
var_dump($policy->check('user/add')); //allow
var_dump($policy->check('user/update'));//allow
/*
* 允许/api/*,但是唯独拒绝/api/order/charge,/api/order/info,/api/sys/*
*/
$policy->addPath('/api/*',PolicyNode::EFFECT_ALLOW);
$policy->addPath('/api/order/charge',PolicyNode::EFFECT_DENY);
$policy->addPath('/api/order/info',PolicyNode::EFFECT_DENY);
$policy->addPath('/api/sys/*',PolicyNode::EFFECT_DENY);
var_dump($policy->check('/api/whatever'));
var_dump($policy->check('/api/order/charge'));
var_dump($policy->check('/api/order/info'));
var_dump($policy->check('/api/sys/whatever'));
///*
// * *表示通配,根节点
// */
//$root = new PolicyNode('*');
//
//$userChild = $root->addChild('user');
//$userChild->addChild('add');
//$userChild->addChild('update');
//$userChild->addChild('*');
//
//$apiChild = $root->addChild('charge');
////$apiChild->addChild('*');
////$userChild->addChild('*');
//
//
//var_dump($root->search('/user/update'));
//对象添加
$root = new PolicyNode('*');
$userChild = $root->addChild('user');
$userAddChild = $userChild->addChild('add');
$userAddChild->addChild('aaaaaa')->setAllow(PolicyNode::EFFECT_ALLOW);
$userChild->addChild('update')->setAllow(PolicyNode::EFFECT_DENY);
$userChild->addChild('*')->setAllow(PolicyNode::EFFECT_ALLOW);
$apiChild = $root->addChild('charge');
$apiChild->addChild('*');
$node = $root->search('/user/add/aaaa');
if ($node) {
var_dump($node->isAllow());
}
```
33 changes: 22 additions & 11 deletions src/Policy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,46 @@ class Policy

function __construct()
{
/*
* *表示通配,根节点
*/
/**
* 表示通配,根节点
*/
$this->root = new PolicyNode("*");
}

/*
*
/**
* 添加路劲并设置权限
* @param string $path
* @param string $allow
*/
public function addPath(string $path,string $allow = PolicyNode::EFFECT_ALLOW)
public function addPath(string $path, string $allow = PolicyNode::EFFECT_ALLOW)
{
$list = explode('/',trim($path,'/'));
$list = explode('/', trim($path, '/'));
$temp = $this->root;
foreach ($list as $path){
$temp = $temp->addChild($path);
foreach ($list as $path) {
$temp = $temp->addChild($path);//递归设置节点
}
$temp->setAllow($allow);
}

/**
* 检测权限
* @param string $path
* @return string
*/
public function check(string $path)
{
$node = $this->root->search($path);
if($node){
if ($node) {
return $node->isAllow();
}else{
} else {
return PolicyNode::EFFECT_UNKNOWN;
}
}

/**
* 所有节点
* @return array
*/
public function toArray()
{
return $this->root->toArray();
Expand Down
12 changes: 12 additions & 0 deletions src/PolicyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function addChild(string $nodeName):PolicyNode
}

/**
* 获取节点名称
* @return string
*/
public function getName(): string
Expand All @@ -55,6 +56,7 @@ public function getName(): string
}

/**
* 设置节点名称
* @param string $name
*/
public function setName(string $name): void
Expand All @@ -63,6 +65,7 @@ public function setName(string $name): void
}

/**
* 获取节点的权限
* @return array
*/
public function getLeaves(): array
Expand All @@ -71,18 +74,27 @@ public function getLeaves(): array
}

/**
* 设置节点权限
* @param array $leaves
*/
public function setLeaves(array $leaves): void
{
$this->leaves = $leaves;
}

/**
* 判断是否允许
* @return string
*/
public function isAllow()
{
return $this->allow;
}

/**
* 设置是否允许
* @param string $allow
*/
public function setAllow(string $allow): void
{
$this->allow = $allow;
Expand Down
45 changes: 45 additions & 0 deletions test/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Created by PhpStorm.
* User: mayn
* Date: 2019/7/3
* Time: 21:06
*/
require_once "vendor/autoload.php";

use EasySwoole\Policy\Policy;
use EasySwoole\Policy\PolicyNode;

//$policy = new Policy();
//
////添加节点权限
//$policy->addPath('/user/add', PolicyNode::EFFECT_ALLOW); //添加允许的单节点
//$policy->addPath('/user/update', PolicyNode::EFFECT_ALLOW);
//$policy->addPath('/user/delete', PolicyNode::EFFECT_DENY); //添加拒绝的单节点
//$policy->addPath('/user/*', PolicyNode::EFFECT_DENY); //添加拒绝的通配节点
//
////验证权限
//var_dump($policy->check('user/asdasd/dsad')); //deny
//var_dump($policy->check('user/add')); //allow
//var_dump($policy->check('user/update')); //allow
//var_dump($policy->check('user/delete')); //deny
//
//print_r($policy->toArray());//树形结构


//对象添加授权
$root = new PolicyNode('*');

$userChild = $root->addChild('user');
$userAddChild = $userChild->addChild('add');
$userAddChild->addChild('aaaaaa')->setAllow(PolicyNode::EFFECT_ALLOW);
$userChild->addChild('update')->setAllow(PolicyNode::EFFECT_DENY);
$userChild->addChild('*')->setAllow(PolicyNode::EFFECT_ALLOW);

$apiChild = $root->addChild('charge');
$apiChild->addChild('*');

$node = $root->search('/user/add/aaaa');
if ($node) {
var_dump($node->isAllow());
}

0 comments on commit c23903e

Please sign in to comment.