Skip to content

Commit 294c203

Browse files
authored
Merge pull request #1 from yk17310320725/master
Implement casbin to easyswoole extension but not implement phpunit
2 parents 89dd6ac + 486efb4 commit 294c203

File tree

11 files changed

+643
-2
lines changed

11 files changed

+643
-2
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = false
10+
11+
[*.{vue,js,scss}]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 2
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.idea
2+
*.DS_Store
3+
/vendor
4+
/coverage
5+
sftp-config.json
6+
composer.lock
7+
.subsplit
8+
.php_cs.cache
9+
/runtime

README.md

Lines changed: 191 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,191 @@
1-
# easyswoole-permission
2-
An authorization library that supports access control models like ACL, RBAC, ABAC in EasySwoole.
1+
<h1 align="center">easyswoole-permission</h1>
2+
<p align="center">An authorization library that supports access control models like ACL, RBAC, ABAC in EasySwoole.</p>
3+
4+
## Installing
5+
6+
Require this package in the `composer.json` of your easyswoole project. This will download the package.
7+
8+
```shell
9+
$ composer require
10+
```
11+
12+
## Usage
13+
14+
add mysql configuration to `dev.php`:
15+
```php
16+
/*################ MYSQL CONFIG ##################*/
17+
18+
'MYSQL' => [
19+
'host' => '127.0.0.1',
20+
'port' => 3306,
21+
'user' => 'root',
22+
'password' => 'root',
23+
'database' => 'easyswoole',
24+
'timeout' => 5,
25+
'charset' => 'utf8mb4',
26+
]
27+
```
28+
29+
add mysql configuration to `EasySwooleEvent.php`:
30+
31+
```php
32+
use EasySwoole\ORM\Db\Connection;
33+
use EasySwoole\ORM\DbManager;
34+
35+
public static function initialize()
36+
{
37+
...
38+
$config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
39+
DbManager::getInstance()->addConnection(new Connection($config));
40+
}
41+
```
42+
43+
Before using it, you need to create a table named `casbin_rules` for Casbin to store the policy.
44+
45+
Take mysql as an example:
46+
47+
```sql
48+
CREATE TABLE if not exists `casbin_rules` (
49+
`id` BigInt(20) unsigned NOT NULL AUTO_INCREMENT,
50+
`ptype` varchar(255) DEFAULT NULL,
51+
`v0` varchar(255) DEFAULT NULL,
52+
`v1` varchar(255) DEFAULT NULL,
53+
`v2` varchar(255) DEFAULT NULL,
54+
`v3` varchar(255) DEFAULT NULL,
55+
`v4` varchar(255) DEFAULT NULL,
56+
`v5` varchar(255) DEFAULT NULL,
57+
`create_time` timestamp NULL DEFAULT NULL,
58+
`update_time` timestamp NULL DEFAULT NULL,
59+
PRIMARY KEY (`id`)
60+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
61+
```
62+
63+
Then you can start like this:
64+
65+
```php
66+
use Easyswolle\Permission\Casbin;
67+
use Easyswolle\Permission\Config;
68+
69+
$config = new Config();
70+
$casbin = new Casbin($config);
71+
72+
// adds permissions to a user
73+
$casbin->addPermissionForUser('eve', 'articles', 'read');
74+
// adds a role for a user.
75+
$casbin->addRoleForUser('eve', 'writer');
76+
// adds permissions to a rule
77+
$casbin->addPolicy('writer', 'articles', 'edit');
78+
```
79+
80+
You can check if a user has a permission like this:
81+
82+
```php
83+
// to check if a user has permission
84+
if ($casbin->enforce('eve', 'articles', 'edit')) {
85+
// permit eve to edit articles
86+
} else {
87+
// deny the request, show an error
88+
}
89+
```
90+
91+
### Using Enforcer Api
92+
93+
It provides a very rich api to facilitate various operations on the Policy:
94+
95+
Gets all roles:
96+
97+
```php
98+
Enforcer::getAllRoles(); // ['writer', 'reader']
99+
```
100+
101+
Gets all the authorization rules in the policy.:
102+
103+
```php
104+
Enforcer::getPolicy();
105+
```
106+
107+
Gets the roles that a user has.
108+
109+
```php
110+
Enforcer::getRolesForUser('eve'); // ['writer']
111+
```
112+
113+
Gets the users that has a role.
114+
115+
```php
116+
Enforcer::getUsersForRole('writer'); // ['eve']
117+
```
118+
119+
Determines whether a user has a role.
120+
121+
```php
122+
Enforcer::hasRoleForUser('eve', 'writer'); // true or false
123+
```
124+
125+
Adds a role for a user.
126+
127+
```php
128+
Enforcer::addRoleForUser('eve', 'writer');
129+
```
130+
131+
Adds a permission for a user or role.
132+
133+
```php
134+
// to user
135+
Enforcer::addPermissionForUser('eve', 'articles', 'read');
136+
// to role
137+
Enforcer::addPermissionForUser('writer', 'articles','edit');
138+
```
139+
140+
Deletes a role for a user.
141+
142+
```php
143+
Enforcer::deleteRoleForUser('eve', 'writer');
144+
```
145+
146+
Deletes all roles for a user.
147+
148+
```php
149+
Enforcer::deleteRolesForUser('eve');
150+
```
151+
152+
Deletes a role.
153+
154+
```php
155+
Enforcer::deleteRole('writer');
156+
```
157+
158+
Deletes a permission.
159+
160+
```php
161+
Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).
162+
```
163+
164+
Deletes a permission for a user or role.
165+
166+
```php
167+
Enforcer::deletePermissionForUser('eve', 'articles', 'read');
168+
```
169+
170+
Deletes permissions for a user or role.
171+
172+
```php
173+
// to user
174+
Enforcer::deletePermissionsForUser('eve');
175+
// to role
176+
Enforcer::deletePermissionsForUser('writer');
177+
```
178+
179+
Gets permissions for a user or role.
180+
181+
```php
182+
Enforcer::getPermissionsForUser('eve'); // return array
183+
```
184+
185+
Determines whether a user has a permission.
186+
187+
```php
188+
Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false
189+
```
190+
191+
See [Casbin API](https://casbin.org/docs/en/management-api) for more APIs.

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "casbin/easyswoole-permission",
3+
"description": "An authorization library that supports access control models like ACL, RBAC, ABAC in EasySwoole.",
4+
"license": "Apache-2.0",
5+
"authors": [
6+
{
7+
"name": "yikang",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"php": "^7.2",
13+
"ext-swoole": ">=4.5",
14+
"casbin/casbin": "^2.2",
15+
"easyswoole/orm": "^1.4",
16+
"easyswoole/easyswoole": "3.x"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"EasySwoole\\Permission\\": "src/"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"EasySwoole\\Permission\\Tests\\": "tests/"
26+
}
27+
}
28+
}

src/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)