class Base extends BaseController{
protected function initialize(){
$admin = Session::get('admin');
if(!$admin){
if(Request::isAjax()){
exit(json_encode(array('code'=>1,'msg'=>'您还未登录,请先登录')));
}
exit('<div style="text-align:center;color:#333;font-size:18px;">您还未登录,请先登录</div><script>setTimeout(function(){window.location.href="/admins/account/login"},2000);</script>');
}
$randstr = Cookie::get('randstr');
$cache_randstr = Cache::get('uid_'.$admin['id']);
if($randstr!=$cache_randstr){
Session::delete('admin');
if(Request::isAjax()){
exit(json_encode(array('code'=>1,'msg'=>'您的帐号在其他地方登录,请重新登录')));
}
exit('<div style="text-align:center;color:#333;font-size:18px;">您的帐号在其他地方登录,请重新登录</div><script>setTimeout(function(){window.location.href="/admins/account/login"},2000);</script>');
}
$controller = Request::controller();
$action = Request::action();
$curMenu = Db::table('admin_menu')->where('controller',$controller)->where('action',$action)->find();
if(!$curMenu){
$this->_noaccess('该功能不存在');
}
if($curMenu['status']==1){
$this->_noaccess('该功能已被禁用,请联系管理员');
}
$mygroup = Db::table('admin_group')->where('id',$admin['gid'])->find();
if(!$mygroup){
$this->_noaccess('该角色不存在,请联系管理员');
}
$mygroup['rights'] = json_decode($mygroup['rights'],true);
echo '<pre>';
print_r($mygroup['rights']);
if(!in_array($curMenu['mid'],$mygroup['rights'])){
$this->_noaccess('没有权限,请联系管理员');
}
$this->admin = $admin;
$this->mygroup = $mygroup;
}
private function _noaccess($msg){
if(Request::isAjax()){
exit(json_encode(array('code'=>1,'msg'=>$msg)));
}else{
exit('<div style="text-align:center;color:#333;font-size:18px;">'.$msg.'</div>');
}
}
注意mid 就是 right里的数据 admin_group 与admin进行关联 admin_group.id =admin.gid
$mygroup = Db::table('admin_group')->where('id',$admin['gid'])->find();
menu表与admin_group 进行关联 查看mid是否在rights数组里
if(!in_array($curMenu['mid'],$mygroup['rights'])){
$this->_noaccess('没有权限,请联系管理员');
}
|