Home > php教程 > php手册 > PHPCMS不显示无权限栏目对应的左侧菜单

PHPCMS不显示无权限栏目对应的左侧菜单

WBOY
Release: 2016-06-13 09:39:12
Original
804 people have browsed it

PHPCMS可以给某些角色设定权限,比如某些栏目对于某些角色是无法访问的。但是左侧菜单还是会显示该栏目名称,这对编辑来说是不需要的。都访问不了,还显示栏目名称干啥呢。看了一下PHPCMS的代码,简单地实现了隐藏菜单的小功能。

从后台页面可以很简单地分析出,左侧菜单的生成是用Ajax:

$.ajax(
{	
	type:'get', 
	url:'?mod=phpcms&file=menu&action=get_menu_list&menuid='+id, 
	cache:cache_refresh,
	dataType:'json', 
	success:function(json)
	{
		……
	}
}
Copy after login

请求的地址是 http://www.bkjia.com/admin.php?mod=phpcms&file=menu&action=get_menu_list&menuid=4

在URL参数中,file是指请求的控制器文件,这里menu就是指 admin/menu.inc.php这个文件,action 则是方法。我们找到 menu.inc.php 下的 get_menu_list 方法:

case 'get_menu_list':
	$data = $m->get_child($menuid);
	$data = str_charset(CHARSET, 'utf-8', $data);
	$max = array_slice($data, -1);
	$data['max'] = $max[0]['menuid'];
	$data = json_encode($data);
	if(PHP_OS < 5.0) header('Content-type: text/html; charset=utf-8');
	echo $data;
	break;
Copy after login

将其改为:

case 'get_menu_list':
	$data = $m->get_child($menuid);
	
	foreach($data as $k=>$val)
	{
		if( !empty($data[$k]['keyid']) && substr( $data[$k]['keyid'], 0, 5 ) == 'catid' )
		{
			if( $data[$k]['keyid'] == 'catid_0' )
			{
				continue;
			}
			$catid = str_replace("catid_", "", $data[$k]['keyid']);
			
			// 管理权限
			$allow_manage = $priv_role->check('catid', $catid, 'manage');
			
			if(!$allow_manage)
			{
				unset($data[$k]);
			}
		}
	}
	
	$data = str_charset(CHARSET, 'utf-8', $data);
	$max = array_slice($data, -1);
	$data['max'] = $max[0]['menuid'];
	$data = json_encode($data);
	if(PHP_OS < 5.0) header('Content-type: text/html; charset=utf-8');
	echo $data;
	break;
Copy after login

代码存在投机性,但大概思路就是这样。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template