Auth.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\SystemAuth;
  4. use think\admin\Controller;
  5. use think\admin\helper\QueryHelper;
  6. use think\admin\service\AdminService;
  7. /**
  8. * 系统权限管理
  9. * Class Auth
  10. * @package app\admin\controller
  11. */
  12. class Auth extends Controller
  13. {
  14. /**
  15. * 绑定数据表
  16. * @var string
  17. */
  18. private $table = 'SystemAuth';
  19. /**
  20. * 系统权限管理
  21. * @auth true
  22. * @menu true
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public function index()
  28. {
  29. $this->_query(SystemAuth::class)->layTable(function () {
  30. $this->title = '系统权限管理';
  31. }, function (QueryHelper $query) {
  32. $query->dateBetween('create_at')->like('title,desc')->equal('status,utype');
  33. });
  34. }
  35. /**
  36. * 添加系统权限
  37. * @auth true
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function add()
  43. {
  44. $this->_applyFormToken();
  45. $this->_form($this->table, 'form');
  46. }
  47. /**
  48. * 编辑系统权限
  49. * @auth true
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function edit()
  55. {
  56. $this->_applyFormToken();
  57. $this->_form($this->table, 'form');
  58. }
  59. /**
  60. * 修改权限状态
  61. * @auth true
  62. * @throws \think\db\exception\DbException
  63. */
  64. public function state()
  65. {
  66. $this->_save($this->table, $this->_vali([
  67. 'status.in:0,1' => '状态值范围异常!',
  68. 'status.require' => '状态值不能为空!',
  69. ]));
  70. }
  71. /**
  72. * 权限配置节点
  73. * @auth true
  74. * @throws \ReflectionException
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function apply()
  80. {
  81. $map = $this->_vali(['auth.require#id' => '权限ID不能为空!']);
  82. if (input('action') === 'get') {
  83. if ($this->app->isDebug()) AdminService::instance()->clearCache();
  84. $checkeds = $this->app->db->name('SystemAuthNode')->where($map)->column('node');
  85. $this->success('获取权限节点成功!', AdminService::instance()->getTree($checkeds));
  86. } elseif (input('action') === 'save') {
  87. [$post, $data] = [$this->request->post(), []];
  88. foreach ($post['nodes'] ?? [] as $node) {
  89. $data[] = ['auth' => $map['auth'], 'node' => $node];
  90. }
  91. $this->app->db->name('SystemAuthNode')->where($map)->delete();
  92. $this->app->db->name('SystemAuthNode')->insertAll($data);
  93. sysoplog('系统权限管理', "配置系统权限[{$map['auth']}]授权成功");
  94. $this->success('访问权限修改成功!', 'javascript:history.back()');
  95. } else {
  96. $this->title = '权限配置节点';
  97. $this->_form($this->table, 'apply');
  98. }
  99. }
  100. /**
  101. * 删除系统权限
  102. * @auth true
  103. * @throws \think\db\exception\DbException
  104. */
  105. public function remove()
  106. {
  107. $this->_delete($this->table);
  108. }
  109. /**
  110. * 表单结果处理
  111. * @param bool $result
  112. */
  113. protected function _add_form_result(bool $result)
  114. {
  115. if ($result) {
  116. $id = $this->app->db->name($this->table)->getLastInsID();
  117. sysoplog('系统权限管理', "添加系统权限[{$id}]成功");
  118. }
  119. }
  120. /**
  121. * 表单结果处理
  122. * @param boolean $result
  123. */
  124. protected function _edit_form_result(bool $result)
  125. {
  126. if ($result) {
  127. $id = input('id') ?: 0;
  128. sysoplog('系统权限管理', "修改系统权限[{$id}]成功");
  129. }
  130. }
  131. /**
  132. * 状态结果处理
  133. * @param boolean $result
  134. */
  135. protected function _state_save_result(bool $result)
  136. {
  137. if ($result) {
  138. [$id, $state] = [input('id'), input('status')];
  139. sysoplog('系统权限管理', ($state ? '激活' : '禁用') . "系统权限[{$id}]成功");
  140. }
  141. }
  142. /**
  143. * 删除结果处理
  144. * @param boolean $result
  145. * @throws \think\db\exception\DbException
  146. */
  147. protected function _remove_delete_result(bool $result)
  148. {
  149. if ($result) {
  150. $map = $this->_vali(['auth.require#id' => '权限ID不能为空!']);
  151. $this->app->db->name('SystemAuthNode')->where($map)->delete();
  152. sysoplog('系统权限管理', "删除系统权限[{$map['auth']}]及授权配置");
  153. $this->success("权限删除成功!");
  154. } else {
  155. $this->error("权限删除失败,请稍候再试!");
  156. }
  157. }
  158. }