优化菜单规则中的条件判断

pull/312/head
Karson 2021-05-19 17:52:34 +08:00
parent ab083686a8
commit cdebb284c3
1 changed files with 13 additions and 5 deletions

View File

@ -205,9 +205,17 @@ class Auth
if (!empty($rule['condition']) && !in_array('*', $ids)) {
//根据condition进行验证
$user = $this->getUserInfo($uid); //获取用户信息,一维数组
$command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule['condition']);
@(eval('$condition=(' . $command . ');'));
if ($condition) {
$nums = 0;
$condition = str_replace(['&&', '||'], "\r\n", $rule['condition']);
$condition = preg_replace('/\{(\w*?)\}/', '\\1', $condition);
$conditionArr = explode("\r\n", $condition);
foreach ($conditionArr as $index => $item) {
preg_match("/^(\w+)\s?([\>\<\=]+)\s?(.*)$/", trim($item), $matches);
if ($matches && isset($user[$matches[1]]) && version_compare($user[$matches[1]], $matches[3], $matches[2])) {
$nums++;
}
}
if ($conditionArr && ((stripos($rule['condition'], "||") !== false && $nums > 0) || count($conditionArr) == $nums)) {
$rulelist[$rule['id']] = strtolower($rule['name']);
}
} else {