Auth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\admin\controller;
  16. use think\admin\Controller;
  17. use think\admin\helper\QueryHelper;
  18. use think\admin\model\SystemAuth;
  19. use think\admin\model\SystemNode;
  20. use think\admin\service\AdminService;
  21. /**
  22. * 系统权限管理
  23. * Class Auth
  24. * @package app\admin\controller
  25. */
  26. class Auth extends Controller
  27. {
  28. /**
  29. * 系统权限管理
  30. * @auth true
  31. * @menu true
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function index()
  37. {
  38. SystemAuth::mQuery()->layTable(function () {
  39. $this->title = '系统权限管理';
  40. }, function (QueryHelper $query) {
  41. $query->dateBetween('create_at')->like('title,desc')->equal('status,utype');
  42. });
  43. }
  44. /**
  45. * 添加系统权限
  46. * @auth true
  47. */
  48. public function add()
  49. {
  50. SystemAuth::mForm('form');
  51. }
  52. /**
  53. * 编辑系统权限
  54. * @auth true
  55. */
  56. public function edit()
  57. {
  58. SystemAuth::mForm('form');
  59. }
  60. /**
  61. * 修改权限状态
  62. * @auth true
  63. */
  64. public function state()
  65. {
  66. SystemAuth::mSave($this->_vali([
  67. 'status.in:0,1' => '状态值范围异常!',
  68. 'status.require' => '状态值不能为空!',
  69. ]));
  70. }
  71. /**
  72. * 删除系统权限
  73. * @auth true
  74. */
  75. public function remove()
  76. {
  77. SystemAuth::mDelete();
  78. }
  79. /**
  80. * 权限配置节点
  81. * @auth true
  82. * @throws \ReflectionException
  83. */
  84. public function apply()
  85. {
  86. $map = $this->_vali(['auth.require#id' => '权限ID不能为空!']);
  87. if (input('action') === 'get') {
  88. if ($this->app->isDebug()) AdminService::clearCache();
  89. $nodes = SystemNode::mk()->where($map)->column('node');
  90. $this->success('获取权限节点成功!', AdminService::getTree($nodes));
  91. } elseif (input('action') === 'save') {
  92. [$post, $data] = [$this->request->post(), []];
  93. foreach ($post['nodes'] ?? [] as $node) {
  94. $data[] = ['auth' => $map['auth'], 'node' => $node];
  95. }
  96. SystemNode::mk()->where($map)->delete();
  97. SystemNode::mk()->insertAll($data);
  98. sysoplog('系统权限管理', "配置系统权限[{$map['auth']}]授权成功");
  99. $this->success('访问权限修改成功!', 'javascript:history.back()');
  100. } else {
  101. SystemAuth::mForm('apply');
  102. }
  103. }
  104. /**
  105. * 表单后置数据处理
  106. * @param array $data
  107. */
  108. protected function _apply_form_filter(array &$data)
  109. {
  110. if ($this->request->isGet()) {
  111. $this->title = "编辑【{$data['title']}】授权";
  112. }
  113. }
  114. }