/** * 读取指定的单一文件 * * @param string $file Loader::parseFileRealPath() * @param bool $require_file 是否返回文件 等于false时,返回文件文本内容 * @return mixed * @throws CoreException */ public static function read($file, $require_file = true) { if (is_file($file)) { $file_path = $file; } else { $file_path = Loader::getFilePath($file); } $read_file_flag = (int) $require_file; if (isset(self::$loaded[$file_path][$read_file_flag])) { return self::$loaded[$file_path][$read_file_flag]; } if (is_readable($file_path)) { if (false === $require_file) { $file_content = file_get_contents($file_path); self::$loaded[$file_path][$read_file_flag] = $file_content; return $file_content; } $ext = Helper::getExt($file_path); switch ($ext) { case 'php': $data = (require $file_path); self::$loaded[$file_path][$read_file_flag] = $data; break; case 'json': $data = json_decode(file_get_contents($file_path), true); self::$loaded[$file_path][$read_file_flag] = $data; break; case 'ini': $data = parse_ini_file($file_path, true); self::$loaded[$file_path][$read_file_flag] = $data; break; default: throw new CoreException('不支持的解析格式'); } return $data; } else { throw new CoreException("读取文件失败:{$file}"); } }
/** * 扫描控制器文件 * * @param bool $hashMap * @return array */ private function scanControllers($hashMap = false) { $controller_file = Loader::getFilePath('app::controllers'); $nav_data = array(); foreach (glob(rtrim($controller_file, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*.php') as $f) { $fi = pathinfo($f); $class_name = $fi['filename']; $ori_nav_data = array('name' => lcfirst($class_name), 'link' => lcfirst($class_name), 'status' => 1); if ($hashMap) { $nav_data[$ori_nav_data['link']] = $ori_nav_data; } else { $nav_data[] = $ori_nav_data; } } return $nav_data; }
function __construct() { parent::__construct(); $this->u = $_SESSION['u']; $this->ACL = new AclModule(); $this->ADMIN = new AdminModule(); /** * 查询登录用户信息 */ $user_info = $this->ADMIN->getAdminInfo(array('name' => $this->u)); $role_id = $user_info['rid']; /** * 导航菜单 */ $nav_menu_data = $this->ACL->getMenu(); $controller = lcfirst($this->controller); /** * 菜单icon */ $icon = Loader::read(Loader::getFilePath('::config/menu_icon.config.php')); $tpl_dir_name = $this->config->get('sys', 'default_tpl_dir'); $icon_config = array(); if (isset($icon[$tpl_dir_name])) { $icon_config = $icon[$tpl_dir_name]; } /** * 判断是否是超级管理员 */ if ($role_id == 0) { /** * 设置view导航数据 */ $this->view->setNavMenu($nav_menu_data); $all_menu = $this->ACL->getNavChildMenu($nav_menu_data); $child_menu = array(); if (isset($nav_menu_data[$controller])) { $child_menu = $all_menu[$controller]['child_menu']; } $this->view->setMenu($child_menu); $this->view->setAllMenu($all_menu, $icon_config); } else { /** * 查询所属管理角色 */ $role_info = $this->ACL->getRoleInfo(array('id' => $role_id)); /** * 角色允许的方法 */ $accept_behavior = explode(',', $role_info['behavior']); /** * 只保留允许访问的菜单 */ foreach ($nav_menu_data as $k => $nav) { if (!in_array($nav['id'], $accept_behavior)) { unset($nav_menu_data[$k]); } } /** * 设置view导航数据 */ $this->view->setNavMenu($nav_menu_data); $all_menu = $this->ACL->getNavChildMenu($nav_menu_data); $this->view->setAllMenu($all_menu, $icon_config); $child_menu = array(); if (isset($nav_menu_data[$controller])) { $child_menu = $all_menu[$controller]['child_menu']; } else { //如果没有访问权限 使用有权限的第一个菜单 $accept_menus = array_keys($nav_menu_data); if (!empty($accept_menus)) { $this->to($accept_menus[0]); } $this->to(); } $accept_action = array(); foreach ($child_menu as $c_key => $c_value) { //过滤无权限的菜单 if (!in_array($c_value['id'], $accept_behavior)) { unset($child_menu[$c_key]); } else { $accept_action[] = $c_value['link']; } } $this->view->setMenu($child_menu); //都没有权限执行action时,跳转到第一个有权限的action if (!in_array($this->action, $accept_action)) { if ($this->is_ajax_request()) { $this->dieJson($this->getStatus(100030)); } else { $this->view->notice(100030); exit(0); } } } }