Common.php 通用函数类库
/**
* 检测用户id
* @param name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
* @param uid int 认证用户的id
*/
function sp_auth_check($uid, $name=null)
{
if(empty($uid)) return false;
if(empty($name)){
$name=strtolower(MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME);
}
$iAuth_obj = new appCommonLibiAuth();
return $iAuth_obj->check($uid);
}
AdminbaseController.php 后台管理的父控制器类
class AdminbaseController extends Controller
{
public $uid = 0;
//用户实例
public $userObj = null;
/**
* 构造函数
* Adminbase constructor.
*/
public function __construct()
{
parent::__construct();
}
public function _initialize()
{
$this->uid = Session::read('AdminId');
if(!empty($this->uid ))
{
//检测过已经登录了
$this->userObj = Db::name('users')->where('uid',$this->uid)->find();
if(!$this->check_access($this->uid))
{
$this->error("您没有访问权限!",Url::build('admin/index/login'));
exit();
}
$this->assign('admin',$this->userObj);
}
else
{
//没有登录的
$this->error("您还没有登录!",Url::build('admin/index/login'));
exit();
}
}
/**
* 检测权限
* @param $uid
*/
private function check_access(&$uid)
{
if($uid == 1)
{
//超级管理员
return true;
}
$request = Request::instance();
//如果不是这个应用池的账户也不通过
$pools = explode(',',$this->userObj['pools']);
if(!in_array(strtolower($request->module()), $pools)) return false;
$rule = $request->module() . '_' . $request->controller() . '_' . $request->action() ;
$no_need_check_rules = Config::get('inc_auth.no_need_check_rules');
if(!in_array(strtolower($rule),$no_need_check_rules))
{
//验证权限
return sp_auth_check($uid);
}
else
{
return true;
}
}
}
inc_auth.php 认证配置文件
$config['no_need_check_rules'] = array('admin_index_index','admin_index_login');
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。







