123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * +----------------------------------------------------------------------
- * | 后台中间件
- * +----------------------------------------------------------------------
- */
- namespace app\sys\middleware;
- use think\facade\Config;
- use think\facade\Session;
- use think\Response;
- use think\exception\HttpResponseException;
- use think\facade\Request;
- use think\model\Collection;
- class Admin
- {
- public function handle($request, \Closure $next)
- {
- // 获取当前用户
- $admin_id = Session::get('adminuser.userid');
- if (empty($admin_id)) {
- return redirect((string)url('login/index'));
- }
- // 查找当前控制器和方法,控制器首字母大写,方法名首字母小写 如:Index/index
- $route = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', Request::controller())) . '/' . strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', Request::action()));
- // 权限认证
- if (!$this->checkAuth($route, Session::get('adminuser.roleid'))) {
- $this->error('您无此操作权限!');
- }
- // 进行操作日志的记录
- $this->syslogRecord($route);
- // 中间件handle方法的返回值必须是一个Response对象。
- return $next($request);
- }
- protected function syslogRecord($route = '')
- {
- // 定义方法白名单(不记录日志)
- $allow = [
-
- ];
-
- $action = Request::action();
- if ($action != 'index' && !in_array($route, $allow)) {
- \app\common\model\SysLog::record();
- }
- }
- /**
- * 检查权限
- * @param string|array $route 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
- * @param integer $rid 认证用户角色ID
- * @return boolean 通过验证返回true;失败返回false
- */
- public function checkAuth($route, $rid)
- {
- // 超级管理员不检查权限
- if ($rid==1) {
- return true;
- }
- $menus = \app\common\model\SysMenu::getUserMenuList($rid);
- if (!Config::get('app.auth_on')) {
- return true;
- }
- // 定义方法白名单
- $allow = [
- 'index/index', // 首页
- 'index/usedspace', // 使用空间
- 'index/clearcache', // 清除缓存
- 'file_manager/uploadimg', // 图片上传
- ];
- // 查询所有不验证的方法并放入白名单
- $menuOpen = \app\common\model\SysMenu::where('open', 1)->column('url');
-
- $allow = array_merge($allow, $menuOpen);
- foreach ($menus as $value) {
- if ($value->type == 0) {
- continue;
- }
- $allow[] = $value->url;
- }
- $allow = array_unique($allow);
- if (in_array($route, $allow)) {
- return true;
- }
- return false;
- }
- /**
- * 操作错误跳转的快捷方法 抽的 liliuwei Jump error 方法
- * @access protected
- * @param mixed $msg 提示信息
- * @return void
- */
- protected function error($msg = '')
- {
- $url = Request::isAjax() ? '' : 'javascript:history.back(-1);';
- $result = [
- 'code' => 0,
- 'msg' => $msg,
- 'data' => '',
- 'url' => $url,
- 'wait' => 3,
- ];
- $type = Request::isJson() || Request::isAjax() ? 'json' : 'html';;
- if ('html' == strtolower($type)) {
- $type = 'view';
- $dispatch_error_tmpl = app()->getRootPath().'/vendor/liliuwei/thinkphp-jump/src/tpl/dispatch_jump.tpl';
- $response = Response::create($dispatch_error_tmpl, $type)->assign($result)->header([]);
- } else {
- $response = Response::create($result, $type)->header([]);
- }
- throw new HttpResponseException($response);
- }
- }
|