配置文件:
'AUTHLIST'=>array(
//需要级别1访问的列表
"1"=>array("uploadfile","myuploadedfiles","deletescrb"),
//需要级别2访问的列表
"2"=>array("scrb",'jinriscrb','zhouqitzbb','zhibiaotzbb','duibiaoqxfx','excelToHtml','gongyequlishi','ajaxScrb','getjishi'),
),
控制器:
// 权限验证(只适合只有一个模块的多个模块需要重新进行相应的配置)
$authlist=C("AUTHLIST");
$needauth="";
// 循环取得需要验证的列表
foreach ($authlist as $key => $value) {
foreach ($value as $k => $v) {
if ($v==ACTION_NAME) {
$needauth=$key;
break;
}
}
}
if ($needauth) {
$userauth=M("User")->field('auth')->find(session("xxdb.id"));
if (!in_array($needauth,explode(',',$userauth['auth']))) {
return $this->error("您没有权限!");
}
}
数据库配置一个auth字段
前台登录的显示和隐藏
// 根据权限设置左侧的显示和不显示
// 即使个人修改显示也无法访问或者用js移除节点也可以
function list_allow($actname){
$authlist=C("AUTHLIST");
$needauth="";
foreach ($authlist as $key => $value) {
foreach ($value as $k => $v) {
if ($v==$actname) {
$needauth=$key;
break;
}
}
}
if ($needauth) {
$userauth=M("User")->field('auth')->find(session("xxdb.id"));
if (!in_array($needauth,explode(',',$userauth['auth']))) {
return "display:none;";
}
}
return "";
}
前台用js移除不显示的节点
$(".left_switch li").each(function() {
var isnone=$(this).attr('style');
if (isnone && isnone.indexOf('none')) {
$(this).remove();
}
if ($(this).find('span').text() == mtt) {
$(this).addClass("active");
$(this).find('dl').slideDown().end().siblings('li').find('dl').slideUp();
}
})