AuthController.php 3.0 KB

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