Skip to content

Commit 16bd74c

Browse files
committed
Support tp6.0 stable version
1 parent ba6c1ca commit 16bd74c

File tree

4 files changed

+85
-43
lines changed

4 files changed

+85
-43
lines changed

README.md

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
</h1>
44

55
<p align="center">
6-
<strong>Think-authz 是一个专为ThinkPHP6.0打造的授权(角色和权限控制)工具</strong>
6+
<strong>Think-authz 是一个专为 ThinkPHP6.0 打造的授权(角色和权限控制)工具</strong>
77
</p>
88

99
<p align="center">
@@ -32,11 +32,7 @@
3232
* [用法](#用法)
3333
* [快速开始](#快速开始)
3434
* [使用 Enforcer Api](#使用-enforcer-api)
35-
* [Using a middleware](#using-a-middleware)
36-
* [basic Enforcer Middleware](#basic-enforcer-middleware)
37-
* [HTTP Request Middleware ( RESTful is also supported )](#http-request-middleware--restful-is-also-supported-)
38-
* [Using commands](#using-commands)
39-
* [Cache](#using-cache)
35+
* [使用中间件](#使用中间件)
4036
* [感谢](#thinks)
4137
* [License](#license)
4238

@@ -67,7 +63,7 @@ php think tauthz:publish
6763
这将自动生成 `config/tauthz-rbac-model.conf``config/tauthz.php` 文件。
6864

6965

70-
执行迁移工具(确保数据库配置信息正确):
66+
执行迁移工具(**确保数据库配置信息正确**):
7167

7268
```
7369
php think migrate:run
@@ -95,7 +91,7 @@ Enforcer::addPolicy('writer', 'articles','edit');
9591

9692
```
9793

98-
You can check if a user has a permission like this:
94+
你可以检查一个用户是否拥有某个权限:
9995

10096
```php
10197
// to check if a user has permission
@@ -109,45 +105,45 @@ if (Enforcer::enforce("eve", "articles", "edit")) {
109105

110106
### 使用 Enforcer Api
111107

112-
It provides a very rich api to facilitate various operations on the Policy:
108+
它提供了非常丰富的 `API`,以促进对 `Policy` 的各种操作:
113109

114-
Gets all roles:
110+
获取所有角色:
115111

116112
```php
117113
Enforcer::getAllRoles(); // ['writer', 'reader']
118114
```
119115

120-
Gets all the authorization rules in the policy.:
116+
获取所有的角色的授权规则:
121117

122118
```php
123119
Enforcer::getPolicy();
124120
```
125121

126-
Gets the roles that a user has.
122+
获取某个用户的所有角色:
127123

128124
```php
129125
Enforcer::getRolesForUser('eve'); // ['writer']
130126
```
131127

132-
Gets the users that has a role.
128+
获取某个角色的所有用户:
133129

134130
```php
135131
Enforcer::getUsersForRole('writer'); // ['eve']
136132
```
137133

138-
Determines whether a user has a role.
134+
决定用户是否拥有某个角色:
139135

140136
```php
141137
Enforcer::hasRoleForUser('eve', 'writer'); // true or false
142138
```
143139

144-
Adds a role for a user.
140+
给用户添加角色:
145141

146142
```php
147143
Enforcer::addRoleForUser('eve', 'writer');
148144
```
149145

150-
Adds a permission for a user or role.
146+
赋予权限给某个用户或角色:
151147

152148
```php
153149
// to user
@@ -156,37 +152,37 @@ Enforcer::addPermissionForUser('eve', 'articles', 'read');
156152
Enforcer::addPermissionForUser('writer', 'articles','edit');
157153
```
158154

159-
Deletes a role for a user.
155+
删除用户的角色:
160156

161157
```php
162158
Enforcer::deleteRoleForUser('eve', 'writer');
163159
```
164160

165-
Deletes all roles for a user.
161+
删除某个用户的所有角色:
166162

167163
```php
168164
Enforcer::deleteRolesForUser('eve');
169165
```
170166

171-
Deletes a role.
167+
删除单个角色:
172168

173169
```php
174170
Enforcer::deleteRole('writer');
175171
```
176172

177-
Deletes a permission.
173+
删除某个权限:
178174

179175
```php
180176
Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).
181177
```
182178

183-
Deletes a permission for a user or role.
179+
删除某个用户或角色的权限:
184180

185181
```php
186182
Enforcer::deletePermissionForUser('eve', 'articles', 'read');
187183
```
188184

189-
Deletes permissions for a user or role.
185+
删除某个用户或角色的所有权限:
190186

191187
```php
192188
// to user
@@ -195,43 +191,46 @@ Enforcer::deletePermissionsForUser('eve');
195191
Enforcer::deletePermissionsForUser('writer');
196192
```
197193

198-
Gets permissions for a user or role.
194+
获取用户或角色的所有权限:
199195

200196
```php
201197
Enforcer::getPermissionsForUser('eve'); // return array
202198
```
203199

204-
Determines whether a user has a permission.
200+
决定某个用户是否拥有某个权限
205201

206202
```php
207203
Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false
208204
```
209205

210-
### Using a middleware
206+
更多 `API` 参考 [Casbin API](https://casbin.org/docs/en/management-api)
211207

212-
敬请期待...
208+
### 使用中间件
213209

214-
#### basic Enforcer Middleware
215210

211+
该扩展包带有一个 `Basic` 中间件。 您可以将它们添加到您的`app/middleware.php`文件中:
216212

213+
```php
214+
<?php
217215

218-
#### HTTP Request Middleware ( RESTful is also supported )
219-
220-
221-
```
216+
return [
217+
// ... 其他中间件
218+
'alias' => [
219+
'authz' => \tauthz\middleware\Basic::class,
220+
],
221+
];
222222
```
223223

224-
### Using artisan commands
225-
226-
敬请期待...
227-
228-
### Using cache
224+
然后就可以使用它们来保护路由了:
229225

230-
敬请期待...
226+
```php
227+
Route::get('news/:id','News/Show')
228+
->middleware('authz', ['news', 'read']);
229+
```
231230

232231
## 感谢
233232

234-
[Casbin](https://github.com/php-casbin/php-casbin) . You can find the full documentation of Casbin [on the website](https://casbin.org/).
233+
[Casbin](https://github.com/php-casbin/php-casbin),你可以查看全部文档在其 [官网](https://casbin.org/) 上。
235234

236235
## License
237236

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
],
2121
"license": "Apache-2.0",
2222
"require": {
23-
"casbin/casbin": "~2.0",
24-
"topthink/framework": "6.0.*-dev",
25-
"topthink/think-orm": "2.0.*-dev",
23+
"casbin/casbin": "~2.1",
24+
"topthink/framework": "^6.0.0",
2625
"topthink/think-migration": "^3.0",
2726
"casbin/psr3-bridge": "^1.1"
2827
},
2928
"require-dev": {
3029
"phpunit/phpunit": "~7.0",
3130
"php-coveralls/php-coveralls": "^2.1",
32-
"topthink/think": "6.0.*-dev"
31+
"topthink/think": "^6.0.0"
3332
},
3433
"autoload": {
3534
"psr-4": {
@@ -51,4 +50,4 @@
5150
]
5251
}
5352
}
54-
}
53+
}

src/exception/unauthorized.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace tauthz\exception;
4+
5+
class Unauthorized extends \Exception
6+
{
7+
}

src/middleware/Basic.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace tauthz\middleware;
4+
5+
use tauthz\exception\Unauthorized;
6+
use think\Request;
7+
8+
class Basic
9+
{
10+
/**
11+
* Undocumented function.
12+
*
13+
* @param Request $request
14+
* @param \Closure $next
15+
* @param mixed ...$args
16+
*
17+
* @return mixed
18+
*/
19+
public function handle(Request $request, \Closure $next, $args)
20+
{
21+
$authzIdentifier = $this->getAuthzIdentifier($request);
22+
if (!$request) {
23+
throw new Unauthorized();
24+
}
25+
26+
if (!Enforcer::enforce($authzIdentifier, ...$args)) {
27+
throw new Unauthorized();
28+
}
29+
30+
return $next($request);
31+
}
32+
33+
public function getAuthzIdentifier(Request $request)
34+
{
35+
return $request->middleware('auth_id');
36+
}
37+
}

0 commit comments

Comments
 (0)