Auth.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use library\Controller;
  15. use think\Db;
  16. /**
  17. * 系统权限管理
  18. * Class Auth
  19. * @package app\admin\controller
  20. */
  21. class Auth extends Controller
  22. {
  23. /**
  24. * 默认数据模型
  25. * @var string
  26. */
  27. public $table = 'SystemAuth';
  28. /**
  29. * 系统权限管理
  30. * @throws \think\Exception
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @throws \think\exception\DbException
  34. * @throws \think\exception\PDOException
  35. */
  36. public function index()
  37. {
  38. $this->title = '系统权限管理';
  39. $query = $this->_query($this->table)->dateBetween('create_at');
  40. $query->like('title,desc')->equal('status')->order('sort asc,id desc')->page();
  41. }
  42. /**
  43. * 权限配置节点
  44. * @return mixed
  45. * @throws \ReflectionException
  46. * @throws \think\Exception
  47. * @throws \think\exception\PDOException
  48. */
  49. public function apply()
  50. {
  51. $this->title = '权限配置节点';
  52. $auth = $this->request->post('id', '0');
  53. switch (strtolower($this->request->post('action'))) {
  54. case 'get': // 获取权限配置
  55. $nodes = \app\admin\service\AuthService::get();
  56. $checked = Db::name('SystemAuthNode')->where(['auth' => $auth])->column('node');
  57. foreach ($nodes as &$node) $node['checked'] = in_array($node['node'], $checked);
  58. $data = $this->_apply_filter(\library\tools\Data::arr2tree($nodes, 'node', 'pnode', '_sub_'));
  59. return $this->success('获取权限节点成功!', $data);
  60. case 'save': // 保存权限配置
  61. list($post, $data) = [$this->request->post(), []];
  62. foreach (isset($post['nodes']) ? $post['nodes'] : [] as $node) {
  63. $data[] = ['auth' => $auth, 'node' => $node];
  64. }
  65. Db::name('SystemAuthNode')->where(['auth' => $auth])->delete();
  66. Db::name('SystemAuthNode')->insertAll($data);
  67. return $this->success('权限授权更新成功!');
  68. default:
  69. return $this->_form($this->table, 'apply');
  70. }
  71. }
  72. /**
  73. * 节点数据拼装
  74. * @param array $nodes
  75. * @param integer $level
  76. * @return array
  77. */
  78. private function _apply_filter($nodes, $level = 1)
  79. {
  80. foreach ($nodes as $key => $node) if (!empty($node['_sub_']) && is_array($node['_sub_'])) {
  81. $node[$key]['_sub_'] = $this->_apply_filter($node['_sub_'], $level + 1);
  82. }
  83. return $nodes;
  84. }
  85. /**
  86. * 添加系统权限
  87. * @return array|string
  88. */
  89. public function add()
  90. {
  91. $this->applyCsrfToken();
  92. $this->_form($this->table, 'form');
  93. }
  94. /**
  95. * 编辑系统权限
  96. * @return array|string
  97. */
  98. public function edit()
  99. {
  100. $this->applyCsrfToken();
  101. $this->_form($this->table, 'form');
  102. }
  103. /**
  104. * 禁用系统权限
  105. */
  106. public function forbid()
  107. {
  108. $this->applyCsrfToken();
  109. $this->_save($this->table, ['status' => '0']);
  110. }
  111. /**
  112. * 启用系统权限
  113. */
  114. public function resume()
  115. {
  116. $this->applyCsrfToken();
  117. $this->_save($this->table, ['status' => '1']);
  118. }
  119. /**
  120. * 删除系统权限
  121. */
  122. public function remove()
  123. {
  124. $this->applyCsrfToken();
  125. $this->_delete($this->table);
  126. }
  127. /**
  128. * 删除结果处理
  129. * @param boolean $result
  130. * @throws \think\Exception
  131. * @throws \think\exception\PDOException
  132. */
  133. protected function _remove_delete_result($result)
  134. {
  135. if ($result) {
  136. $where = ['auth' => $this->request->post('id')];
  137. Db::name('SystemAuthNode')->where($where)->delete();
  138. $this->success("权限删除成功!", '');
  139. } else {
  140. $this->error("权限删除失败,请稍候再试!");
  141. }
  142. }
  143. }