312 lines
12 KiB
PHP
312 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: linfan
|
|
* Date: 2018/11/7
|
|
* Time: 14:10
|
|
*/
|
|
if (!defined('BASEPATH')) exit('No direct script access allowed');
|
|
//session_start();
|
|
|
|
class Auth {
|
|
|
|
public $uid, $username, $role;
|
|
|
|
public function index()
|
|
{
|
|
$CI = &get_instance();
|
|
|
|
$cookie = $CI->input->cookie(LOGIN_COOKIE);
|
|
$CI->load->library('encryption');
|
|
$user_info = $CI->encryption->decrypt($cookie);
|
|
|
|
$CI->load->helper('sys');
|
|
$dir = $CI->router->fetch_directory() ? $CI->router->fetch_directory() : '';
|
|
$ctrl = $CI->router->fetch_class();
|
|
$mth = $CI->router->fetch_method();
|
|
$methods = explode('_', $mth);
|
|
$action = action_node($methods[0]);
|
|
$node = DIRECTORY_SEPARATOR.$dir.$ctrl;
|
|
$auth_match = array(1 => 'index', 2 => 'lists', 3 => 'get', 4 => 'add', 5 => 'edit', 6 => 'del', 7 => 'batch', 8 => 'export');
|
|
$CI->load->model('sys/sys_admin_model');
|
|
$CI->load->model('sys/sys_menu_model');
|
|
$CI->load->model('sys/sys_role_model');
|
|
$CI->load->model('sys/sys_city_model');
|
|
$CI->load->model('Area_model');
|
|
|
|
if($user_info = json_decode($user_info, true))
|
|
{
|
|
//登录ip判断
|
|
$admin_user = $CI->sys_admin_model->get(array('id' => $user_info['id']));
|
|
$ip = get_client_ip();
|
|
$ip_arr = array();
|
|
if(SUPER_ADMIN == $admin_user['role_id']){
|
|
//超级管理员免检
|
|
} else if($this->is_ignore($user_info['id'])){
|
|
//免检
|
|
} elseif(false !== strpos($_SERVER['HTTP_HOST'], "admin.dev.liche.cn") || false !== strpos($_SERVER['HTTP_HOST'], "admin.lc.haodian.cn")){
|
|
//开发测试免检
|
|
} elseif(filter_var($ip, FILTER_VALIDATE_IP) && $ip != $admin_user['login_ip']) {
|
|
// $CI->load->model('sys/sys_config_model');
|
|
// $config_ip = $CI->sys_config_model->select(array("v LIKE '%\"status\":\"1\"%'" => NULL,"k" => "site"));
|
|
// $ip_arr = array();
|
|
// foreach ($config_ip as $key => $value)
|
|
// {
|
|
// $ip_arr[] = json_decode($value['v'])->ip;
|
|
// }
|
|
// $ip_arr = array_merge($ip_arr, array_column($CI->sys_admin_model->select(array(),'','','','login_ip'), 'login_ip'));
|
|
// if(!in_array($ip, $ip_arr) && $dir)
|
|
// {
|
|
// if($admin_user['id'] > 10){//管理员id>10去掉验证码登录
|
|
// $admin_info = array(
|
|
// 'id' => $admin_user['id'],
|
|
// );
|
|
// $domain = explode('.', $_SERVER['HTTP_HOST']);
|
|
// array_shift($domain);
|
|
// $domain = implode('.', $domain);
|
|
// $CI->input->set_cookie(LOGIN_COOKIE, $CI->encryption->encrypt(json_encode($admin_info)), time() + 86400, $domain);
|
|
// $this->returnMsg('访问IP发生变化,请重新登录', '/login/check_view');
|
|
// }
|
|
// }
|
|
}
|
|
|
|
//权限分类筛选
|
|
$info = (array)json_decode($admin_user['other_json']);
|
|
if($admin_user['role_id'] == SUPER_ADMIN) {
|
|
$_SESSION['admin_role_id'] = '0';
|
|
} else {
|
|
if($info['city_id']) {
|
|
$province = $CI->Area_model->select(['city_id in ( '.implode(',', $info['city_id']).')' => null]);
|
|
$city = array_column($CI->sys_city_model->select(['status' => '0']), 'city_id');
|
|
foreach ($info['city_id'] as $key => $value) {
|
|
if(in_array($value, $city)) {
|
|
unset($info['city_id'][$key]);
|
|
}
|
|
}
|
|
$info['province_id'] = array_column($province, 'province_id');
|
|
$info['province_name'] = array_column($province, 'province_name');
|
|
}
|
|
$_SESSION['admin_role_id'] = $admin_user['role_id'];
|
|
$_SESSION['admin_info'] = $info;
|
|
}
|
|
|
|
$spm = $CI->input->get('spm');
|
|
//获取当前请求最符合的菜单节点
|
|
// $menus = $CI->sys_menu_model->select(array("url like '" . $node . "%'" => null, 'status' => 1), "id ASC");
|
|
// $menu = array();
|
|
// foreach($menus as $item){
|
|
// if(!$menu){
|
|
// $menu = $item;
|
|
// continue;
|
|
// }
|
|
// //节点更匹配的菜单
|
|
// $url_arr = explode('?', $item['url']);
|
|
// $url_arr2 = explode('?', $menu['url']);
|
|
// $url = $node . "/{$mth}";
|
|
//
|
|
// $p1 = strpos($url, $url_arr[0]);
|
|
// $p2 = strpos($url, $url_arr2[0]);
|
|
// //url匹配到method的优先级高
|
|
// if(0 === $p1 && 0 !== $p2){
|
|
// $menu = $item;
|
|
// continue;
|
|
// } elseif(0 !== $p1 && 0 === $p2) {
|
|
// continue;
|
|
// }
|
|
// if('index' == $mth){
|
|
// //访问index方法,有可能url里只到ct一级
|
|
// if((0 === $p1 || $node == $url_arr[0]) && (0 !== $p2 && $node != $p2)){
|
|
// $menu = $item;
|
|
// continue;
|
|
// } elseif(0 !== $p1 && $node != $url_arr[0] && (0 === $p2 || $node == $p2)) {
|
|
// continue;
|
|
// }
|
|
// } else {//非index,判断ct是否匹配
|
|
// $ct_arr1 = array_slice(explode('/', $url_arr[0]), -2);
|
|
// $ct_arr2 = array_slice(explode('/', $url_arr2[0]), -2);
|
|
// if(!in_array($ctrl, $ct_arr1)){
|
|
// continue;
|
|
// } elseif(!in_array($ctrl, $ct_arr2)){
|
|
// $menu = $item;
|
|
// continue;
|
|
// }
|
|
// }
|
|
//
|
|
// //匹配节点更高的项
|
|
// if(($spm && 0 !== strpos($menu['node'], $spm)) || strlen($item['node']) > strlen($menu['node'])){
|
|
// $menu = $item;
|
|
// continue;
|
|
// }
|
|
// }
|
|
|
|
$role = $CI->sys_role_model->get(array('id' => $admin_user['role_id']));
|
|
$action_json = $role['action_json'] ? json_decode($role['action_json'], true) : array();
|
|
$menus = $CI->sys_menu_model->select(array("url like '" . $node . "%'" => null, 'status' => 1), "id ASC");
|
|
foreach($menus as $item) {
|
|
if (isset($action_json[$item['id']])) {
|
|
$menu = $item;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if($admin_user['role_id'] != SUPER_ADMIN && $dir)
|
|
{
|
|
if(!$admin_user['status'])
|
|
{
|
|
return $this->returnMsg('您的账号已被禁用');
|
|
}
|
|
else
|
|
{
|
|
$menu_ids = explode(',', $role['menu_ids']);
|
|
|
|
if(!$role['status'])
|
|
{
|
|
return $this->returnMsg('您的权限已被禁用');
|
|
} elseif('json' == $methods[0]){
|
|
//弹框等公共搜索使用,无需菜单权限校验
|
|
}
|
|
else
|
|
{
|
|
/*edit by xuxianbin 20190521 同一个url有可能对应多个节点*/
|
|
// $menu = $CI->sys_menu_model->get(array("url like '" . $node . "%'" => null, 'status' => 1));
|
|
if(!$action_json[$menu['id']])
|
|
{
|
|
return $this->returnMsg('系统菜单尚未创建');
|
|
}
|
|
|
|
if(!in_array($action, $action_json[$menu['id']]))
|
|
{
|
|
return $this->returnMsg('您的权限不足');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if($spm)
|
|
{
|
|
$spm_arr = explode('-', $spm);
|
|
if(!$menu_ids)
|
|
{
|
|
$role = $CI->sys_role_model->get(array('id' => $admin_user['role_id']));
|
|
$menu_ids = explode(',', $role['menu_ids']);
|
|
}
|
|
|
|
if(count($spm_arr) >= 5)
|
|
{
|
|
$four = $CI->sys_menu_model->select(array('pid' => $spm_arr[3], 'status' => 1), 'sort desc');
|
|
if($four)
|
|
{
|
|
$arr = $spm_arr;
|
|
|
|
foreach ($four as $v)
|
|
{
|
|
if($admin_user['role_id'] != SUPER_ADMIN && !in_array($v['id'], $menu_ids))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
$active = $v['id'] == $spm_arr[4] ? true : false;
|
|
$arr[4] = $v['id'];
|
|
|
|
$CI->data['snavs'][] = array(
|
|
'active' => $active,
|
|
'spm' => implode('-', $arr),
|
|
'url' => $v['url'],
|
|
'name' => $v['name'],
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
//获取面包屑
|
|
$spm_arr1 = explode('-', $menu['node']);
|
|
$spm_arr1 && !is_numeric($spm_arr1[0]) && $spm_arr1 = array_slice($spm_arr1, 1);
|
|
if($spm_arr1){
|
|
$rows = $CI->sys_menu_model->select(array("id in (" . implode(",",$spm_arr1) . ")" => null, 'status' => 1), 'sort desc');
|
|
$menu_arr = array();
|
|
$breads = array();
|
|
foreach($rows as $row){
|
|
$menu_arr[$row['id']] = $row;
|
|
}
|
|
foreach($spm_arr1 as $v){
|
|
$item = $menu_arr[$v];
|
|
$breads[] = array(
|
|
'spm' => $item['node'],
|
|
'url' => '#' == $item['url'] ? '' : $item['url'],
|
|
'name' => $item['name'],
|
|
);
|
|
}
|
|
|
|
$CI->data['breads'] = $breads;
|
|
}
|
|
|
|
}
|
|
|
|
$CI->uid = $admin_user ? $admin_user['id'] : 0;
|
|
$CI->username = $admin_user ? $admin_user['username'] : '';
|
|
$CI->role = $admin_user ? $admin_user['role_id'] : 0;
|
|
}
|
|
elseif($ctrl != 'login')
|
|
{
|
|
header('location:/login');
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 初始化app参数
|
|
*/
|
|
public function initapp(){
|
|
$Input = &load_class("Input", "core");
|
|
$Config = &load_class('Config', 'core');
|
|
$key_cookie = "hd_app_id";
|
|
$app_id = $Input->get_post("app_id");
|
|
if(!$app_id){
|
|
$app_id = $Input->cookie($key_cookie);
|
|
}
|
|
|
|
if($app_id){
|
|
$Input->set_cookie($key_cookie, $app_id, time() + 86400);
|
|
|
|
$Config->load('app', true, true);
|
|
$configs = $Config->item('app');
|
|
$config = $configs[$app_id];
|
|
//根据应用加载库
|
|
if($config['db']){
|
|
$GLOBALS['app_db'] = $config['db'];
|
|
}
|
|
}
|
|
}
|
|
|
|
private function returnMsg($msg = '网络错误', $url = '')
|
|
{
|
|
header('Content-Type:application/json; charset=utf-8');
|
|
echo json_encode(array('data' => $this->data, 'code' => 0, 'msg' => $msg, 'url' => $url, 'wait' => 2000), JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* 判断管理员是否免检
|
|
* @param $admin_id
|
|
* @return bool
|
|
*/
|
|
private function is_ignore($admin_id){
|
|
$CI = &get_instance();
|
|
$CI->load->model('sys/sys_config_model', 'config_model');
|
|
|
|
$where = array('k' => 'igadmin', 'v LIKE \'%"admin_id":"' .$admin_id. '"%\'' => null);
|
|
$exist = $CI->config_model->get($where);
|
|
if($exist){
|
|
$json = json_decode($exist['v'], true);
|
|
if($json['status']){
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|