説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Group.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2007 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: fastadmin: https://www.fastadmin.net/
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\Controller\auth;
  12. use app\admin\model\AuthGroup as AuthGroupModel;
  13. use app\common\controller\Adminbase;
  14. use think\Db;
  15. use util\Tree;
  16. /**
  17. * 权限管理控制器
  18. */
  19. class Group extends Adminbase
  20. {
  21. //当前登录管理员所有子组别
  22. protected $childrenGroupIds = [];
  23. //当前组别列表数据
  24. protected $grouplist = [];
  25. protected $groupdata = [];
  26. protected $noNeedRight = [];
  27. protected function initialize()
  28. {
  29. parent::initialize();
  30. $this->modelClass = new AuthGroupModel;
  31. $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  32. $groupList = AuthGroupModel::where('id', 'in', $this->childrenGroupIds)->select()->toArray();
  33. Tree::instance()->init($groupList);
  34. $groupList = [];
  35. if ($this->auth->isAdministrator()) {
  36. $groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  37. } else {
  38. $groups = $this->auth->getGroups();
  39. $groupIds = [];
  40. foreach ($groups as $m => $n) {
  41. if (in_array($n['id'], $groupIds) || in_array($n['parentid'], $groupIds)) {
  42. continue;
  43. }
  44. $groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['parentid']), 'title'));
  45. foreach ($groupList as $index => $item) {
  46. $groupIds[] = $item['id'];
  47. }
  48. }
  49. }
  50. $groupName = [];
  51. foreach ($groupList as $k => $v) {
  52. $groupName[$v['id']] = $v['title'];
  53. }
  54. $this->grouplist = $groupList;
  55. $this->groupdata = $groupName;
  56. $this->assign('groupdata', $this->groupdata);
  57. }
  58. //权限管理首页
  59. public function index()
  60. {
  61. if ($this->request->isAjax()) {
  62. $list = $this->grouplist;
  63. $total = count($list);
  64. $result = ["code" => 0, "count" => $total, "data" => $list];
  65. return json($result);
  66. } else {
  67. return $this->fetch();
  68. }
  69. }
  70. //创建管理员用户组
  71. public function add()
  72. {
  73. if ($this->request->isPost()) {
  74. $this->token();
  75. $params = $this->request->post("row/a", [], 'strip_tags');
  76. $result = $this->validate($params, 'app\admin\validate\AuthGroup');
  77. if (true !== $result) {
  78. return $this->error($result);
  79. }
  80. if (!in_array($params['parentid'], $this->childrenGroupIds)) {
  81. $this->error('父组别超出权限范围');
  82. }
  83. $parentmodel = AuthGroupModel::get($params['parentid']);
  84. if (!$parentmodel) {
  85. $this->error('父组别未找到');
  86. }
  87. if ($params) {
  88. $this->modelClass->create($params);
  89. $this->success('新增成功');
  90. }
  91. $this->error('参数不能为空');
  92. }
  93. return $this->fetch();
  94. }
  95. //编辑管理员用户组
  96. public function edit()
  97. {
  98. $id = $this->request->param('id/d');
  99. if (!in_array($id, $this->childrenGroupIds)) {
  100. $this->error('你没有权限访问!');
  101. }
  102. $row = $this->modelClass->get($id);
  103. if (!$row) {
  104. $this->error('记录未找到');
  105. }
  106. if ($this->request->isPost()) {
  107. $this->token();
  108. $params = $this->request->post("row/a", [], 'strip_tags');
  109. //父节点不能是非权限内节点
  110. if (!in_array($params['parentid'], $this->childrenGroupIds)) {
  111. $this->error('父组别超出权限范围');
  112. }
  113. // 父节点不能是它自身的子节点或自己本身
  114. if (in_array($params['parentid'], Tree::instance()->getChildrenIds($row->id, true))) {
  115. $this->error('父角色不能是自身!');
  116. }
  117. $params['rules'] = explode(',', $params['rules']);
  118. $parentmodel = AuthGroupModel::get($params['parentid']);
  119. if (!$parentmodel) {
  120. $this->error('父组别未找到');
  121. }
  122. // 父级别的规则节点
  123. $parentrules = explode(',', $parentmodel->rules);
  124. // 当前组别的规则节点
  125. $currentrules = $this->auth->getRuleIds();
  126. $rules = $params['rules'];
  127. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  128. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  129. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  130. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  131. $params['rules'] = implode(',', $rules);
  132. if ($params) {
  133. Db::startTrans();
  134. try {
  135. $row->save($params);
  136. $children_auth_groups = model("AuthGroup")->all(['id' => ['in', implode(',', (Tree::instance()->getChildrenIds($row->id)))]]);
  137. $childparams = [];
  138. foreach ($children_auth_groups as $key => $children_auth_group) {
  139. $childparams[$key]['id'] = $children_auth_group->id;
  140. $childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
  141. }
  142. model("AuthGroup")->saveAll($childparams);
  143. Db::commit();
  144. } catch (Exception $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. $this->success('编辑成功');
  149. }
  150. $this->error('参数不能为空');
  151. }
  152. $this->assign("data", $row);
  153. return $this->fetch();
  154. }
  155. //访问授权页面
  156. public function access()
  157. {
  158. $id = $this->request->param('id/d');
  159. $pid = $this->request->param('pid/d');
  160. if (!in_array($id, $this->childrenGroupIds)) {
  161. $this->error('你没有权限访问!');
  162. }
  163. $row = $this->modelClass->get($id);
  164. if (!$row) {
  165. $this->error('记录未找到');
  166. }
  167. if ($this->request->isPost()) {
  168. $this->token();
  169. $params = $this->request->post("row/a", [], 'strip_tags');
  170. $params['rules'] = explode(',', $params['rules']);
  171. $parentmodel = $this->modelClass->get($params['parentid']);
  172. if (!$parentmodel) {
  173. $this->error('父组别未找到');
  174. }
  175. // 父级别的规则节点
  176. $parentrules = explode(',', $parentmodel->rules);
  177. // 当前组别的规则节点
  178. $currentrules = $this->auth->getRuleIds();
  179. $rules = $params['rules'];
  180. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  181. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  182. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  183. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  184. Db::startTrans();
  185. try {
  186. $row->rules = implode(',', $rules);
  187. $row->save();
  188. $children_auth_groups = model("AuthGroup")->all(['id' => ['in', implode(',', (Tree::instance()->getChildrenIds($row->id)))]]);
  189. $childparams = [];
  190. foreach ($children_auth_groups as $key => $children_auth_group) {
  191. $childparams[$key]['id'] = $children_auth_group->id;
  192. $childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
  193. }
  194. model("AuthGroup")->saveAll($childparams);
  195. Db::commit();
  196. } catch (Exception $e) {
  197. Db::rollback();
  198. $this->error($e->getMessage());
  199. }
  200. $this->success('编辑成功');
  201. }
  202. $parentGroupModel = $this->modelClass->get($pid);
  203. $ruleList = model('AuthRule')->order('listorder', 'desc')->order('id', 'asc')->select()->toArray();
  204. //读取父类角色所有节点列表
  205. $parentRuleList = [];
  206. if (in_array('*', explode(',', $parentGroupModel->rules))) {
  207. $parentRuleList = $ruleList;
  208. } else {
  209. $parentRuleIds = explode(',', $parentGroupModel->rules);
  210. foreach ($ruleList as $k => $v) {
  211. if (in_array($v['id'], $parentRuleIds)) {
  212. $parentRuleList[] = $v;
  213. }
  214. }
  215. }
  216. $ruleTree = new Tree();
  217. $groupTree = new Tree();
  218. //当前所有正常规则列表
  219. $ruleTree->init($parentRuleList);
  220. //角色组列表
  221. $groupTree->init(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select()->toArray());
  222. //读取当前角色下规则ID集合
  223. $adminRuleIds = $this->auth->getRuleIds();
  224. //是否是超级管理员
  225. $superadmin = $this->auth->isAdministrator();
  226. //当前拥有的规则ID集合
  227. $currentRuleIds = explode(',', $row->rules);
  228. $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
  229. /*$hasChildrens = [];
  230. foreach ($parentRuleList as $k => $v) {
  231. if ($v['haschild']) {
  232. $hasChildrens[] = $v['id'];
  233. }
  234. }*/
  235. $parentRuleIds = array_map(function ($item) {
  236. return $item['id'];
  237. }, $parentRuleList);
  238. $nodeList = [];
  239. foreach ($parentRuleList as $k => $v) {
  240. if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
  241. continue;
  242. }
  243. if ($v['parentid'] && !in_array($v['parentid'], $parentRuleIds)) {
  244. continue;
  245. }
  246. //$ischeck = in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens);
  247. $ischeck = in_array($v['id'], $currentRuleIds);
  248. $nodeList[] = ['id' => $v['id'], 'parentid' => $v['parentid'], 'name' => $v['title'], 'checked' => $ischeck];
  249. }
  250. //dump($nodeList);
  251. $this->assign('nodeList', json_encode($nodeList));
  252. $this->assign("data", $row);
  253. return $this->fetch();
  254. }
  255. //批量更新
  256. public function multi()
  257. {
  258. // 管理员禁止批量操作
  259. $this->error();
  260. }
  261. }