Auth.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ 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. /**
  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, ['status' => input('status')]);
  76. }
  77. /**
  78. * 权限配置节点
  79. * @auth true
  80. * @return mixed
  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 = ['auth' => input('id', '0')];
  89. $action = strtolower(input('action', ''));
  90. if ($action === 'get') {
  91. $checkeds = $this->app->db->name('SystemAuthNode')->where($map)->column('node');
  92. $this->success('获取权限节点成功!', AdminService::instance()->getTree($checkeds));
  93. } elseif ($action === 'save') {
  94. list($post, $data) = [$this->request->post(), []];
  95. foreach (isset($post['nodes']) ? $post['nodes'] : [] as $node) {
  96. $data[] = ['auth' => $map['auth'], 'node' => $node];
  97. }
  98. $this->app->db->name('SystemAuthNode')->where($map)->delete();
  99. $this->app->db->name('SystemAuthNode')->insertAll($data);
  100. AdminService::instance()->apply(true);
  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 = ['auth' => $this->request->post('id')];
  126. $this->app->db->name('SystemAuthNode')->where($map)->delete();
  127. $this->success("权限删除成功!", '');
  128. } else {
  129. $this->error("权限删除失败,请稍候再试!");
  130. }
  131. }
  132. }