多针递归搜索数组- PHP



Array:

$arrayStr = ["user_id" => 2,
"user_name" => "Denny Septian Panggabean",
"user_company" => "KMK Global Sports",
"user_relation" => "Broker",
"user_roles" => "Super Admin",
"user_rules" => [
[
"menu_no" => "1",
"menu_parent" => "0",
"menu_name" => "Dashboard",
"menu_link" => "dashboard",
"menu_icon" => "fas fa-tachometer-alt",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "1",
"menu_locked" => "0"
],
[
"menu_no" => "2",
"menu_parent" => "0",
"menu_name" => "Settings",
"menu_link" => "javascript:void(0)",
"menu_icon" => "fas fa-cogs",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "2",
"menu_locked" => "0",
"menu_child" => [
[
"menu_no" => "5",
"menu_parent" => "2",
"menu_name" => "Diagnosa",
"menu_link" => "dashboard/diagnosa",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "1",
"menu_locked" => "0"
],
[
"menu_no" => "6",
"menu_parent" => "2",
"menu_name" => "Provider",
"menu_link" => "dashboard/provider",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "2",
"menu_locked" => "0"
],
[
"menu_no" => "7",
"menu_parent" => "2",
"menu_name" => "Relation",
"menu_link" => "dashboard/relation",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "3",
"menu_locked" => "0"
],
[
"menu_no" => "8",
"menu_parent" => "2",
"menu_name" => "Plan",
"menu_link" => "dashboard/plan",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "4",
"menu_locked" => "0"
],
[
"menu_no" => "4",
"menu_parent" => "2",
"menu_name" => "Benefit Group",
"menu_link" => "dashboard/benefit-group",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "5",
"menu_locked" => "0"
],
[
"menu_no" => "3",
"menu_parent" => "2",
"menu_name" => "Benefit",
"menu_link" => "dashboard/benefit",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "6",
"menu_locked" => "0"
],
[
"menu_no" => "9",
"menu_parent" => "2",
"menu_name" => "Menu",
"menu_link" => "dashboard/menu",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "R",
"menu_order" => "7",
"menu_locked" => "0"
],
[
"menu_no" => "10",
"menu_parent" => "2",
"menu_name" => "User",
"menu_link" => "dashboard/user",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "8",
"menu_locked" => "0"
],
[
"menu_no" => "11",
"menu_parent" => "2",
"menu_name" => "Policy",
"menu_link" => "dashboard/policy",
"menu_icon" => "fas fa-angle-double-right",
"menu_class" => "",
"menu_access" => "F",
"menu_order" => "9",
"menu_locked" => "0"
],
],
],
],
];

功能:

public function arrRecursiveAccessMap(string $needle = '', string $needleOther = '', array $haystack = [])
{
$dataParams = array(
'create' => array('F','W'),
'update' => array('F','W'),
'delete' => array('F'),
);
$paramKeys = array_keys($dataParams);
if (is_numeric($needleOther) || in_array($needleOther, $paramKeys, true)) {
foreach($haystack as $first_level_key=>$value) {
if ($needle === $value) {
return array($first_level_key);
} elseif (is_array($value)) {
$callback = $this->arrRecursiveAccessMap($needle, $needleOther, $value);

if ($callback) {
return array_merge(array($first_level_key), $callback);
}
}
}

return false;
}
return false;
}

概念:

任何用户都可以访问菜单列表,如果每个用户都输入不可访问的uri参数,则拒绝它。

的例子:

用户John Doe有列表菜单

  1. 产品(完全访问)
  2. 报告(完全访问)
  3. 菜单(只读)

And John Doe openhttps://localhost/dashboard/menu/create, by system decline it.

期望:

我想搜索值仪表板/菜单获取'menu_access'值R. 然后验证if valueR$dataParams['create']别名[' F ', ' W ']

?的例子:

使用:arrRecursiveAccessMap('dashboard/menu', 'create', $arrayStr);

预期结果:false

$dataParams = array(
'create' => array('F','W'),
'update' => array('F','W'),
'delete' => array('F'),
);
$paramKeys = array_keys($dataParams);
  • 在每个不需要的递归调用中重复上面的代码片段。您可以将其作为函数的参数。

  • 你也写了return array_merge(array($first_level_key), $callback);,但你说的是Expect Result : false。因此,也不需要array_merge

  • 你也有这个检查if (is_numeric($needleOther) || in_array($needleOther, $paramKeys, true)) {,这也是不需要的。


解决方案:

您可以简单地创建一个函数,将$dataParamscreate键作为参数传递。继续在menu_child上递归地调用同一个函数,如果在$dataParams['create']中找到menu_access的值,则返回true,否则返回false

片段:

<?php
function userHasAccess($menu_rules, $dataParams, $data_param_key, $value_to_search){
foreach($menu_rules as $menu_rule){
if(($menu_rule['menu_link'] === $value_to_search && in_array($menu_rule['menu_access'], $dataParams[$data_param_key]))
|| userHasAccess($menu_rule['menu_child'] ?? [], $dataParams, $data_param_key, $value_to_search)){
return true;
}
}
return false;
}
var_dump(userHasAccess($arrayStr['user_rules'], $dataParams, 'create', 'dashboard/menu'));
<<p>在线演示/strong>

相关内容

  • 没有找到相关文章