AuthService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Ycbl\AdminAuth\Service;
  3. use Hyperf\Contract\ConfigInterface;
  4. use Hyperf\Di\Annotation\Inject;
  5. use Hyperf\Utils\Collection;
  6. use Hyperf\Utils\Context;
  7. use Qbhy\HyperfAuth\AuthManager;
  8. use Ycbl\AdminAuth\Dao\AuthGroup;
  9. use Ycbl\AdminAuth\Dao\AuthGroupAccess;
  10. use Ycbl\AdminAuth\Dao\AuthRule;
  11. class AuthService
  12. {
  13. /**
  14. * @Inject
  15. * @var AuthRule
  16. */
  17. protected $authRuleDao;
  18. /**
  19. * @Inject
  20. * @var AuthGroupAccess
  21. */
  22. protected $authGroupAccessDao;
  23. /**
  24. * @Inject
  25. * @var AuthGroup
  26. */
  27. protected $authGroupDao;
  28. /**
  29. * @Inject
  30. * @var AuthManager
  31. */
  32. protected $authManager;
  33. const TREE = 1;
  34. const LIST = 2;
  35. /**
  36. * @var mixed
  37. */
  38. private $config;
  39. public function __construct(ConfigInterface $config)
  40. {
  41. $this->config = $config->get('admin_auth');
  42. }
  43. /**
  44. * 获取菜单列表
  45. * @param int $type
  46. * @param bool $has_role
  47. * @return array
  48. */
  49. public function getMenuList(int $type = self::TREE, $has_role = false)
  50. {
  51. // 读取管理员当前拥有的权限节点
  52. $user_role = $this->getRuleList();
  53. // 获取所有菜单项
  54. if ($has_role){
  55. $ids = $this->getRuleIds();
  56. $list = $this->authRuleDao->getEnableRulesById($ids)->toArray();
  57. }else{
  58. $list = $this->authRuleDao->getAllMenu()->toArray();
  59. }
  60. //
  61. foreach ($list as $k => $v) {
  62. if (!in_array($v['auth'], $user_role)) {
  63. unset($list[$k]);
  64. }
  65. }
  66. $tree = make(TreeService::class)->init($list);
  67. $result = $tree->getTreeArray(0);
  68. if ($type === self::LIST) {
  69. return $tree->getTreeList($result);
  70. } else {
  71. return $result;
  72. }
  73. }
  74. /**
  75. * 获取用户所在权限组
  76. * @param $uid
  77. * @return array|Collection|mixed
  78. */
  79. public function getGroups($uid = '')
  80. {
  81. $uid = $uid ? $uid : $this->authManager->user()->getId();
  82. if (Context::has('auth_group.' . $uid)) {
  83. return Context::get('auth_group.' . $uid);
  84. }
  85. $group_ids = $this->authGroupAccessDao->getUserGroupIds($uid);
  86. $user_group = $this->authGroupDao->getEnableGroupsById($group_ids)->toArray();
  87. Context::set('auth_group.' . $uid, $user_group ?: []);
  88. return Context::get('auth_group.' . $uid);
  89. }
  90. /**
  91. * 获取用户的所有规则ID
  92. * @param $uid
  93. * @return array
  94. */
  95. public function getRuleIds($uid = '')
  96. {
  97. $uid = $uid ? $uid : $this->authManager->user()->getId();
  98. $groups = $this->getGroups($uid);
  99. $ids = [];
  100. foreach ($groups as $group) {
  101. $ids = array_merge($ids, explode(',', trim($group['rules'], ',')));
  102. }
  103. $ids = array_unique($ids);
  104. return $ids;
  105. }
  106. /**
  107. * 获取规则列表
  108. * @param $uid
  109. * @return array|mixed|null
  110. */
  111. public function getRuleList($uid = '')
  112. {
  113. $uid = $uid ? $uid : $this->authManager->user()->getId();
  114. if (Context::has('auth_rule_list.' . $uid)) {
  115. return Context::get('auth_rule_list.' . $uid);
  116. }
  117. $redis = redis_pool('default');
  118. $redis_rule_list = $redis->get('_rule_list_' . $uid);
  119. if (2 == $this->config['auth_type'] && !empty($redis_rule_list)) {
  120. return json_decode($redis_rule_list, true);
  121. }
  122. $ids = $this->getRuleIds($uid);
  123. if (empty($ids)) {
  124. Context::set('auth_rule_list.' . $uid, []);
  125. return [];
  126. }
  127. $rules = $this->authRuleDao->getEnableRulesById($ids)->toArray();
  128. $rule_list = [];
  129. //拥有的规则id 包含* 则直接返回*
  130. if (in_array('*', $ids)) {
  131. $rule_list[] = '*';
  132. }
  133. foreach ($rules as $rule) {
  134. $rule_list[$rule['id']] = $rule['auth'];
  135. }
  136. Context::set('auth_rule_list.' . $uid, $rule_list);
  137. if (2 == $this->config['auth_type']) {
  138. //规则列表结果保存到session
  139. $redis->set('_rule_list_' . $uid, json_encode($rule_list));
  140. }
  141. return array_unique($rule_list);
  142. }
  143. public function cleanCache()
  144. {
  145. $redis = redis_pool('default');
  146. $redis->del("_rule_list_" . $this->authManager->user()->getId());
  147. }
  148. /**
  149. * 检查权限
  150. * @param $name
  151. * @param $uid
  152. * @param string $relation
  153. * @return bool
  154. */
  155. public function check($name, $uid = '', $relation = 'or')
  156. {
  157. $uid = $uid ? $uid : $this->authManager->user()->getId();
  158. //权限认证开关未开启状态直接返回验证成功
  159. if (!$this->config['auth_on']) {
  160. return true;
  161. }
  162. $ruleList = $this->getRuleList($uid);
  163. //规则列表包含* 则直接返回验证通过
  164. if (in_array('*', $ruleList)) {
  165. return true;
  166. }
  167. //判断验证数组还是字符串,转换为数组形式
  168. if (is_string($name)) {
  169. $name = strtolower($name);
  170. if (strpos($name, ',') !== false) {
  171. $name = explode(',', $name);
  172. } else {
  173. $name = [$name];
  174. }
  175. }
  176. //保存验证通过的规则名
  177. $list = [];
  178. foreach ($ruleList as $rule) {
  179. if (in_array($rule, $name)) {
  180. $list[] = $rule;
  181. }
  182. }
  183. if ('or' == $relation && !empty($list)) {
  184. return true;
  185. }
  186. $diff = array_diff($name, $list);
  187. if ('and' == $relation && empty($diff)) {
  188. return true;
  189. }
  190. return false;
  191. }
  192. /**
  193. * 判断当前用户是否超级管理员
  194. * @return bool
  195. */
  196. public function isSuperAdmin()
  197. {
  198. return in_array('*', $this->getRuleIds()) ? true : false;
  199. }
  200. /**
  201. * 获取当前用户ID
  202. * @return mixed
  203. */
  204. public function getUserId()
  205. {
  206. return $this->authManager->user()->getId();
  207. }
  208. }