[更新]权限节点刷新机制

This commit is contained in:
邹景立 2017-03-17 16:06:27 +08:00
parent fa522c8e83
commit 1adba1fbf0
2 changed files with 35 additions and 12 deletions

View File

@ -39,6 +39,39 @@ class Node {
return false;
}
/**
* 获取授权节点
* @staticvar array $nodes
* @return array
*/
public static function getAuthNode() {
static $nodes = [];
if (empty($nodes)) {
$nodes = cache('need_access_node');
if (empty($nodes)) {
$nodes = Db::name('SystemNode')->where('is_auth', '1')->column('node');
cache('need_access_node', $nodes);
}
}
return $nodes;
}
/**
* 检查用户节点权限
* @param string $node
* @return bool
*/
public static function checkAuthNode($node) {
$auth_node = strtolower($node);
if (session('user.username') === 'admin' || stripos($node, 'admin/index') === 0) {
return true;
}
if (!in_array($auth_node, self::getAuthNode())) {
return true;
}
return in_array($auth_node, (array) session('user.nodes'));
}
/**
* 获取系统代码节点
* @return array

View File

@ -13,6 +13,7 @@
// +----------------------------------------------------------------------
use app\admin\model\Node as NodeModal;
use think\Config;
use think\Db;
use Wechat\Loader;
@ -73,18 +74,7 @@ function decode($string) {
* @return bool
*/
function auth($node) {
$nodes = cache('need_access_node');
if (empty($nodes)) {
$nodes = Db::name('SystemNode')->where('is_auth', '1')->column('node');
cache('need_access_node', $nodes);
}
if (session('user.username') === 'admin' || stripos($node, 'admin/index') === 0) {
return true;
}
if (!in_array(strtolower($node), array_values($nodes))) {
return true;
}
return in_array(strtolower($node), (array) session('user.nodes'));
return NodeModal::checkAuthNode($node);
}
/**