Created
August 11, 2020 11:49
-
-
Save llychao/37223c5791bd0f546691fbd5ae690bd7 to your computer and use it in GitHub Desktop.
正则实现权限控制
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 验证权限 | |
* @param $path : controller/action | |
* @param $rount_rule | |
* array ( | |
'allow' => | |
array ( | |
'test/test', | |
'Doc/*', //允许project/所有方法 | |
'*' //允许所有 | |
), | |
'refuse' => | |
array ( | |
'test/index', | |
'project/*', //禁止project/所有方法 | |
'*' //禁止所有 | |
), | |
)' | |
* @return bool | |
*/ | |
function checkRoute($path, $rount_rule) | |
{ | |
if (!$rount_rule) {//如果没写规则就都拒绝 | |
return false; | |
} | |
if (isset($rount_rule['refuse']) && $rount_rule['refuse']) {//先验证拒绝权限 | |
$route_arr = array_filter($rount_rule['refuse']); | |
if (in_array('*', $route_arr)) { | |
return false; | |
} | |
$route_pattern = "/" . str_ireplace('/', '\/', join('|', $route_arr)) . "/i"; | |
if (preg_match($route_pattern, $path)) { | |
return false; | |
} | |
} | |
if (isset($rount_rule['allow']) && $rount_rule['allow']) {//验证允许权限 | |
$route_arr = array_filter($rount_rule['allow']); | |
if (in_array('*', $route_arr)) { | |
return true; | |
} | |
$route_pattern = "/" . str_ireplace('/', '\/', join('|', $route_arr)) . "/i"; | |
if (preg_match($route_pattern, $path)) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment