Auth.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\service\admin;
  3. use app\model\admin\Role;
  4. use app\model\admin\role\Menu;
  5. use laytp\traits\Error;
  6. use think\facade\Request;
  7. /**
  8. * 后台权限服务实现者
  9. * Class Auth
  10. * @package app\service\admin
  11. */
  12. class Auth
  13. {
  14. use Error;
  15. protected $_noNeedLogin = [];//无需登录的方法名数组
  16. protected $_noNeedAuth = [];//无需鉴权的方法名数组
  17. /**
  18. * 设置无需登录的方法名数组
  19. * @param array $noNeedLogin
  20. */
  21. public function setNoNeedLogin($noNeedLogin = [])
  22. {
  23. $this->_noNeedLogin = $noNeedLogin;
  24. }
  25. /**
  26. * 获取无需登录的方法名数组
  27. * @return array
  28. */
  29. public function getNoNeedLogin()
  30. {
  31. return $this->_noNeedLogin;
  32. }
  33. /**
  34. * 当前节点是否需要登录
  35. * @param bool $noNeedLogin
  36. * @return bool true:需要登录,false:不需要登录
  37. */
  38. public function needLogin($noNeedLogin = false)
  39. {
  40. $noNeedLogin === false && $noNeedLogin = $this->getNoNeedLogin();
  41. $noNeedLogin = is_array($noNeedLogin) ? $noNeedLogin : explode(',', $noNeedLogin);
  42. //为空表示所有方法都需要登录,返回true
  43. if (!$noNeedLogin) {
  44. return true;
  45. }
  46. $noNeedLogin = array_map('strtolower', $noNeedLogin);
  47. $request = Request::instance();
  48. //判断当前请求的操作名是否存在于不需要登录的方法名数组中,如果存在,表明不需要登录,返回false
  49. if (in_array(strtolower($request->action()), $noNeedLogin) || in_array('*', $noNeedLogin)) {
  50. return false;
  51. }
  52. //默认为需要登录
  53. return true;
  54. }
  55. /**
  56. * 设置无需鉴权的方法名数组
  57. * @param array $noNeedAuth
  58. */
  59. public function setNoNeedAuth($noNeedAuth = [])
  60. {
  61. $this->_noNeedAuth = $noNeedAuth;
  62. }
  63. /**
  64. * 获取无需鉴权的方法名数组
  65. * @return array
  66. */
  67. public function getNoNeedAuth()
  68. {
  69. return $this->_noNeedAuth;
  70. }
  71. /**
  72. * 当前节点是否需要鉴权
  73. * @param bool $noNeedAuth
  74. * @return bool true:需要登录,false:不需要登录
  75. */
  76. public function needAuth($noNeedAuth = false)
  77. {
  78. $noNeedAuth === false && $noNeedAuth = $this->getNoNeedAuth();
  79. $noNeedAuth = is_array($noNeedAuth) ? $noNeedAuth : explode(',', $noNeedAuth);
  80. //为空表示所有方法都需要鉴权,返回true
  81. if (!$noNeedAuth) {
  82. return true;
  83. }
  84. $noNeedAuth = array_map('strtolower', $noNeedAuth);
  85. //判断当前请求的操作名是否存在于不需要鉴权的方法名数组中,如果存在,表明不需要鉴权,返回false
  86. if (in_array(strtolower(Request::action()), $noNeedAuth) || in_array('*', $noNeedAuth)) {
  87. return false;
  88. }
  89. //默认为需要鉴权
  90. return true;
  91. }
  92. /**
  93. * 获取某用户拥有的权限列表
  94. * @param $userId int 用户ID,当为空时,为获取当前登录用户权限列表
  95. * @return array
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public function getAuthList($userId = 0)
  101. {
  102. $where[] = ['is_show', '=', 1];
  103. $user = $userId ? \app\model\admin\User::findOrEmpty($userId) : UserServiceFacade::getUser();
  104. //当前后台管理员如果是超级管理员,则拥有所有的权限列表
  105. if ($user->is_super_manager === 1) {
  106. $result = \app\model\admin\Menu::where($where)->select()->toArray();
  107. } else {
  108. //如果不是超级管理员,先查询拥有哪些角色,通过角色查询出权限节点列表
  109. $adminUserId = $user->id;
  110. $roleIds = \app\model\admin\role\User::where('admin_user_id', '=', $adminUserId)->column('admin_role_id');
  111. $menuIds = \app\model\admin\menu\Role::where('admin_role_id', 'in', $roleIds)->column('admin_menu_id');
  112. $where[] = ['id', 'in', $menuIds];
  113. $result = \app\model\admin\Menu::where($where)->select()->toArray();
  114. }
  115. return $result;
  116. }
  117. /**
  118. * 获取某用户是否有某节点的权限
  119. * @param integer $userId 登录用户ID
  120. * @param string $node 节点字符串
  121. * @return bool
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. */
  126. public function hasAuth($userId, $node)
  127. {
  128. if (!$userId || !$node) return false;
  129. $authList = $this->getAuthList($userId);
  130. $authArr = [];
  131. foreach ($authList as $k => $v) {
  132. $authArr[] = trim($v['rule'], '/');
  133. }
  134. $authArr = array_filter(array_unique($authArr));
  135. sort($authArr);
  136. if (in_array($node, $authArr)) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. }