出現場景:
?
1. 沒有登錄,也能訪問網頁
2. 沒有相關權限,也能訪問對應的控制器和方法
?
解決方案:
?
定義一個CommonController,其他控制器繼承CommonController,在CommonController中定義初始化方法_initialize
注:這里用的是tp3.2框架,如果我們直接在CommonController中定義__constrct構造方法,會造成重寫Controller,所以我們直接使用_initialize方法(_initialize方法是tp框架的初始化方法)
注:也可以parent::__construct();[tp框架中推薦用_initalize方法,如果是其他框架或是原生,可以用parent::__construct()]
?
具體實施:
?
1. 解決沒有登錄也可以訪問網頁
?
<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {Public function _initialize(){// 初始化的時候檢查用戶權限if(!isset($_SESSION['username']) || $_SESSION['username']==''){$this->redirect('Login/login');}}
}
?>
核心思想: 判斷是否有session
?
2. ?解決沒有相關權限,也能訪問對應的控制器和方法
?
核心思想:
(1) 從session中獲取當前訪問的角色id role_id
(2) 根據role_id從role表中查詢role_auth_path的值(role_auth_path記錄了當前角色所有能夠訪問的控制器-方法字符串)
(3) 獲取當前訪問的控制器-方法字符串,和當前角色的role_auth_path進行比較,如果當前訪問的控制器-方法在role_auth_path中,則讓其正常訪問, 如果不在,則說明當前角色不具有訪問權限,為跳墻訪問
數據表地址:?http://blog.csdn.net/m_nanle_xiaobudiu/article/details/79443389
?
具體實施:
?
<?php
namespace Home\Controller;
use Think\Controller;class CommonController extends Controller{function _initialize(){//檢測sessionif(!session('?id')){$this->error('您尚未登錄,請登錄后再訪問', U('Index/login'));}//獲取當前訪問的控制器-方法$now_path = CONTROLLER_NAME.'-'.ACTION_NAME;//獲取session中的roleid,從role表中獲取role_auth_path$role_id = session('role_id');$role_info = D('Role')->field("role_auth_path")->find($role_id);$role_auth_path = $role_info['role_auth_path'];//將 role_auth_path 轉為數組$role_auth_path = explode(',', $role_auth_path);//判斷$now_path是否在$role_auth_path中if(!in_array($now_path, $role_auth_path)){//如果不存在,則為跳墻訪問,跳轉回登錄頁$this->error('您無權訪問該模塊,請重新登錄再訪問', U('Index/login'), 3);}}
}
?
備注:
為角色分配權限
?
html部分:
?
角色列表頁面roleList.html
?
<table ><thead><tr><th><input name="" type="checkbox" value="" id="checkAll" /></th><th>編號</th><th>角色名</th><th>權限ids</th><th>權限路徑</th><th>操作</th></tr></thead><tbody><foreach name="role_list" item="vo"><tr><td><input name="" type="checkbox" value="" /></td><td>{$vo.role_id}</td><td>{$vo.role_name}</td><td>{$vo.role_auth_ids}</td><td>{$vo.role_auth_path}</td><td><a href="{:U('distribute', 'role_id='.$vo[role_id])}" class="tablelink">分配權限</a> <a href="#" class="tablelink"> 刪除</a></td></tr></foreach></tbody>
</table>
?
權限列表 distribute.html
?
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><script language="JavaScript" src="__ADMIN__/js/jquery.js"></script>
</head><body><div><form action="" method="post"><input type="hidden" name="role_id" value="{$Think.get.role_id}" />您正在給【<label style="color: red;font-weight: bolder;">{$role_info.role_name}</label>】設置權限<ul><table><thead><tr><th>權限分類</th><th>權限</th></tr></thead><tbody><foreach name="auth_p" item="vo"><tr><td><in name="vo.auth_id" value="$role_info.role_auth_ids"><input type="checkbox" name="auth_id[]" value="{$vo.auth_id}" checked="checked" /><else /><input type="checkbox" name="auth_id[]" value="{$vo.auth_id}" /></in>{$vo.auth_name}</td><td><foreach name="auth_s" item="v"><if condition="$v.auth_pid eq $vo.auth_id "><input type="checkbox" name="auth_id[]" value="{$v.auth_id}"<in name="v.auth_id" value="$role_info.role_auth_ids">checked="checked"</in>>{$v.auth_name} </if></foreach></td></tr></foreach></tbody></table><br/><li><label> </label><input name="" id="btnSubmit" type="button" class="btn" value="確認保存" /></li></ul></form></div>
</body><script type="text/javascript">
$(function(){//給btnsubmit綁定點擊事件$('#btnSubmit').on('click',function(){//表單提交$('form').submit();})
});
</script>
</html>
?
php部分: 對應的方法:
?
?
<?php
function distribute(){$role_model = D('Role');if(IS_POST){//1. 接收role_id的值$role_id = I('post.role_id');//1. 接收表單數據$ids = I('post.auth_id');//dump($ids);//2. 將數組轉為字符串$ids = implode(',', $ids);//3. 根據ids從auth表中查詢數據,拼接role_auth_path需要數據$auth_list = D('Auth')->where("auth_id in ($ids)")->select();//dump($auth_list);die;$role_auth_path = '';foreach($auth_list as $value){if($value['auth_c'] != ''){$role_auth_path .= $value['auth_c'].'-'.$value['auth_a'].',';}}//去掉最后一個 ,$role_auth_path = rtrim($role_auth_path, ',');//4. 構造修改數據$save_data = array('role_id' => $role_id,'role_auth_ids' => $ids,'role_auth_path' => $role_auth_path);if($role_model->save($save_data)){$this->success('分配權限成功', U('roleList'), 3);} else {$this->error('分配權限失敗', U('roleList'), 3);}} else {//1.接收角色id$role_id = I('get.role_id');//2. 根據角色id查詢角色信息$role_info = $role_model->find($role_id);//3. 分配到模板$this->assign('role_info', $role_info);//4. 分一二級讀取auth表中的權限信息$auth_model = D('Auth');$auth_p = $auth_model->where('auth_pid=0')->select();$auth_s = $auth_model->where('auth_pid!=0')->select();$this->assign('auth_p', $auth_p);$this->assign('auth_s', $auth_s);$this->display();}}?>
?
?
?