AuthController.php 2.9 KB

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