Auth.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~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 controller\BasicAdmin;
  15. use service\DataService;
  16. use service\NodeService;
  17. use service\ToolsService;
  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. * 默认数据模型
  30. * @var string
  31. */
  32. public $table = 'SystemAuth';
  33. /**
  34. * 权限列表
  35. */
  36. public function index()
  37. {
  38. $this->title = '系统权限管理';
  39. return parent::_list($this->table);
  40. }
  41. /**
  42. * 权限授权
  43. * @return string
  44. */
  45. public function apply()
  46. {
  47. $auth_id = $this->request->get('id', '0');
  48. $method = '_apply_' . strtolower($this->request->get('action', '0'));
  49. if (method_exists($this, $method)) {
  50. return $this->$method($auth_id);
  51. }
  52. $this->assign('title', '节点授权');
  53. return $this->_form($this->table, 'apply');
  54. }
  55. /**
  56. * 读取授权节点
  57. * @param $auth_id
  58. */
  59. protected function _apply_getnode($auth_id)
  60. {
  61. $nodes = NodeService::get();
  62. $checked = Db::name('SystemAuthNode')->where(['auth' => $auth_id])->column('node');
  63. foreach ($nodes as $key => &$node) {
  64. $node['checked'] = in_array($node['node'], $checked);
  65. }
  66. $all = $this->_apply_filter(ToolsService::arr2tree($nodes, 'node', 'pnode', '_sub_'));
  67. $this->success('获取节点成功!', '', $all);
  68. }
  69. /**
  70. * 保存授权节点
  71. * @param $auth_id
  72. */
  73. protected function _apply_save($auth_id)
  74. {
  75. list($data, $post) = [[], $this->request->post()];
  76. foreach (isset($post['nodes']) ? $post['nodes'] : [] as $node) {
  77. $data[] = ['auth' => $auth_id, 'node' => $node];
  78. }
  79. Db::name('SystemAuthNode')->where(['auth' => $auth_id])->delete();
  80. Db::name('SystemAuthNode')->insertAll($data);
  81. $this->success('节点授权更新成功!', '');
  82. }
  83. /**
  84. * 节点数据拼装
  85. * @param array $nodes
  86. * @param int $level
  87. * @return array
  88. */
  89. protected function _apply_filter($nodes, $level = 1)
  90. {
  91. foreach ($nodes as $key => &$node) {
  92. if (!empty($node['_sub_']) && is_array($node['_sub_'])) {
  93. $node['_sub_'] = $this->_apply_filter($node['_sub_'], $level + 1);
  94. }
  95. }
  96. return $nodes;
  97. }
  98. /**
  99. * 权限添加
  100. */
  101. public function add()
  102. {
  103. return $this->_form($this->table, 'form');
  104. }
  105. /**
  106. * 权限编辑
  107. */
  108. public function edit()
  109. {
  110. return $this->_form($this->table, 'form');
  111. }
  112. /**
  113. * 权限禁用
  114. */
  115. public function forbid()
  116. {
  117. if (DataService::update($this->table)) {
  118. $this->success("权限禁用成功!", '');
  119. }
  120. $this->error("权限禁用失败,请稍候再试!");
  121. }
  122. /**
  123. * 权限恢复
  124. */
  125. public function resume()
  126. {
  127. if (DataService::update($this->table)) {
  128. $this->success("权限启用成功!", '');
  129. }
  130. $this->error("权限启用失败,请稍候再试!");
  131. }
  132. /**
  133. * 权限删除
  134. */
  135. public function del()
  136. {
  137. if (DataService::update($this->table)) {
  138. $id = $this->request->post('id');
  139. Db::name('SystemAuthNode')->where('auth', $id)->delete();
  140. $this->success("权限删除成功!", '');
  141. }
  142. $this->error("权限删除失败,请稍候再试!");
  143. }
  144. }