qc.ifish7.com/Application/Admin/Controller/AdminController.class.php

189 lines
6.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller {
/**
* 后台控制器初始化
*/
protected function _initialize(){
//auth验证
define('UID',is_login());
if( !UID ){// 还没登录 跳转到登录页面
//如果是ajax请求则用error方法输入提示(error方法可以识别是否是ajax)
if(IS_AJAX){
$this->error('登陆验证信息不存在,请重新登陆',U('Public/login'));
}else{
$this->redirect(U('Public/login'));
}
}
//多语言
if(C("LANG_SWITCH_ON")){
$this->LANG_OPT=C("LANG_OPT");
$this->admin_lang= session("admin_lang");
}
//是否是超级管理员
define('IS_ROOT', is_administrator());
if(!IS_ROOT && C('ADMIN_ALLOW_IP')){
// 检查IP地址访问
if(!in_array(get_client_ip(),explode(',',C('ADMIN_ALLOW_IP')))){
$this->error('403:禁止访问');
}
}
// 检测访问权限
$access = $this->accessControl();
//检测非动态权限
$rule = strtolower(CONTROLLER_NAME.'/'.ACTION_NAME);
//根据用户查询节点
$role_ids=M("role_user")->where(array("user_id"=>UID))->getField("role_id",true);
$node_rs=array();
if($role_ids){
$node_ids=M("access")->join("left join __ROLE__ on __ACCESS__.role_id = __ROLE__.id")->where(array("status"=>1,"role_id"=>array("in",$role_ids)))->getField("node_id",true);
if($node_ids){
$node_rs=M("node")->where(array("id"=>array("in",$node_ids)))->order("pid desc")->select();
}
}
C('ROLE_NODE_RS',$node_rs);
//
if ( $access === false ) {
$this->error('403:禁止访问');
}elseif( $access === null ){
$dynamic = $this->checkDynamic();//检测分类栏目有关的各项动态权限
if( $dynamic === null ){
if ( !$this->checkRule($rule,1)){
$this->error('未授权访问!');
}
}elseif( $dynamic === false ){
$this->error('未授权访问!');
}
}
//左侧菜单
$this->assign('__MENU__', $this->getMenus());
//页面标题
$path=CONTROLLER_NAME."/".ACTION_NAME;
$pagetitle=M("node")->where(array("name"=>$path))->order("pid desc")->getField("title");
$this->assign("pagetitle",$pagetitle);
//模型循环
$where=array();
$where["pid"]=0;
$where["status"]=1;
if($this->admin_lang){
$where["lang"]=$this->admin_lang;
}
//
$catData=M("cat")->where($where)->order("sort asc,catid desc")->select();
$role=session("role");
$this->role=$role;
foreach($catData as $v){
if(strstr($role[info_view],"|".$v[catid]."|")||IS_ROOT){
$modeldate[]=$v;
}
}
$this->modeldate=$modeldate;
//记录一级栏目session便于栏目高亮
if($_GET[catid]&&$GLOBALS[cat][$_GET[catid]][pid]==0){
session("currentCatid",$_GET[catid]);
}
$this->currentCatid=session("currentCatid");
}
/**
* 权限检测
* @param string $rule 检测的规则
* @param string $mode check模式
* @return boolean
* @author 朱亚杰 <xcoolcc@gmail.com>
*/
final protected function checkRule($rule){
if(IS_ROOT){
return true;//管理员允许访问任何页面
}
$rule=strtolower($rule);
//这里只针对 已经添加过节点的控制器做验证未增加到node表中的不做验证
$node=D('node')->field('name')->select();
//转小写
$s_node=array();
foreach($node as $v){
$s_node[]=strtolower($v[name]);
}
if(!in_array($rule,$s_node))
{
return true;
}
//对角色节点做验证
foreach(C('ROLE_NODE_RS') as $v){
$name=strtolower($v[name]);
if($name==$rule){
return true;
}
}
return false;
}
/**
* 检测是否是需要动态判断的权限
* @return boolean|null
* 返回true则表示当前访问有权限
* 返回false则表示当前访问无权限
* 返回null则会进入checkRule根据节点授权判断权限
*
* @author 朱亚杰 <xcoolcc@gmail.com>
*/
protected function checkDynamic(){
if(IS_ROOT){
return true;//管理员允许访问任何页面
}
return null;//不明,需checkRule
}
/**
* action访问控制,在 **登陆成功** 后执行的第一项权限检测任务
*
* @return boolean|null 返回值必须使用 `===` 进行判断
*
* 返回 **false**, 不允许任何人访问(超管除外)
* 返回 **true**, 允许任何管理员访问,无需执行节点权限检测
* 返回 **null**, 需要继续执行节点权限检测决定是否允许访问
* @author 朱亚杰 <xcoolcc@gmail.com>
*/
final protected function accessControl(){
if(IS_ROOT){
return true;//管理员允许访问任何页面
}
$allow = C('ALLOW_VISIT');
$deny = C('DENY_VISIT');
$check = strtolower(CONTROLLER_NAME.'/'.ACTION_NAME);
if ( !empty($deny) && in_array_case($check,$deny) ) {
return false;//非超管禁止访问deny中的方法
}
if ( !empty($allow) && in_array_case($check,$allow) ) {
return true;
}
return null;//需要检测节点权限
}
//菜单
public function getMenus(){
$field=array("id","name","title","pid","status","icon");
$and=array();
$and["is_hide"]=0;
if(!APP_DEBUG){
$and["is_dev"]=0;
}
$data=M("node")->field($field)->where($and)->order('sort asc')->select();
//检测权限
foreach($data as $v){
if (!$this->checkRule($v['name'])){
continue;
}else{
$result[]=$v;
}
}
//
$data=node_merge($result);
return $data;
}
}