Auth.php 4.4 KB

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