Auth.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use app\admin\model\Node;
  15. use controller\BasicAdmin;
  16. use library\Data;
  17. use library\Tools;
  18. use think\Db;
  19. /**
  20. * 系统权限管理控制器
  21. * Class Auth
  22. * @package app\admin\controller
  23. * @author Anyon <zoujingli@qq.com>
  24. * @date 2017/02/15 18:13
  25. */
  26. class Auth extends BasicAdmin {
  27. /**
  28. * 默认数据模型
  29. * @var string
  30. */
  31. protected $table = 'SystemAuth';
  32. /**
  33. * 权限列表
  34. */
  35. public function index() {
  36. $this->title = '系统权限管理';
  37. parent::_list($this->table);
  38. }
  39. /**
  40. * 权限授权
  41. * @return void
  42. */
  43. public function apply() {
  44. $auth_id = $this->request->get('id', '0');
  45. switch (strtolower($this->request->get('action', '0'))) {
  46. case 'getnode':
  47. $nodes = Node::get();
  48. $checked = Db::name('SystemAuthNode')->where('auth', $auth_id)->column('node');
  49. foreach ($nodes as $key => &$node) {
  50. $node['checked'] = in_array($node['node'], $checked);
  51. if (empty($node['is_auth']) && substr_count($node['node'], '/') > 1) {
  52. unset($nodes[$key]);
  53. }
  54. }
  55. $this->success('获取节点成功!', '', $this->_filterNodes($this->_filterNodes(Tools::arr2tree($nodes, 'node', 'pnode', '_sub_'))));
  56. break;
  57. case 'save':
  58. $data = [];
  59. $post = $this->request->post();
  60. foreach (isset($post['nodes']) ? $post['nodes'] : [] as $node) {
  61. $data[] = ['auth' => $auth_id, 'node' => $node];
  62. }
  63. Db::name('SystemAuthNode')->where('auth', $auth_id)->delete();
  64. Db::name('SystemAuthNode')->insertAll($data);
  65. $this->success('节点授权更新成功!', '');
  66. break;
  67. default :
  68. $this->assign('title', '节点授权');
  69. $this->_form($this->table, 'apply');
  70. }
  71. }
  72. protected function _filterNodes($nodes, $level = 1) {
  73. foreach ($nodes as $key => &$node) {
  74. if (!empty($node['_sub_']) && is_array($node['_sub_'])) {
  75. $node['_sub_'] = $this->_filterNodes($node['_sub_'], $level + 1);
  76. } elseif ($level < 3) {
  77. unset($nodes[$key]);
  78. }
  79. }
  80. return $nodes;
  81. }
  82. /**
  83. * 权限添加
  84. */
  85. public function add() {
  86. return $this->_form($this->table, 'form');
  87. }
  88. /**
  89. * 权限编辑
  90. */
  91. public function edit() {
  92. return $this->add();
  93. }
  94. /**
  95. * 权限禁用
  96. */
  97. public function forbid() {
  98. if (Data::update($this->table)) {
  99. $this->success("权限禁用成功!", '');
  100. } else {
  101. $this->error("权限禁用失败,请稍候再试!");
  102. }
  103. }
  104. /**
  105. * 权限恢复
  106. */
  107. public function resume() {
  108. if (Data::update($this->table)) {
  109. $this->success("权限启用成功!", '');
  110. } else {
  111. $this->error("权限启用失败,请稍候再试!");
  112. }
  113. }
  114. /**
  115. * 权限删除
  116. */
  117. public function del() {
  118. if (Data::update($this->table)) {
  119. $this->success("权限删除成功!", '');
  120. } else {
  121. $this->error("权限删除失败,请稍候再试!");
  122. }
  123. }
  124. }