AuthController.php 3.1 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 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. array_shift($route);
  57. if (in_array(strtolower($controller), $this->skipLogController, true)) return true;
  58. $nowAuthName = SystemMenus::getAuthName($action, $controller, $module, $route);
  59. $baseNowAuthName = SystemMenus::getAuthName($action, $controller, $module, []);
  60. //积分设置的父类 不是系统设置 但是 $baseNowAuthName 确实验证得 系统设置权限
  61. if ((in_array($nowAuthName, $allAuth) && !in_array($nowAuthName, $this->auth)) || (in_array($baseNowAuthName, $allAuth) && ($nowAuthName != 'admin/setting.systemconfig/index/type/3/tab_id/11' && !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. }