Auth.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\AuthService;
  4. use app\common\model\AuthGroupAccessModel;
  5. use app\common\model\AuthGroupModel;
  6. use app\common\model\AuthRuleModel;
  7. use app\Request;
  8. use think\App;
  9. use app\BaseController;
  10. use think\facade\View;
  11. class Auth extends BaseController
  12. {
  13. private $service;
  14. public function __construct(App $app)
  15. {
  16. $this->service = new AuthService();
  17. parent::__construct($app);
  18. }
  19. public function index()
  20. {
  21. View::assign([
  22. 'list' => $this->service->authGroupModel->findByPaginate($this->request->param()),
  23. ]);
  24. return view();
  25. }
  26. public function add() {
  27. if ($this->request->isAjax()) {
  28. $params = $this->request->param();
  29. $res = $this->service->authGroupModel->save([
  30. 'name' => format_string($params['name'] ?? null),
  31. 'rules' => format_string($params['rule_ids'] ?? null),
  32. 'describe' => format_string($params['describe'] ?? null),
  33. ]);
  34. return $this->ok($res);
  35. }
  36. return view();
  37. }
  38. public function edit() {
  39. $params = $this->request->param();
  40. $id = $params['id'];
  41. $group = $this->service->authGroupModel->findById($id);
  42. if ($this->request->isAjax()) {
  43. $res = $this->service->authGroupModel->where('id', $params['id'])->save([
  44. 'name' => format_string($params['name'] ?? null),
  45. 'rules' => format_string($params['rule_ids'] ?? null),
  46. 'describe' => format_string($params['describe'] ?? null),
  47. 'update_time' => time()
  48. ]);
  49. return $this->ok($res);
  50. }
  51. View::assign([
  52. 'group' => $group,
  53. ]);
  54. return view();
  55. }
  56. public function findAuthRuleByAdminId($admin_id = null) {
  57. $all_rules = $this->service->authRuleModel->findByIds(["*"]);
  58. $format_rules = array_map(function ($data) {
  59. return [
  60. 'id' => $data['id'],
  61. 'title' => $data['name'],
  62. 'pid' => $data['pid'],
  63. 'field' => $data['name'],
  64. 'spread' => false,
  65. 'checked' => false
  66. ];
  67. }, $all_rules);
  68. if(!isset($admin_id))
  69. return $this->ok(recursion($format_rules));
  70. $rules = $this->service->loadRuleByAdminId($admin_id);
  71. $in_rule_ids = array_map(function ($data) {
  72. return $data['id'];
  73. }, $rules);
  74. foreach ($format_rules as &$item) $item['checked'] = in_array($item['id'], $in_rule_ids);
  75. return $this->ok(recursion($format_rules));
  76. }
  77. /**
  78. * @param null $group_id
  79. * @return \think\response\Json
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function findAuthRuleByGroupId($group_id = null) {
  85. $all_rules = $this->service->authRuleModel->findByIds(["*"]);
  86. $format_rules = array_map(function ($data) {
  87. return [
  88. 'id' => $data['id'],
  89. 'title' => $data['name'],
  90. 'pid' => $data['pid'],
  91. 'field' => $data['name'],
  92. 'spread' => false,
  93. 'checked' => false
  94. ];
  95. }, $all_rules);
  96. if(!isset($group_id))
  97. return $this->ok(recursion($format_rules));
  98. $rules = $this->service->loadRuleByGroupId($group_id);
  99. $in_rule_ids = array_map(function ($data) {
  100. return $data['id'];
  101. }, $rules);
  102. foreach ($format_rules as &$item) $item['checked'] = in_array($item['id'], $in_rule_ids);
  103. return $this->ok(recursion($format_rules));
  104. }
  105. }