Skip to content

Commit a146f3d

Browse files
committed
init
1 parent ac1ea4e commit a146f3d

File tree

14 files changed

+935
-1
lines changed

14 files changed

+935
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build
2+
vendor
3+
.idea
4+
.vscode
5+
.phpunit*
6+
composer.lock

README.md

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,105 @@
1-
# webman-permission
1+
# webman casbin plugin
2+
3+
[![Latest Stable Version](http://poser.pugx.org/tinywan/casbin/v)](https://packagist.org/packages/tinywan/casbin)
4+
[![Total Downloads](http://poser.pugx.org/tinywan/casbin/downloads)](https://packagist.org/packages/tinywan/casbin)
5+
[![License](http://poser.pugx.org/tinywan/casbin/license)](https://packagist.org/packages/tinywan/casbin)
6+
[![PHP Version Require](http://poser.pugx.org/tinywan/casbin/require/php)](https://packagist.org/packages/tinywan/casbin)
7+
[![webman-event](https://img.shields.io/github/last-commit/tinywan/casbin/main)]()
8+
[![webman-event](https://img.shields.io/github/v/tag/tinywan/casbin?color=ff69b4)]()
9+
210
An authorization library that supports access control models like ACL, RBAC, ABAC for webman plugin
11+
12+
## Requirements
13+
14+
- [ThinkORM](https://www.workerman.net/doc/webman/db/others.html)
15+
- [PHP-DI](https://github.com/PHP-DI/PHP-DI)
16+
17+
## Installation
18+
19+
```sh
20+
composer require tinywan/casbin
21+
```
22+
23+
## Configure
24+
25+
### 1、DI
26+
27+
configure `config/container.php`,Its final content is as follows:
28+
29+
```php
30+
$builder = new \DI\ContainerBuilder();
31+
$builder->addDefinitions(config('dependence', []));
32+
$builder->useAutowiring(true);
33+
return $builder->build();
34+
```
35+
36+
### 2、Database configuration
37+
38+
(1)修改数据库 `thinkorm` 配置
39+
40+
(2)创建 `casbin_rule` 数据表
41+
42+
```sql
43+
CREATE TABLE `casbin_rule` (
44+
`id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
45+
`ptype` VARCHAR ( 128 ) NOT NULL DEFAULT '',
46+
`v0` VARCHAR ( 128 ) NOT NULL DEFAULT '',
47+
`v1` VARCHAR ( 128 ) NOT NULL DEFAULT '',
48+
`v2` VARCHAR ( 128 ) NOT NULL DEFAULT '',
49+
`v3` VARCHAR ( 128 ) NOT NULL DEFAULT '',
50+
`v4` VARCHAR ( 128 ) NOT NULL DEFAULT '',
51+
`v5` VARCHAR ( 128 ) NOT NULL DEFAULT '',
52+
PRIMARY KEY ( `id` ) USING BTREE,
53+
KEY `idx_ptype` ( `ptype` ) USING BTREE,
54+
KEY `idx_v0` ( `v0` ) USING BTREE,
55+
KEY `idx_v1` ( `v1` ) USING BTREE,
56+
KEY `idx_v2` ( `v2` ) USING BTREE,
57+
KEY `idx_v3` ( `v3` ) USING BTREE,
58+
KEY `idx_v4` ( `v4` ) USING BTREE,
59+
KEY `idx_v5` ( `v5` ) USING BTREE
60+
) ENGINE = INNODB CHARSET = utf8mb4 COMMENT = '策略规则表';
61+
```
62+
(3)配置 `config/redis` 配置
63+
64+
## 重启webman
65+
66+
```
67+
php start.php restart
68+
```
69+
或者
70+
```
71+
php start.php restart -d
72+
```
73+
74+
## 用法
75+
76+
### 快速开始
77+
78+
安装成功后,可以这样使用:
79+
80+
```php
81+
use Tinywan\Casbin\Permission;
82+
83+
// adds permissions to a user
84+
Permission::addPermissionForUser('eve', 'articles', 'read');
85+
// adds a role for a user.
86+
Permission::addRoleForUser('eve', 'writer');
87+
// adds permissions to a rule
88+
Permission::addPolicy('writer', 'articles','edit');
89+
```
90+
91+
你可以检查一个用户是否拥有某个权限:
92+
93+
```php
94+
if (Permission::enforce("eve", "articles", "edit")) {
95+
echo '恭喜你!通过权限认证';
96+
} else {
97+
echo '对不起,您没有该资源访问权限';
98+
}
99+
```
100+
101+
更多 `API` 参考 [Casbin API](https://casbin.org/docs/en/management-api)
102+
103+
## 感谢
104+
105+
[Casbin](https://github.com/php-casbin/php-casbin),你可以查看全部文档在其 [官网](https://casbin.org/) 上。

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "tinywan/casbin",
3+
"description": "Webman Casbin Plugin",
4+
"type": "library",
5+
"license": "MIT",
6+
"require": {
7+
"php": ">=7.4",
8+
"casbin/casbin": "^3.20",
9+
"topthink/think-orm": "^2.0",
10+
"php-di/php-di": "^6.3",
11+
"workerman/redis": "^1.0"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"Tinywan\\Casbin\\": "src"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Tinywan\\Tests\\": "tests/"
21+
}
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "~7.0|~8.0|~9.0",
25+
"php-coveralls/php-coveralls": "^2.1",
26+
"workerman/webman": "^1.0"
27+
},
28+
"repositories": {
29+
"packagist": {
30+
"type": "composer",
31+
"url": "https://mirrors.aliyun.com/composer//"
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)