Auth.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 library\Controller;
  16. use library\service\AdminService;
  17. use think\Db;
  18. /**
  19. * 权限管理
  20. * Class
  21. * @package app\admin\controller
  22. */
  23. class Auth extends Controller
  24. {
  25. /**
  26. * 默认数据模型
  27. * @var string
  28. */
  29. public $table = 'SystemAuth';
  30. /**
  31. * 权限管理
  32. * @auth true
  33. * @menu true
  34. * @throws \think\Exception
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. * @throws \think\exception\PDOException
  39. */
  40. public function index()
  41. {
  42. $this->title = '权限管理';
  43. sysoplog('权限管理', '访问权限管理页面');
  44. $query = $this->_query($this->table)->dateBetween('create_at');
  45. $query->like('title,desc')->equal('status')->order('sort desc,id desc')->page();
  46. }
  47. /**
  48. * 授权
  49. * @auth true
  50. * @throws \ReflectionException
  51. * @throws \think\Exception
  52. * @throws \think\exception\PDOException
  53. */
  54. public function apply()
  55. {
  56. $map = ['auth' => input('id', '0')];
  57. $action = strtolower(input('action', ''));
  58. if ($action === 'get') {
  59. $checkeds = Db::name('SystemAuthNode')->where($map)->column('node');
  60. sysoplog('权限管理', '获取权限节点成功');
  61. $this->success('获取权限节点成功!', AdminService::instance()->getTree($checkeds));
  62. } elseif ($action === 'save') {
  63. list($post, $data) = [$this->request->post(), []];
  64. foreach (isset($post['nodes']) ? $post['nodes'] : [] as $node) {
  65. $data[] = ['auth' => $map['auth'], 'node' => $node];
  66. }
  67. Db::name('SystemAuthNode')->where($map)->delete();
  68. Db::name('SystemAuthNode')->insertAll($data);
  69. AdminService::instance()->apply(true);
  70. sysoplog('权限管理', '权限授权更新成功');
  71. $this->success('权限授权更新成功!', 'javascript:history.back()');
  72. } else {
  73. $this->title = '权限配置节点';
  74. $this->_form($this->table, 'apply');
  75. }
  76. }
  77. /**
  78. * 添加权限
  79. * @auth true
  80. * @throws \think\Exception
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. * @throws \think\exception\PDOException
  85. */
  86. public function add()
  87. {
  88. $this->_form($this->table, 'form');
  89. }
  90. /**
  91. * 编辑权限
  92. * @auth true
  93. * @throws \think\Exception
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. * @throws \think\exception\PDOException
  98. */
  99. public function edit()
  100. {
  101. $this->_form($this->table, 'form');
  102. }
  103. /**
  104. * 处理成功回调
  105. */
  106. public function _form_result($result,$data){
  107. $name = isset($data['id']) ? '编辑' : '新增';
  108. if ($result) {
  109. sysoplog('权限管理', $name.'成功'.json_encode($data,true));
  110. }else{
  111. sysoplog('权限管理', $name.'失败'.json_encode($data,true));
  112. }
  113. }
  114. /**
  115. * 刷新权限
  116. * @auth true
  117. */
  118. public function refresh()
  119. {
  120. try {
  121. AdminService::instance()->apply(true);
  122. sysoplog('权限管理', '刷新系统授权成功');
  123. $this->success('刷新系统授权成功!');
  124. } catch (\think\exception\HttpResponseException $exception) {
  125. throw $exception;
  126. } catch (\Exception $e) {
  127. $this->error("刷新系统授权失败<br>{$e->getMessage()}");
  128. }
  129. }
  130. /**
  131. * 禁用权限
  132. * @auth true
  133. * @throws \think\Exception
  134. * @throws \think\exception\PDOException
  135. */
  136. public function forbid()
  137. {
  138. $this->applyCsrfToken();
  139. $log = ['action'=>'权限管理','content'=>'禁用'];
  140. $this->_save($this->table, ['status' => '0'],$log);
  141. }
  142. /**
  143. * 启用权限
  144. * @auth true
  145. * @throws \think\Exception
  146. * @throws \think\exception\PDOException
  147. */
  148. public function resume()
  149. {
  150. $this->applyCsrfToken();
  151. $log = ['action'=>'权限管理','content'=>'启用'];
  152. $this->_save($this->table, ['status' => '1'],$log);
  153. }
  154. /**
  155. * 删除权限
  156. * @auth true
  157. * @throws \think\Exception
  158. * @throws \think\exception\PDOException
  159. */
  160. public function remove()
  161. {
  162. $this->applyCsrfToken();
  163. $log = ['action'=>'权限管理'];
  164. $this->_delete($this->table,$log);
  165. }
  166. /**
  167. * 删除结果处理
  168. * @param boolean $result
  169. * @throws \think\Exception
  170. * @throws \think\exception\PDOException
  171. */
  172. protected function _remove_delete_result($result)
  173. {
  174. if ($result) {
  175. $map = ['auth' => $this->request->post('id')];
  176. Db::name('SystemAuthNode')->where($map)->delete();
  177. $this->success("权限删除成功!", '');
  178. } else {
  179. $this->error("权限删除失败,请稍候再试!");
  180. }
  181. }
  182. }