AuthGroupAccessService.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Ycbl\AdminAuth\Service;
  3. use Exception;
  4. use Hyperf\Di\Annotation\Inject;
  5. use Ycbl\AdminAuth\Dao\AuthGroupAccess;
  6. class AuthGroupAccessService
  7. {
  8. /**
  9. * @Inject
  10. * @var AuthGroupService
  11. */
  12. protected $authGroup;
  13. /**
  14. * @Inject
  15. * @var AuthGroupAccess
  16. */
  17. protected $authGroupAccessDao;
  18. /**
  19. * 增加用户权限组关系
  20. * @param $uid
  21. * @param string|array $group_id
  22. * @return bool
  23. */
  24. public function saveAuthGroupAccess(int $uid, $group_id)
  25. {
  26. if (!is_array($group_id)) {
  27. $group_id = explode(',', $group_id);
  28. }
  29. $children_group_ids = $this->authGroup->getChildrenGroupIds(true);
  30. //过滤不允许的组别,避免越权
  31. $groups = array_intersect($children_group_ids, $group_id);
  32. $dataSet = [];
  33. foreach ($groups as $group) {
  34. $dataSet[] = ['uid' => $uid, 'group_id' => $group];
  35. }
  36. return $this->authGroupAccessDao->saveAll($dataSet);
  37. }
  38. /**
  39. * 更新权限组
  40. * @param int $uid
  41. * @param $group_id
  42. * @return bool
  43. */
  44. public function updateAuthGroupAccess(int $uid, $group_id)
  45. {
  46. $this->authGroupAccessDao->deleteByUid($uid);
  47. return $this->saveAuthGroupAccess($uid, $group_id);
  48. }
  49. /**
  50. * 删除权限组
  51. * @param int $uid
  52. * @return int|mixed
  53. * @throws Exception
  54. */
  55. public function deleteAuthGroupAccess(int $uid)
  56. {
  57. $children_admin_ids = $this->authGroup->getChildrenAdminIds(true);
  58. if (!in_array($uid,$children_admin_ids)){
  59. throw new Exception('您没有权限');
  60. }
  61. return $this->authGroupAccessDao->deleteByUid($uid);
  62. }
  63. }