向数组添加了更多数据


added more data to an array

我目前有一个数组,其中包含用户可以执行的信息。我目前在下拉菜单中使用了一个实例,这非常好。我现在想做的是,只有当他们有权访问时,才包括特定的项目。所以说,如果$user_access=2,那么他们可以下载->small_image,但不能下载->large_image。我希望能够为数组中的每个项目分配默认访问权限。。。甚至是钥匙。所以我可以说用户访问权限是0,所以他们根本不应该显示"下载"。

我想也许把每个对象的访问整数放在一个对象中。因此$access->download->small_image=2;然后我可以逐一检查访问级别是否大于或等于它,并将其添加到数组中?实际上,我不确定如何正确地执行循环和构建数组。另一种方式。。。也许以某种方式将访问值放在实际数组中?任何想法都很棒。

$var = array("Download" =>                                                
                   array("small_image" => "ajax_load",                         
                         "large_image" => "/master.php"),                       
                   "Page" =>                                                    
                   array("Edit" => "edit.php",                                  
                         "View" => "view.php",                                  
                         "Stuff" => "stuff.php")                                
                   );      

访问控制总是一个模糊的问题,因为有无限多的选项。听起来你会对一个访问数组感到满意,IE:

array(
    0 => "root",
    10 => "Group1",
    20 => "Group2"
);

我可能会为你的ACL(访问控制列表)使用一个数组:

$ACL = array(
    "Download" => array(
        "Small_Image" => 10,
        "Large_Image" => 20
    ),
    "Page" => array(
        "Edit" => 0,
        "View" => 10
    )
);

然后调用一个函数进行授权:

function checkACL($option, $arg, $lvl)
{
    if( $ACL[$option][$arg] >= $lvl )
        return true;
    else
        return false;
};
$userLevel = 10;
if( checkACL("Page", "Edit", $userLevel) ) {
    /*
     * Display Edit page here.
     */
} else {
    /*
     * Display "Not authorized" or redirect to a different page.
     */
}

对不起,希望这个话题不要太离题
但你可以调整它来显示你的菜单。我假设你的登录系统已经很粗糙了:)

if( checkACL("Download", "Small_Image", $userLevel) ) 
    echo '<a href="'.$var['Download']['Small_Image'].'">Small Image</a>';

编辑:

$var = array(
    "download.small_image" => array(
        "id"    => 1001,
        "file"  => "/img/blue.png",
        "width" => "100px",
        "height"=> "100px",
        "accessLevel"=> 20
    ),
    "page.listUsers" => array(
        "id"   => 1011,
        "file" => "/pages/list.php",
        "accessLevel" => 10
    )
);

因此,它的工作原理相同,但所有信息都是集中的
如果我不使用数据库,我会把它存储为json文件
您不想将其硬编码到php文件中,而是希望它易于修改。

// This will load your permissions into $var
$var = json_decode(file_get_contents("/acl.json"));
// If you design a page to modify permissions, you can save the file with this.
file_put_contents("/acl.json", json_encode($var));
// json is simple, it's basically just { "key" : "val" }