AuthService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constants\AdminConstant;
  4. use EasyAdmin\tool\CommonTool;
  5. use think\facade\Db;
  6. /**
  7. * 权限验证服务 by wes
  8. * Class AuthService
  9. * @package app\common\service
  10. */
  11. class AuthService
  12. {
  13. /**
  14. * 用户ID
  15. * @var null
  16. */
  17. protected $adminId = null;
  18. /**
  19. * 默认配置
  20. * @var array
  21. */
  22. protected $config = [
  23. 'auth_on' => true, // 权限开关
  24. 'system_admin' => 'system_admin', // 用户表
  25. 'system_auth' => 'system_auth', // 权限表
  26. 'system_node' => 'system_node', // 节点表
  27. 'system_auth_node' => 'system_auth_node',// 权限-节点表
  28. ];
  29. /**
  30. * 管理员信息
  31. * @var array|\think\Model|null
  32. */
  33. protected $adminInfo;
  34. /**
  35. * 所有节点信息
  36. * @var array
  37. */
  38. protected $nodeList;
  39. /**
  40. * 管理员所有授权节点
  41. * @var array
  42. */
  43. protected $adminNode;
  44. /***
  45. * 构造方法
  46. * AuthService constructor.
  47. * @param null $adminId
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function __construct($adminId = null)
  53. {
  54. $this->adminId = $adminId;
  55. $this->adminInfo = $this->getAdminInfo();
  56. $this->nodeList = $this->getNodeList();
  57. $this->adminNode = $this->getAdminNode();
  58. return $this;
  59. }
  60. /**
  61. * 检测检测权限
  62. * @param null $node
  63. * @return bool
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function checkNode($node = null)
  69. {
  70. // 判断是否为超级管理员
  71. if ($this->adminId == AdminConstant::SUPER_ADMIN_ID) {
  72. return true;
  73. }
  74. // 判断权限验证开关
  75. if ($this->config['auth_on'] == false) {
  76. return true;
  77. }
  78. // 判断是否需要获取当前节点
  79. if (empty($node)) {
  80. $node = $this->getCurrentNode();
  81. } else {
  82. $node = $this->parseNodeStr($node);
  83. }
  84. // 判断是否加入节点控制,优先获取缓存信息
  85. if (!isset($this->nodeList[$node])) {
  86. return false;
  87. }
  88. $nodeInfo = $this->nodeList[$node];
  89. if ($nodeInfo['is_auth'] == 0) {
  90. return true;
  91. }
  92. // 用户验证,优先获取缓存信息
  93. if (empty($this->adminInfo) || $this->adminInfo['status'] != 1 || empty($this->adminInfo['auth_ids'])) {
  94. return false;
  95. }
  96. // 判断该节点是否允许访问
  97. if (in_array($node, $this->adminNode)) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * 获取当前节点
  104. * @return string
  105. */
  106. public function getCurrentNode()
  107. {
  108. $node = $this->parseNodeStr(request()->controller() . '/' . request()->action());
  109. return $node;
  110. }
  111. /**
  112. * 获取当前管理员所有节点
  113. * @return array
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function getAdminNode()
  119. {
  120. $nodeList = [];
  121. $adminInfo = Db::name($this->config['system_admin'])
  122. ->where([
  123. 'id' => $this->adminId,
  124. 'status' => 1,
  125. ])->find();
  126. if (!empty($adminInfo) && !empty($adminInfo['auth_ids'])) {
  127. $buildAuthSql = Db::name($this->config['system_auth'])
  128. ->distinct(true)
  129. ->whereIn('id', $adminInfo['auth_ids'])
  130. ->field('id')
  131. ->buildSql(true);
  132. $buildAuthNodeSql = Db::name($this->config['system_auth_node'])
  133. ->distinct(true)
  134. ->where("auth_id IN {$buildAuthSql}")
  135. ->field('node_id')
  136. ->buildSql(true);
  137. $nodeList = Db::name($this->config['system_node'])
  138. ->distinct(true)
  139. ->where("id IN {$buildAuthNodeSql}")
  140. ->column('node');
  141. }
  142. return $nodeList;
  143. }
  144. /**
  145. * 获取所有节点信息
  146. * @time 2021-01-07
  147. * @return array
  148. * @author zhongshaofa <shaofa.zhong@happy-seed.com>
  149. */
  150. public function getNodeList(){
  151. return Db::name($this->config['system_node'])
  152. ->column('id,node,title,type,is_auth','node');
  153. }
  154. /**
  155. * 获取管理员信息
  156. * @time 2021-01-07
  157. * @return array|\think\Model|null
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. * @author zhongshaofa <shaofa.zhong@happy-seed.com>
  162. */
  163. public function getAdminInfo(){
  164. return Db::name($this->config['system_admin'])
  165. ->where('id', $this->adminId)
  166. ->find();
  167. }
  168. /**
  169. * 驼峰转下划线规则
  170. * @param string $node
  171. * @return string
  172. */
  173. public function parseNodeStr($node)
  174. {
  175. $array = explode('/', $node);
  176. foreach ($array as $key => $val) {
  177. if ($key == 0) {
  178. $val = explode('.', $val);
  179. foreach ($val as &$vo) {
  180. $vo = CommonTool::humpToLine(lcfirst($vo));
  181. }
  182. $val = implode('.', $val);
  183. $array[$key] = $val;
  184. }
  185. }
  186. $node = implode('/', $array);
  187. return $node;
  188. }
  189. }