AuthController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\model\system\SystemAdmin;
  13. use app\admin\model\system\SystemMenus;
  14. use app\admin\model\system\SystemRole;
  15. use basic\AuthBasic;
  16. use basic\SystemBasic;
  17. use behavior\system\SystemBehavior;
  18. use service\HookService;
  19. use think\Session;
  20. use think\Url;
  21. /**
  22. * 基类 所有控制器继承的类
  23. * Class AuthController
  24. * @package app\admin\controller
  25. */
  26. class AuthController extends AuthBasic
  27. {
  28. /**
  29. * 当前登陆管理员信息
  30. * @var
  31. */
  32. protected $adminInfo;
  33. /**
  34. * 当前登陆管理员ID
  35. * @var
  36. */
  37. protected $adminId;
  38. /**
  39. * 当前管理员权限
  40. * @var array
  41. */
  42. protected $auth = [];
  43. protected $skipLogController = ['index', 'common'];
  44. protected function _initialize()
  45. {
  46. parent::_initialize();
  47. if (!SystemAdmin::hasActiveAdmin()) return $this->redirect('Login/index');
  48. try {
  49. $adminInfo = SystemAdmin::activeAdminInfoOrFail();
  50. } catch (\Exception $e) {
  51. return $this->failed(SystemAdmin::getErrorInfo($e->getMessage()), Url::build('Login/index'));
  52. }
  53. $this->adminInfo = $adminInfo;
  54. $this->adminId = $adminInfo['id'];
  55. $this->getActiveAdminInfo();
  56. $this->auth = SystemAdmin::activeAdminAuthOrFail();
  57. $this->adminInfo->level === 0 || $this->checkAuth();
  58. $this->assign('_admin', $this->adminInfo);
  59. HookService::listen('admin_visit', $this->adminInfo, 'system', false, SystemBehavior::class);
  60. }
  61. protected function checkAuth($action = null, $controller = null, $module = null, array $route = [])
  62. {
  63. static $allAuth = null;
  64. if ($allAuth === null) $allAuth = SystemRole::getAllAuth();
  65. if ($module === null) $module = $this->request->module();
  66. if ($controller === null) $controller = $this->request->controller();
  67. if ($action === null) $action = $this->request->action();
  68. if (!count($route)) $route = $this->request->route();
  69. if (in_array(strtolower($controller), $this->skipLogController, true)) return true;
  70. $nowAuthName = SystemMenus::getAuthName($action, $controller, $module, $route);
  71. $baseNowAuthName = SystemMenus::getAuthName($action, $controller, $module, []);
  72. if ((in_array($nowAuthName, $allAuth) && !in_array($nowAuthName, $this->auth)) || (in_array($baseNowAuthName, $allAuth) && !in_array($baseNowAuthName, $this->auth)))
  73. exit($this->failed('没有权限访问!'));
  74. return true;
  75. }
  76. /**
  77. * 获得当前用户最新信息
  78. * @return SystemAdmin
  79. */
  80. protected function getActiveAdminInfo()
  81. {
  82. $adminId = $this->adminId;
  83. $adminInfo = SystemAdmin::getValidAdminInfoOrFail($adminId);
  84. if (!$adminInfo) $this->failed(SystemAdmin::getErrorInfo('请登陆!'));
  85. $this->adminInfo = $adminInfo;
  86. SystemAdmin::setLoginInfo($adminInfo);
  87. return $adminInfo;
  88. }
  89. }