Auth.php 5.8 KB

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