Skip to content

Commit fbbaaf1

Browse files
committed
优化权限控制
1 parent 986a14b commit fbbaaf1

File tree

5 files changed

+97
-59
lines changed

5 files changed

+97
-59
lines changed

helper.php

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,87 @@
22

33
use tadmin\model\Config;
44

5-
function script_path()
6-
{
7-
if ('cli' == PHP_SAPI) {
8-
$scriptName = realpath($_SERVER['argv'][0]);
9-
} else {
10-
$scriptName = $_SERVER['SCRIPT_FILENAME'];
11-
}
5+
if (!function_exists('script_path')) {
6+
function script_path()
7+
{
8+
if ('cli' == PHP_SAPI) {
9+
$scriptName = realpath($_SERVER['argv'][0]);
10+
} else {
11+
$scriptName = $_SERVER['SCRIPT_FILENAME'];
12+
}
1213

13-
return realpath(dirname($scriptName)).'/';
14+
return realpath(dirname($scriptName)).'/';
15+
}
1416
}
1517

16-
function app_path($path = '')
17-
{
18-
return env('app_path').ltrim($path, '/');
18+
if (!function_exists('app_path')) {
19+
function app_path($path = '')
20+
{
21+
return env('app_path').ltrim($path, '/');
22+
}
1923
}
2024

21-
function public_path($path = '')
22-
{
23-
return script_path().ltrim($path, '/');
24-
// return app_path('../public/').ltrim($path, '/');
25+
if (!function_exists('public_path')) {
26+
function public_path($path = '')
27+
{
28+
return script_path().ltrim($path, '/');
29+
// return app_path('../public/').ltrim($path, '/');
30+
}
2531
}
2632

27-
function admin_path($path = '')
28-
{
29-
return __DIR__.'/'.ltrim($path, '/');
33+
if (!function_exists('admin_path')) {
34+
function admin_path($path = '')
35+
{
36+
return __DIR__.'/'.ltrim($path, '/');
37+
}
3038
}
3139

32-
function admin_config_path($path = '')
33-
{
34-
return admin_path('config/').ltrim($path, '/');
40+
if (!function_exists('admin_config_path')) {
41+
function admin_config_path($path = '')
42+
{
43+
return admin_path('config/').ltrim($path, '/');
44+
}
3545
}
3646

37-
function admin_route_path($path = '')
38-
{
39-
return admin_path('route/').ltrim($path, '/');
47+
if (!function_exists('admin_route_path')) {
48+
function admin_route_path($path = '')
49+
{
50+
return admin_path('route/').ltrim($path, '/');
51+
}
4052
}
4153

42-
function admin_view_path($path = '')
43-
{
44-
return admin_path('resource/view/').ltrim($path, '/');
54+
if (!function_exists('admin_view_path')) {
55+
function admin_view_path($path = '')
56+
{
57+
return admin_path('resource/view/').ltrim($path, '/');
58+
}
4559
}
4660

47-
function site_config($key)
48-
{
49-
return Config::get($key);
61+
if (!function_exists('site_config')) {
62+
function site_config($key)
63+
{
64+
return Config::get($key);
65+
}
5066
}
5167

52-
function array_deep_merge(array $a, array $b)
53-
{
54-
foreach ($a as $key => $val) {
55-
if (isset($b[$key])) {
56-
if (gettype($a[$key]) != gettype($b[$key])) {
57-
continue;
58-
}
59-
if (is_array($a[$key])) {
60-
$a[$key] = array_deep_merge($a[$key], $b[$key]);
61-
} else {
62-
$a[$key] = $b[$key];
68+
if (!function_exists('array_deep_merge')) {
69+
function array_deep_merge(array $a, array $b)
70+
{
71+
foreach ($a as $key => $val) {
72+
if (isset($b[$key])) {
73+
if (gettype($a[$key]) != gettype($b[$key])) {
74+
continue;
75+
}
76+
if (is_array($a[$key])) {
77+
$a[$key] = array_deep_merge($a[$key], $b[$key]);
78+
} else {
79+
$a[$key] = $b[$key];
80+
}
6381
}
6482
}
65-
}
6683

67-
return $a;
84+
return $a;
85+
}
6886
}
6987

7088
\think\Console::addDefaultCommands([

src/controller/Transfer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace tadmin\controller;
4+
5+
use tadmin\support\controller\Controller;
6+
7+
class Transfer extends Controller
8+
{
9+
public function message($value = null)
10+
{
11+
$this->error($value);
12+
}
13+
}

src/controller/auth/Adminer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected function updateAdminer(Request $request)
121121
protected function validateAdminAccount(array $data)
122122
{
123123
$validate = Validate::make([
124-
'admin_account' => 'require|alphaDash|max:16|unique:adminer',
124+
'admin_account' => 'require|alphaDash|max:16|unique:adminers',
125125
], [
126126
'admin_account.require' => '登录账号必须',
127127
'admin_account.alphaDash' => '登录账号只能是字母、数字和下划线_及破折号-',

src/middleware/PermissionCheck.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace tadmin\middleware;
44

5+
use tadmin\controller\Transfer;
56
use tadmin\service\auth\facade\Auth;
67
use Casbin;
78

@@ -21,8 +22,9 @@ public function handle($request, \Closure $next)
2122
return $next($request);
2223
}
2324

24-
if (true !== Casbin::enforce($this->request->method(true), $this->parseCurrentPath())) {
25-
throw new \Exception('权限不足');
25+
if (true !== Casbin::enforce('adminer.'.$adminer->id, $this->request->method(true), $this->parseCurrentPath())) {
26+
return controller(Transfer::class, '')->message('权限不足');
27+
// throw new \Exception('权限不足');
2628
}
2729

2830
return $next($request);

src/service/casbin/Adapter.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,46 @@
33
namespace tadmin\service\casbin;
44

55
use Casbin\Persist\Adapter as AdapterContract;
6-
use tadmin\model\Role;
76
use Casbin\Exceptions\CasbinException;
87
use Casbin\Persist\AdapterHelper;
8+
use tadmin\model\AdminerRole;
9+
use tadmin\model\Role;
10+
use tadmin\service\auth\facade\Auth;
911

1012
class Adapter implements AdapterContract
1113
{
1214
use AdapterHelper;
1315

14-
protected $roleId;
15-
16-
protected $role;
17-
18-
public function __construct()
19-
{
20-
// $this->role = $role;
21-
}
22-
2316
public function savePolicyLine($ptype, array $rule)
2417
{
2518
}
2619

2720
public function loadPolicy($model)
2821
{
29-
$roles = Role::when(null !== $this->roleId, function ($query) {
30-
$query->where('id', $this->roleId);
22+
$adminer = Auth::user();
23+
// 加载所有(当前登录用户)的角色及其权限
24+
$roles = Role::when($adminer, function ($query) use ($adminer) {
25+
$query->whereIn('id', $adminer->roles->column('id'));
3126
})
3227
->with('permissions')
3328
->select();
3429

3530
foreach ($roles as $role) {
3631
foreach ($role->permissions as $permission) {
37-
$line = 'p, '.$permission->http_method.', '.$permission->http_path;
32+
$line = 'p, role.'.$role->id.', '.$permission->http_method.', '.$permission->http_path;
3833
$this->loadPolicyLine(trim($line), $model);
3934
}
4035
}
36+
37+
// 加载(当前登录)用户和角色的关系
38+
$adminersOfRoles = AdminerRole::when($adminer, function ($query) use ($adminer) {
39+
$query->where('adminer_id', $adminer->id);
40+
})
41+
->select();
42+
foreach ($adminersOfRoles as $aor) {
43+
$line = 'g, adminer.'.$aor->adminer_id.', role.'.$aor->role_id;
44+
$this->loadPolicyLine(trim($line), $model);
45+
}
4146
}
4247

4348
public function savePolicy($model)

0 commit comments

Comments
 (0)