AuthController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\system\SystemAdmin;
  4. use app\admin\model\system\SystemMenus;
  5. use app\admin\model\system\SystemRole;
  6. use behavior\admin\SystemBehavior;
  7. use service\HookService;
  8. use think\Url;
  9. /**
  10. * 基类 所有控制器继承的类
  11. * Class AuthController
  12. * @package app\admin\controller
  13. */
  14. class AuthController extends SystemBasic
  15. {
  16. /**
  17. * 当前登陆管理员信息
  18. * @var
  19. */
  20. protected $adminInfo;
  21. /**
  22. * 当前登陆管理员ID
  23. * @var
  24. */
  25. protected $adminId;
  26. /**
  27. * 当前管理员权限
  28. * @var array
  29. */
  30. protected $auth = [];
  31. protected $skipLogController = ['index','common'];
  32. protected function _initialize()
  33. {
  34. parent::_initialize();
  35. if(!SystemAdmin::hasActiveAdmin()) return $this->redirect('Login/index');
  36. try{
  37. $adminInfo = SystemAdmin::activeAdminInfoOrFail();
  38. }catch (\Exception $e){
  39. return $this->failed(SystemAdmin::getErrorInfo($e->getMessage()),Url::build('Login/index'));
  40. }
  41. $this->adminInfo = $adminInfo;
  42. $this->adminId = $adminInfo['id'];
  43. $this->getActiveAdminInfo();
  44. $this->auth = SystemAdmin::activeAdminAuthOrFail();
  45. $this->adminInfo->level === 0 || $this->checkAuth();
  46. $this->assign('_admin',$this->adminInfo);
  47. HookService::listen('admin_visit',$this->adminInfo,'system',false,SystemBehavior::class);
  48. }
  49. protected function checkAuth($action = null,$controller = null,$module = null,array $route = [])
  50. {
  51. static $allAuth = null;
  52. if($allAuth === null) $allAuth = SystemRole::getAllAuth();
  53. if($module === null) $module = $this->request->module();
  54. if($controller === null) $controller = $this->request->controller();
  55. if($action === null) $action = $this->request->action();
  56. if(!count($route)) $route = $this->request->route();
  57. if(in_array(strtolower($controller),$this->skipLogController,true)) return true;
  58. $nowAuthName = SystemMenus::getAuthName($action,$controller,$module,$route);
  59. $baseNowAuthName = SystemMenus::getAuthName($action,$controller,$module,[]);
  60. if((in_array($nowAuthName,$allAuth) && !in_array($nowAuthName,$this->auth)) || (in_array($baseNowAuthName,$allAuth) && !in_array($baseNowAuthName,$this->auth)))
  61. exit($this->failed('没有权限访问!'));
  62. return true;
  63. }
  64. /**
  65. * 获得当前用户最新信息
  66. * @return SystemAdmin
  67. */
  68. protected function getActiveAdminInfo()
  69. {
  70. $adminId = $this->adminId;
  71. $adminInfo = SystemAdmin::getValidAdminInfoOrFail($adminId);
  72. if(!$adminInfo) $this->failed(SystemAdmin::getErrorInfo('请登陆!'));
  73. $this->adminInfo = $adminInfo;
  74. SystemAdmin::setLoginInfo($adminInfo);
  75. return $adminInfo;
  76. }
  77. }