VerifyAuth.class.php
3.64 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* VerifyAuth.class.php
* 验证操作的权限
* $Author$
*/
namespace Com;
use VcySDK\Adminer;
use VcySDK\Service;
class VerifyAuth
{
/**
* 权限缓存文件的文件名
*
* @var string
*/
protected $_cache_file;
/**
* 缓存的配置
*
* @var array
*/
protected $_cache_options;
/**
* 缓存的有权限的url数组
*
* @var array
*/
protected $_cache_list = array();
/**
* 管理员ID
*
* @var string
*/
protected $_eaId;
/**
* 当前操作
*
* @var string
*/
protected $_current_action;
/**
* 实例化
*
* @param string $eaId 管理员ID
*
* @return VerifyAuth
*/
public static function &instance($eaId)
{
static $instance = array();
if (empty($instance[$eaId])) {
$instance[$eaId] = new self($eaId);
}
return $instance[$eaId];
}
/**
* 实例化
*
* @param string $eaId 管理员ID
*/
public function __construct($eaId)
{
// 管理员ID
$this->_eaId = $eaId;
// 缓存的文件名
$this->_cache_file = 'authAction_' . $eaId;
// 缓存的配置
$this->_cache_options = array(
'temp' => get_sitedir()
);
// 初始化一下有权限的url数组
$this->_get_cache();
// 如果操作是Index就省略,保持和前端传过来的一致,才好判断相等
$action = ACTION_NAME;
$action = $action == 'Index' ? '' : '/' . $action;
// 当前操作的地址
$this->_current_action = APP_DIR . '/' . MODULE_NAME . '/' . CONTROLLER_NAME . $action;
}
/**
* 验证权限
*
* @return boolean 有权限返回true,否则返回false
*/
public function verify()
{
// 返回是否有权限
return ! empty($this->_eaId) && is_array($this->_cache_list) && in_array($this->_current_action, $this->_cache_list);
}
/**
* 获取管理员的菜单权限
*
* @param array $menu 菜单权限原始数据数组
*
* @return bool
*/
protected function _get_eaCpmenu($menu = array())
{
// 如果原始数据是空,就请求一次UC获取
if (empty($menu)) {
$sdk = new Adminer(Service::instance());
$info = $sdk->fetch(array(
'eaId' => $this->_eaId
));
$menu = isset($info['eaCpmenu']) ? unserialize($info['eaCpmenu']) : array();
}
// 如果原始数据是空的,就不继续处理了
if (empty($menu)) {
return true;
}
// 循环原始数据,摘出有权限的URL
foreach ($menu as $v) {
// 摘出url,存入$data
if (isset($v['api']['url']) && ! empty($v['api']['url'])) {
$this->_cache_list[] = $v['api']['url'];
}
// 如果还有下级,就递归
if (isset($v['subMenu']) && ! empty($v['subMenu'])) {
$this->_get_eaCpmenu($v['subMenu']);
}
}
return true;
}
/**
* 获取缓存中的有权限的url数组
*/
protected function _get_cache()
{
// 读取缓存文件,获取有权限的操作
$this->_cache_list = S($this->_cache_file, '', $this->_cache_options);
// 如果缓存不存在就去重新缓存一下
if (empty($this->_cache_list)) {
$this->_get_eaCpmenu();
S($this->_cache_file, $this->_cache_list, $this->_cache_options);
}
return true;
}
}