Auth.php 4.5 KB

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