Skip to content

Commit 486efb4

Browse files
committed
feat: can use easyswoole-permission
1 parent e47dcf8 commit 486efb4

File tree

7 files changed

+590
-4
lines changed

7 files changed

+590
-4
lines changed

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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "casbin/easyswoole-permissoin",
2+
"name": "casbin/easyswoole-permission",
33
"description": "An authorization library that supports access control models like ACL, RBAC, ABAC in EasySwoole.",
44
"license": "Apache-2.0",
55
"authors": [
@@ -11,7 +11,9 @@
1111
"require": {
1212
"php": "^7.2",
1313
"ext-swoole": ">=4.5",
14-
"casbin/casbin": "^2.2"
14+
"casbin/casbin": "^2.2",
15+
"easyswoole/orm": "^1.4",
16+
"easyswoole/easyswoole": "3.x"
1517
},
1618
"autoload": {
1719
"psr-4": {

src/Adapters/DatabaseAdapter.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EasySwoole\Permission\Adapters;
6+
7+
use Casbin\Persist\AdapterHelper;
8+
use Casbin\Model\Model;
9+
use Casbin\Persist\Adapter;
10+
use EasySwoole\Permission\Model\RulesModel;
11+
12+
class DatabaseAdapter implements Adapter
13+
{
14+
use AdapterHelper;
15+
16+
/**
17+
* Rules eloquent model
18+
*
19+
* @var Rule
20+
*/
21+
protected $eloquent;
22+
23+
/**
24+
* the DatabaseAdapter constructor.
25+
*
26+
* @param Rule $eloquent
27+
*/
28+
public function __construct(RulesModel $eloquent)
29+
{
30+
$this->eloquent = $eloquent;
31+
}
32+
33+
/**
34+
* savePolicyLine function.
35+
*
36+
* @param string $ptype
37+
* @param array $rule
38+
*/
39+
public function savePolicyLine(string $ptype, array $rule): void
40+
{
41+
$col['ptype'] = $ptype;
42+
foreach ($rule as $key => $value) {
43+
$col['v' . strval($key)] = $value;
44+
}
45+
46+
$this->eloquent->create()->data($col, false)->save();
47+
}
48+
49+
/**
50+
* loads all policy rules from the storage.
51+
*
52+
* @param Model $model
53+
*/
54+
public function loadPolicy(Model $model): void
55+
{
56+
$rows = $this->eloquent->create()->all();
57+
foreach ($rows as &$row) {
58+
$row = $row->hidden(['id', 'create_time', 'update_time'])->toArray(false, false);
59+
}
60+
unset($row);
61+
62+
foreach ($rows as $row) {
63+
$line = implode(', ', array_filter($row, function ($val) {
64+
return '' != $val && !is_null($val);
65+
}));
66+
$this->loadPolicyLine(trim($line), $model);
67+
}
68+
}
69+
70+
/**
71+
* saves all policy rules to the storage.
72+
*
73+
* @param Model $model
74+
*/
75+
public function savePolicy(Model $model): void
76+
{
77+
foreach ($model['p'] as $ptype => $ast) {
78+
foreach ($ast->policy as $rule) {
79+
$this->savePolicyLine($ptype, $rule);
80+
}
81+
}
82+
83+
foreach ($model['g'] as $ptype => $ast) {
84+
foreach ($ast->policy as $rule) {
85+
$this->savePolicyLine($ptype, $rule);
86+
}
87+
}
88+
}
89+
90+
/**
91+
* adds a policy rule to the storage.
92+
* This is part of the Auto-Save feature.
93+
*
94+
* @param string $sec
95+
* @param string $ptype
96+
* @param array $rule
97+
*/
98+
public function addPolicy(string $sec, string $ptype, array $rule): void
99+
{
100+
$this->savePolicyLine($ptype, $rule);
101+
}
102+
103+
/**
104+
* This is part of the Auto-Save feature.
105+
*
106+
* @param string $sec
107+
* @param string $ptype
108+
* @param array $rule
109+
*/
110+
public function removePolicy(string $sec, string $ptype, array $rule): void
111+
{
112+
$count = 0;
113+
114+
$instance = $this->eloquent->create()->get(['ptype' => $ptype]);
115+
116+
foreach ($rule as $key => $value) {
117+
$instance->where('v' . strval($key), $value);
118+
}
119+
120+
foreach ($instance->get() as $model) {
121+
if ($model->delete()) {
122+
++$count;
123+
}
124+
}
125+
}
126+
127+
/**
128+
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
129+
* This is part of the Auto-Save feature.
130+
*
131+
* @param string $sec
132+
* @param string $ptype
133+
* @param int $fieldIndex
134+
* @param string ...$fieldValues
135+
*/
136+
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
137+
{
138+
$count = 0;
139+
140+
$instance = $this->eloquent->create()->get(['ptype' => $ptype]);
141+
142+
foreach (range(0, 5) as $value) {
143+
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
144+
if ('' != $fieldValues[$value - $fieldIndex]) {
145+
$instance->where('v' . strval($value), $fieldValues[$value - $fieldIndex]);
146+
}
147+
}
148+
}
149+
150+
foreach ($instance->get() as $model) {
151+
if ($model->delete()) {
152+
++$count;
153+
}
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)