Auth.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace ba;
  3. use Throwable;
  4. use think\facade\Db;
  5. /**
  6. * 权限规则类
  7. */
  8. class Auth
  9. {
  10. /**
  11. * 默认配置
  12. * @var array|string[]
  13. */
  14. protected array $config = [
  15. 'auth_group' => 'admin_group', // 用户组数据表名
  16. 'auth_group_access' => 'admin_group_access', // 用户-用户组关系表
  17. 'auth_rule' => 'admin_rule', // 权限规则表
  18. ];
  19. /**
  20. * 子菜单规则数组
  21. * @var array
  22. */
  23. protected array $children = [];
  24. /**
  25. * 构造方法
  26. * @param array $config
  27. */
  28. public function __construct(array $config = [])
  29. {
  30. $this->config = array_merge($this->config, $config);
  31. }
  32. /**
  33. * 魔术方法-获取当前配置
  34. * @param $name
  35. * @return mixed
  36. */
  37. public function __get($name): mixed
  38. {
  39. return $this->config[$name];
  40. }
  41. /**
  42. * 获取菜单规则列表
  43. * @access public
  44. * @param int $uid 用户ID
  45. * @return array
  46. * @throws Throwable
  47. */
  48. public function getMenus(int $uid): array
  49. {
  50. $this->children = [];
  51. $originAuthRules = $this->getOriginAuthRules($uid);
  52. foreach ($originAuthRules as $rule) {
  53. $this->children[$rule['PID']][] = $rule;
  54. }
  55. // 没有根菜单规则
  56. if (!isset($this->children[0])) return [];
  57. return $this->getChildren($this->children[0]);
  58. }
  59. /**
  60. * 获取传递的菜单规则的子规则
  61. * @param array $rules 菜单规则
  62. * @return array
  63. */
  64. private function getChildren(array $rules): array
  65. {
  66. var_dump($this->children);
  67. foreach ($rules as $key => $rule) {
  68. var_dump($rule['ID']);
  69. if (array_key_exists($rule['ID'], $this->children)) {
  70. $rules[$key]['children'] = $this->getChildren($this->children[$rule['ID']]);
  71. }
  72. }
  73. die;
  74. return $rules;
  75. }
  76. /**
  77. * 检查是否有某权限
  78. * @param string $name 菜单规则的 name,可以传递两个,以','号隔开
  79. * @param int $uid 用户ID
  80. * @param string $relation 如果出现两个 name,是两个都通过(and)还是一个通过即可(or)
  81. * @param string $mode 如果不使用 url 则菜单规则name匹配到即通过
  82. * @return bool
  83. * @throws Throwable
  84. */
  85. public function check(string $name, int $uid, string $relation = 'or', string $mode = 'url'): bool
  86. {
  87. // 获取用户需要验证的所有有效规则列表
  88. $ruleList = $this->getRuleList($uid);
  89. if (in_array('*', $ruleList)) {
  90. return true;
  91. }
  92. if ($name) {
  93. $name = strtolower($name);
  94. if (str_contains($name, ',')) {
  95. $name = explode(',', $name);
  96. } else {
  97. $name = [$name];
  98. }
  99. }
  100. $list = []; //保存验证通过的规则名
  101. if ('url' == $mode) {
  102. $REQUEST = json_decode(strtolower(json_encode(request()->param(), JSON_UNESCAPED_UNICODE)), true);
  103. }
  104. foreach ($ruleList as $rule) {
  105. $query = preg_replace('/^.+\?/U', '', $rule);
  106. if ('url' == $mode && $query != $rule) {
  107. parse_str($query, $param); //解析规则中的param
  108. $intersect = array_intersect_assoc($REQUEST, $param);
  109. $rule = preg_replace('/\?.*$/U', '', $rule);
  110. if (in_array($rule, $name) && $intersect == $param) {
  111. // 如果节点相符且url参数满足
  112. $list[] = $rule;
  113. }
  114. } elseif (in_array($rule, $name)) {
  115. $list[] = $rule;
  116. }
  117. }
  118. if ('or' == $relation && !empty($list)) {
  119. return true;
  120. }
  121. $diff = array_diff($name, $list);
  122. if ('and' == $relation && empty($diff)) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. /**
  128. * 获得权限规则列表
  129. * @param int $uid 用户id
  130. * @return array
  131. * @throws Throwable
  132. */
  133. public function getRuleList(int $uid): array
  134. {
  135. // 读取用户规则节点
  136. $ids = $this->getRuleIds($uid);
  137. if (empty($ids)) return [];
  138. $originAuthRules = $this->getOriginAuthRules($uid);
  139. // 用户规则
  140. $rules = [];
  141. if (in_array('*', $ids)) {
  142. $rules[] = "*";
  143. }
  144. foreach ($originAuthRules as $rule) {
  145. $rules[$rule['ID']] = strtolower($rule['NAME']);
  146. }
  147. return array_unique($rules);
  148. }
  149. /**
  150. * 获得权限规则原始数据
  151. * @param int $uid 用户id
  152. * @return array
  153. * @throws Throwable
  154. */
  155. public function getOriginAuthRules(int $uid): array
  156. {
  157. $ids = $this->getRuleIds($uid);
  158. if (empty($ids)) return [];
  159. $where = [];
  160. $where[] = ['status', '=', '1'];
  161. // 如果没有 * 则只获取用户拥有的规则
  162. if (!in_array('*', $ids)) {
  163. $where[] = ['id', 'in', $ids];
  164. }
  165. $rules = Db::name($this->config['auth_rule'])
  166. ->withoutField(['remark', 'status', 'weigh', 'update_time', 'create_time'])
  167. ->where($where)
  168. ->order('weigh desc,id asc')
  169. ->select()
  170. ->toArray();
  171. foreach ($rules as $key => $rule) {
  172. if (!empty($rule['keepalive'])) {
  173. $rules[$key]['keepalive'] = $rule['name'];
  174. }
  175. }
  176. return $rules;
  177. }
  178. /**
  179. * 获取权限规则ids
  180. * @param int $uid
  181. * @return array
  182. * @throws Throwable
  183. */
  184. public function getRuleIds(int $uid): array
  185. {
  186. // 用户的组别和规则ID
  187. $groups = $this->getGroups($uid);
  188. $ids = [];
  189. foreach ($groups as $g) {
  190. $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
  191. }
  192. return array_unique($ids);
  193. }
  194. /**
  195. * 获取用户所有分组和对应权限规则
  196. * @param int $uid
  197. * @return array
  198. * @throws Throwable
  199. */
  200. public function getGroups(int $uid): array
  201. {
  202. $dbName = $this->config['auth_group_access'] ?: 'user';
  203. if ($this->config['auth_group_access']) {
  204. $userGroups = Db::name($dbName)
  205. ->alias('aga')
  206. ->join($this->config['auth_group'] . ' ag', 'aga.group_id = ag.id', 'LEFT')
  207. ->field('aga.uid,aga.group_id,ag.id,ag.pid,ag.name,ag.rules')
  208. ->where("aga.uid='$uid' and ag.status='1'")
  209. ->select()
  210. ->toArray();
  211. } else {
  212. $userGroups = Db::name($dbName)
  213. ->alias('u')
  214. ->join($this->config['auth_group'] . ' ag', 'u.group_id = ag.id', 'LEFT')
  215. ->field('u.id as uid,u.group_id,ag.id,ag.name,ag.rules')
  216. ->where("u.id='$uid' and ag.status='1'")
  217. ->select()
  218. ->toArray();
  219. }
  220. return $userGroups;
  221. }
  222. }