-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
139 lines (134 loc) · 2.95 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* 向角色中添加权限名
* url_acl_add_auth("管理员",'产品');
* url_acl_add_auth("管理员",['产品','产品列表']);
*/
function url_acl_add_auth($auth_name, $acl_title)
{
global $_url_acl_add_auth;
if(is_array($acl_title)) {
foreach($acl_title as $title) {
$_url_acl_add_auth[$auth_name][$title] = $title;
}
} else {
$_url_acl_add_auth[$auth_name][$acl_title] = $acl_title;
}
}
/**
* 检测角色是否有权限
*/
function url_acl($auth_name)
{
if(is_array($auth_name)) {
foreach($auth_name as $v) {
if(!$list) {
$list = get_url_acl_auth_urls($v);
} else {
$append = get_url_acl_auth_urls($v);
$list = array_merge($list, $append);
}
}
} else {
$list = get_url_acl_auth_urls($auth_name);
}
if(!$list) {
return false;
}
$uri = get_url_acl_request_uri();
if(function_exists('do_action')) {
$data = ['list' => $list,'uri' => $uri];
do_action("acl.check", $data);
if($data['res']) {
return true;
} else {
return false;
}
}
if($uri && in_array($uri, $list)) {
return true;
} else {
return false;
}
}
/**
* 取角色有的权限
* 返回可以访问的URL
*/
function get_url_acl_auth_urls($auth_name)
{
global $_url_acl_list;
$list = get_url_acl_auth_name($auth_name);
if(!$list) {
return ;
} else {
$urls = [];
foreach($list as $k => $v) {
$url = [];
$arr = $_url_acl_list[$v];
if(is_array($arr)) {
foreach($arr as $v1) {
$url[] = $v1;
}
} else {
$url = $arr;
}
$urls[] = $url;
}
$list = [];
foreach ($urls as $key => $v) {
foreach($v as $v1) {
$list[$v1] = $v1;
}
}
return $list;
}
}
/**
* 取角色拥有的ACL名称
*/
function get_url_acl_auth_name($auth_name)
{
global $_url_acl_add_auth;
if(!is_array($_url_acl_add_auth) || !isset($_url_acl_add_auth[$auth_name])) {
return;
}
$list = $_url_acl_add_auth[$auth_name];
return $list;
}
/**
* 把URL添加到权限中
*/
function url_acl_add($title, $url)
{
global $_url_acl_list;
if(is_array($url)) {
foreach($url as $v) {
$_url_acl_list[$title][] = $v;
}
} else {
$_url_acl_list[$title][] = $url;
}
}
/**
* 取请求URL,不包含?
*/
function get_url_acl_request_uri()
{
$uri = $_SERVER['REQUEST_URI'];
if(strpos($uri, '?') !== false) {
$uri = substr($uri, 0, strpos($uri, '?'));
}
if(function_exists('do_action')) {
do_action("acl.url", $uri);
}
return $uri;
}
/**
* 权限列表
*/
function get_url_acl()
{
global $_url_acl_list;
return $_url_acl_list;
}