AuthController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\controller;
  12. use app\merchant\model\merchant\Merchant;
  13. use app\merchant\model\merchant\MerchantAdmin;
  14. use app\merchant\model\merchant\MerchantMenus;
  15. use basic\AuthBasic;
  16. use service\HookService;
  17. use think\Url;
  18. /**
  19. * 基类 所有控制器继承的类
  20. * Class AuthController
  21. * @package app\merchant\controller
  22. */
  23. class AuthController extends AuthBasic
  24. {
  25. /**
  26. * 当前登陆管理员信息
  27. * @var
  28. */
  29. protected $adminInfo;
  30. /**
  31. * 当前登陆管理员ID
  32. * @var
  33. */
  34. protected $adminId;
  35. /**
  36. * 是否需要审核
  37. * @var
  38. */
  39. protected $isAudit;
  40. /**
  41. * 讲师id
  42. * @var
  43. */
  44. protected $merchantId;
  45. /**
  46. * 讲师id
  47. * @var
  48. */
  49. protected $lecturerId;
  50. /**
  51. * 商户信息
  52. * @var
  53. */
  54. protected $merchantInfo;
  55. /**
  56. * 当前管理员权限
  57. * @var array
  58. */
  59. protected $auth = [];
  60. protected $skipLogController = ['index', 'common'];
  61. protected function _initialize()
  62. {
  63. parent::_initialize();
  64. if (!MerchantAdmin::hasActiveAdmin()) return $this->redirect('Login/index');
  65. try {
  66. $adminInfo = MerchantAdmin::activeAdminInfoOrFail();
  67. $merchantInfo = MerchantAdmin::activeMerchantInfoOrFail();
  68. } catch (\Exception $e) {
  69. return $this->failed(MerchantAdmin::getErrorInfo($e->getMessage()), Url::build('Login/index'));
  70. }
  71. $this->adminInfo = $adminInfo;
  72. $this->adminId = $adminInfo['id'];
  73. $this->merchantInfo = $merchantInfo;
  74. $this->merchantId = $merchantInfo['id'];
  75. $this->lecturerId = $merchantInfo['lecturer_id'];
  76. $this->isAudit = Merchant::where('id', $merchantInfo['id'])->value('is_audit');
  77. $this->getActiveAdminInfo();
  78. $this->auth = MerchantMenus::rulesByAuth($this->adminInfo['rules']);
  79. $this->checkAuth();
  80. $this->assign('_admin', $this->adminInfo);
  81. if ($merchantInfo['is_del'] == 1 || $merchantInfo['status'] == 0) {
  82. $this->failed('讲师删除或者已被禁止登陆', Url::build('Login/index'));
  83. }
  84. }
  85. protected function checkAuth($action = null, $controller = null, $module = null, array $route = [])
  86. {
  87. static $allAuth = null;
  88. if ($allAuth === null) $allAuth = MerchantMenus::getAllAuth();
  89. if ($module === null) $module = $this->request->module();
  90. if ($controller === null) $controller = $this->request->controller();
  91. if ($action === null) $action = $this->request->action();
  92. if (!count($route)) $route = $this->request->route();
  93. if (in_array(strtolower($controller), $this->skipLogController, true)) return true;
  94. $nowAuthName = MerchantMenus::getAuthName($action, $controller, $module, $route);
  95. $baseNowAuthName = MerchantMenus::getAuthName($action, $controller, $module, []);
  96. if ((in_array($nowAuthName, $allAuth) && !in_array($nowAuthName, $this->auth)) || (in_array($baseNowAuthName, $allAuth) && !in_array($baseNowAuthName, $this->auth)))
  97. exit($this->authFail('没有权限访问!'));
  98. return true;
  99. }
  100. /**
  101. * 获得当前用户最新信息
  102. * @return MerchantAdmin
  103. */
  104. protected function getActiveAdminInfo()
  105. {
  106. $adminId = $this->adminId;
  107. $adminInfo = MerchantAdmin::getValidAdminInfoOrFail($adminId);
  108. if (!$adminInfo) $this->failed(MerchantAdmin::getErrorInfo('请登陆!'));
  109. $this->adminInfo = $adminInfo;
  110. MerchantAdmin::setLoginInfo($adminInfo->toArray());
  111. return $adminInfo;
  112. }
  113. }