Skip to content

Commit 232d1d0

Browse files
authored
Merge pull request #1 from yk17310320725/master
Implement casbin to hyper extension but not implement phpunit
2 parents caa1d15 + d22f197 commit 232d1d0

File tree

14 files changed

+665
-2
lines changed

14 files changed

+665
-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

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
.scrutinizer.yml export-ignore
7+
.travis.yml export-ignore
8+
phpunit.php export-ignore
9+
phpunit.xml.dist export-ignore
10+
phpunit.xml export-ignore
11+
.php_cs export-ignore

.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: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,174 @@
1-
# hyperf-permission
2-
An authorization library that supports access control models like ACL, RBAC, ABAC in Hyperf.
1+
<h1 align="center"> hyperf-permission </h1>
2+
3+
<p align="center"> An authorization library that supports access control models like ACL, RBAC, ABAC in Hyperf..</p>
4+
5+
6+
## Installing
7+
8+
Require this package in the `composer.json` of your Hyperf project. This will download the package.
9+
10+
```shell
11+
$ composer require yi17310320725/hyperf-permission:dev-master -vvv
12+
```
13+
14+
To publish the config, run the vendor publish command:
15+
16+
```shell
17+
$ php bin/hyperf.php vendor:publish yi17310320725/hyperf-permission
18+
```
19+
20+
This will create a new model config file named `config/autoload/casbin-rbac-model.conf`, a new permission config file named `config/autoload/permission.php` and new migrate file named `2020_07_22_213202_create_rules_table.php`.
21+
22+
To migrate the migrations, run the migrate command:
23+
24+
```shell
25+
$ php bin/hyperf.php migrate
26+
```
27+
28+
This will create a new table named `rules` .
29+
30+
## Usage
31+
32+
### Quick start
33+
34+
Once installed you can do stuff like this:
35+
36+
```php
37+
use Hyperf\Permission\Casbin;
38+
39+
$casbin = new Casbin();
40+
41+
// adds permissions to a user
42+
$casbin->addPermissionForUser('eve', 'articles', 'read');
43+
// adds a role for a user.
44+
$casbin->addRoleForUser('eve', 'writer');
45+
// adds permissions to a rule
46+
$casbin->addPolicy('writer', 'articles', 'edit');
47+
```
48+
49+
You can check if a user has a permission like this:
50+
51+
```php
52+
// to check if a user has permission
53+
if ($casbin->enforce('eve', 'articles', 'edit')) {
54+
// permit eve to edit articles
55+
} else {
56+
// deny the request, show an error
57+
}
58+
```
59+
60+
### Using Enforcer Api
61+
62+
It provides a very rich api to facilitate various operations on the Policy:
63+
64+
Gets all roles:
65+
66+
```php
67+
Enforcer::getAllRoles(); // ['writer', 'reader']
68+
```
69+
70+
Gets all the authorization rules in the policy.:
71+
72+
```php
73+
Enforcer::getPolicy();
74+
```
75+
76+
Gets the roles that a user has.
77+
78+
```php
79+
Enforcer::getRolesForUser('eve'); // ['writer']
80+
```
81+
82+
Gets the users that has a role.
83+
84+
```php
85+
Enforcer::getUsersForRole('writer'); // ['eve']
86+
```
87+
88+
Determines whether a user has a role.
89+
90+
```php
91+
Enforcer::hasRoleForUser('eve', 'writer'); // true or false
92+
```
93+
94+
Adds a role for a user.
95+
96+
```php
97+
Enforcer::addRoleForUser('eve', 'writer');
98+
```
99+
100+
Adds a permission for a user or role.
101+
102+
```php
103+
// to user
104+
Enforcer::addPermissionForUser('eve', 'articles', 'read');
105+
// to role
106+
Enforcer::addPermissionForUser('writer', 'articles','edit');
107+
```
108+
109+
Deletes a role for a user.
110+
111+
```php
112+
Enforcer::deleteRoleForUser('eve', 'writer');
113+
```
114+
115+
Deletes all roles for a user.
116+
117+
```php
118+
Enforcer::deleteRolesForUser('eve');
119+
```
120+
121+
Deletes a role.
122+
123+
```php
124+
Enforcer::deleteRole('writer');
125+
```
126+
127+
Deletes a permission.
128+
129+
```php
130+
Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).
131+
```
132+
133+
Deletes a permission for a user or role.
134+
135+
```php
136+
Enforcer::deletePermissionForUser('eve', 'articles', 'read');
137+
```
138+
139+
Deletes permissions for a user or role.
140+
141+
```php
142+
// to user
143+
Enforcer::deletePermissionsForUser('eve');
144+
// to role
145+
Enforcer::deletePermissionsForUser('writer');
146+
```
147+
148+
Gets permissions for a user or role.
149+
150+
```php
151+
Enforcer::getPermissionsForUser('eve'); // return array
152+
```
153+
154+
Determines whether a user has a permission.
155+
156+
```php
157+
Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false
158+
```
159+
160+
See [Casbin API](https://casbin.org/docs/en/management-api) for more APIs.
161+
162+
## Contributing
163+
164+
You can contribute in one of three ways:
165+
166+
1. File bug reports using the [issue tracker](https://github.com/yi17310320725/hyperf-authz/issues).
167+
2. Answer questions or fix bugs on the [issue tracker](https://github.com/yi17310320725/hyperf-authz/issues).
168+
3. Contribute new features or update the wiki.
169+
170+
_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._
171+
172+
## License
173+
174+
Apache-2.0

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "casbin/hyperf-permission",
3+
"description": "An authorization library that supports access control models like ACL, RBAC, ABAC in Hyperf.",
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+
"hyperf/db-connection": "^2.0",
15+
"hyperf/config": "~2.0.0",
16+
"casbin/casbin": "^2.2"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Hyperf\\Permission\\": "src/"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Hyperf\\Permission\\Tests\\": "tests/"
26+
}
27+
},
28+
"extra": {
29+
"hyperf": {
30+
"config": "Hyperf\\Permission\\ConfigProvider"
31+
}
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Hyperf\Database\Schema\Schema;
4+
use Hyperf\Database\Schema\Blueprint;
5+
use Hyperf\Database\Migrations\Migration;
6+
7+
class CreateRulesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create(config('permission.database.rules_table'), function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->string('ptype')->nullable();
19+
$table->string('v0')->nullable();
20+
$table->string('v1')->nullable();
21+
$table->string('v2')->nullable();
22+
$table->string('v3')->nullable();
23+
$table->string('v4')->nullable();
24+
$table->string('v5')->nullable();
25+
$table->timestamps();
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
Schema::dropIfExists(config('permission.database.rules_table'));
37+
}
38+
}

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">src/</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

publish/casbin-rbac-model.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[role_definition]
8+
g = _, _
9+
10+
[policy_effect]
11+
e = some(where (p.eft == allow))
12+
13+
[matchers]
14+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

publish/permission.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* Casbin model setting.
6+
*/
7+
'model' => [
8+
// Available Settings: "file", "text"
9+
'config_type' => 'file',
10+
11+
'config_file_path' => __DIR__ . '/casbin-rbac-model.conf',
12+
13+
'config_text' => '',
14+
],
15+
16+
/*
17+
* Casbin adapter .
18+
*/
19+
'adapter' => Hyperf\Permission\Adapters\DatabaseAdapter::class,
20+
21+
/*
22+
* Database setting.
23+
*/
24+
'database' => [
25+
// Database connection for following tables.
26+
'connection' => '',
27+
28+
// Rule table name.
29+
'rules_table' => 'rules',
30+
],
31+
32+
'log' => [
33+
// changes whether Lauthz will log messages to the Logger.
34+
'enabled' => false,
35+
],
36+
];

0 commit comments

Comments
 (0)