tp後臺許可權管理日記
//管理員表
CREATE TABLE `qz_admin` (
`admin_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT '使用者id',
`admin_name` varchar(60) NOT NULL DEFAULT '' COMMENT '使用者名稱',
`email` varchar(60) NOT NULL DEFAULT '' COMMENT 'email',
`password` varchar(32) NOT NULL DEFAULT '' COMMENT '密碼',
`add_time` int(11) NOT NULL DEFAULT '0' COMMENT '新增時間',
`last_login` int(11) NOT NULL DEFAULT '0' COMMENT '最後登入時間',
`last_ip` varchar(15) NOT NULL DEFAULT '' COMMENT '最後登入ip',
`nav_list` text NOT NULL COMMENT '許可權',
`role_id` smallint(5) DEFAULT NULL COMMENT '角色id',
PRIMARY KEY (`admin_id`),
KEY `user_name` (`user_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
//角色表
CREATE TABLE `qz_admin_role` (
`role_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT COMMENT '角色ID',
`role_name` varchar(30) DEFAULT NULL COMMENT '角色名稱',
`act_list` text COMMENT '許可權列表',
`role_desc` varchar(255) DEFAULT NULL COMMENT '角色描述',
PRIMARY KEY (`role_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
//功能選單表
CREATE TABLE `qz_system_menu` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL COMMENT '許可權名字',
`group` varchar(20) DEFAULT NULL COMMENT '所屬分組',
`right` text COMMENT '許可權碼(控制器+動作)',
`is_del` tinyint(1) DEFAULT '0' COMMENT '刪除狀態 1刪除,0正常',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
function getMenuList($act_list){
//根據角色許可權過濾選單
$menu_list = getAllMenu();
if($act_list != 'all'){
$right = M('system_menu')->where("id in ($act_list)")->cache(false)->getField('right',true);
$role_right = "";
foreach ($right as $val){
$role_right .= $val.',';
}
$role_right = explode(',', $role_right);
foreach($menu_list as $k=>$mrr){
foreach ($mrr['sub_menu'] as $j=>$v){
if(!in_array($v['control'].'
unset($menu_list[$k]['sub_menu'][$j]);//過濾選單
}
}
}
}
return $menu_list;
}
function getAllMenu(){
return array(
'system' => array('name'=>'系統設定','icon'=>'fa-cog','sub_menu'=>array(
array('name'=>'網站設定','act'=>'index','control'=>'System'),
array('name'=>'友情連結','act'=>'linkList','control'=>'Article'),
array('name'=>'自定義導航','act'=>'navigationList','control'=>'System'),
array('name'=>'區域管理','act'=>'region','control'=>'Tools'),
array('name'=>'許可權資源列表','act'=>'right_list','control'=>'System'),
)),
'access' => array('name' => '許可權管理', 'icon'=>'fa-gears', 'sub_menu' => array(
array('name' => '管理員列表', 'act'=>'index', 'control'=>'Admin'),
array('name' => '角色管理', 'act'=>'role', 'control'=>'Admin'),
array('name' => '供應商管理', 'act'=>'supplier', 'control'=>'Admin'),
array('name' => '管理員日誌', 'act'=>'log', 'control'=>'Admin'),
)),
'member' => array('name'=>'會員管理','icon'=>'fa-user','sub_menu'=>array(
array('name'=>'會員列表','act'=>'index','control'=>'User'),
array('name'=>'會員等級','act'=>'levelList','control'=>'User'),
)),
'article' => array('name' => '文章管理', 'icon'=>'fa-comments', 'sub_menu' => array(
array('name' => '文章列表', 'act'=>'articleList', 'control'=>'Article'),
array('name' => '文章分類', 'act'=>'categoryList', 'control'=>'Article'),
array('name' => '專題列表', 'act'=>'topicList', 'control'=>'Topic'),
))
);
}
//許可權驗證函式(TP), 入口初始化執行
public function check_priv()
{
$ctl = CONTROLLER_NAME;
$act = ACTION_NAME;
$act_list = session('act_list');
//無需驗證的操作
$uneed_check = array('login','logout','vertifyHandle','vertify','imageUp','upload','login_task');
if($ctl == 'Index' || $act_list == 'all'){
//後臺首頁控制器無需驗證,超級管理員無需驗證
return true;
}elseif(strpos('ajax',$act) || in_array($act,$uneed_check)){
//所有ajax請求不需要驗證許可權
return true;
}else{
$role_right = "";
$right = M('system_menu')->where("id in ($act_list)")->cache(true)->getField('right',true);
foreach ($right as $val){
$role_right .= $val.',';
}
$role_right = explode(',', $role_right);
//檢查是否擁有此操作許可權
if(!in_array($ctl.'[email protected]'.$act, $role_right)){
$this->error('您沒有操作許可權,請聯絡超級管理員分配許可權',U('Admin/Index/index'));
}
}
}
//許可權分配,給角色新增、修改許可權
public function role_info(){
$role_id = I('get.role_id');
$tree = $detail = array();
if($role_id){
$detail = M('admin_role')->where("role_id=$role_id")->find();
$detail['act_list'] = explode(',', $detail['act_list']);
$this->assign('detail',$detail);
}
$right = M('system_menu')->order('id')->select();
foreach ($right as $val){
if(!empty($detail)){
$val['enable'] = in_array($val['id'], $detail['act_list']);
}
$modules[$val['group']][] = $val;
}
//許可權組(對應資料表裡面的group欄位)
$group = array('system'=>'系統管理','access'=>'許可權管理',article'=>'文章管理','member'=>'會員管理');
$this->assign('group',$group);
$this->assign('modules',$modules);
$this->display();
}
//許可權分配儲存
public function roleSave(){
$data = I('post.');
$res = $data['data'];
$res['act_list'] = is_array($data['right']) ? implode(',', $data['right']) : '';
if(empty($data['role_id'])){
$r = D('admin_role')->add($res);
}else{
$r = D('admin_role')->where('role_id='.$data['role_id'])->save($res);
}
if($r){
adminLog('管理角色',__ACTION__);
$this->success("操作成功!",U('Admin/Admin/role_info',array('role_id'=>$data['role_id'])));
}else{
$this->success("操作失敗!",U('Admin/Admin/role'));
}
}