AuthController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  25. * 当前管理员权限
  26. * @var array
  27. */
  28. protected $auth = [];
  29. protected $skipLogController = ['index', 'common'];
  30. protected function initialize()
  31. {
  32. parent::initialize();
  33. if (!SystemAdmin::hasActiveAdmin()) return $this->redirect(Url::buildUrl('login/index')->suffix(false)->build());
  34. try {
  35. $adminInfo = SystemAdmin::activeAdminInfoOrFail();
  36. } catch (\Exception $e) {
  37. return $this->failed(SystemAdmin::getErrorInfo($e->getMessage()), Url::buildUrl('login/index')->suffix(false)->build());
  38. }
  39. $this->adminInfo = $adminInfo;
  40. $this->adminId = $adminInfo['id'];
  41. $this->getActiveAdminInfo();
  42. $this->auth = SystemAdmin::activeAdminAuthOrFail();
  43. $this->adminInfo->level === 0 || $this->checkAuth();
  44. $this->assign('_admin', $this->adminInfo);
  45. $type = 'system';
  46. event('AdminVisit', [$this->adminInfo, $type]);
  47. }
  48. protected function checkAuth($action = null, $controller = null, $module = null, array $route = [])
  49. {
  50. static $allAuth = null;
  51. if ($allAuth === null) $allAuth = SystemRole::getAllAuth();
  52. if ($module === null) $module = app('http')->getName();
  53. if ($controller === null) $controller = $this->request->controller();
  54. if ($action === null) $action = $this->request->action();
  55. if (!count($route)) $route = $this->request->route();
  56. if (in_array(strtolower($controller), $this->skipLogController, true)) return true;
  57. $nowAuthName = SystemMenus::getAuthName($action, $controller, $module, $route);
  58. $baseNowAuthName = SystemMenus::getAuthName($action, $controller, $module, []);
  59. if ((in_array($nowAuthName, $allAuth) && !in_array($nowAuthName, $this->auth)) || (in_array($baseNowAuthName, $allAuth) && !in_array($baseNowAuthName, $this->auth)))
  60. exit($this->failed('没有权限访问!'));
  61. return true;
  62. }
  63. /**
  64. * 获得当前用户最新信息
  65. * @return SystemAdmin
  66. */
  67. protected function getActiveAdminInfo()
  68. {
  69. $adminId = $this->adminId;
  70. $adminInfo = SystemAdmin::getValidAdminInfoOrFail($adminId);
  71. if (!$adminInfo) $this->failed(SystemAdmin::getErrorInfo('请登陆!'));
  72. $this->adminInfo = $adminInfo;
  73. SystemAdmin::setLoginInfo($adminInfo);
  74. return $adminInfo;
  75. }
  76. }