AuthService.php 6.1 KB

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