AuthService.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\admin\service;
  3. use app\common\model\AuthGroupAccessModel;
  4. use app\common\model\AuthGroupModel;
  5. use app\common\model\AuthRuleModel;
  6. class AuthService
  7. {
  8. public $authGroupModel;
  9. public $authGroupAccessModel;
  10. public $authRuleModel;
  11. public function __construct()
  12. {
  13. $this->authGroupModel = new AuthGroupModel();
  14. $this->authGroupAccessModel = new AuthGroupAccessModel();
  15. $this->authRuleModel = new AuthRuleModel();
  16. }
  17. /**
  18. * @param int $group_id
  19. * @return array
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public function loadRuleByGroupId(int $group_id) {
  25. $group = $this->authGroupModel->findById($group_id);
  26. if ($group) {
  27. $rules_str = $group->rules;
  28. return $this->authRuleModel->findByIds($rules_str == "*" ? ["*"] : explode(',', $rules_str));
  29. }
  30. return [];
  31. }
  32. /**
  33. * @param int $adminId
  34. * @return array
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function loadRuleByAdminId(int $adminId) {
  40. $access = $this->authGroupAccessModel->findByAdminId($adminId);
  41. if ($access && $access->group_id > 0) {
  42. $group = $this->authGroupModel->findById($access->group_id);
  43. if ($group) {
  44. $rules_str = $group->rules;
  45. return $this->authRuleModel->findByIds($rules_str == "*" ? ["*"] : explode(',', $rules_str));
  46. }
  47. }
  48. return [];
  49. }
  50. }