Group.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 角色组
  7. *
  8. * @icon fa fa-group
  9. * @remark 角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别下级的角色组或管理员
  10. */
  11. class Group extends Backend
  12. {
  13. protected $model = null;
  14. //当前登录管理员所有子节点组别
  15. protected $childrenIds = [];
  16. //当前组别列表数据
  17. protected $groupdata = [];
  18. //无需要权限判断的方法
  19. protected $noNeedRight = ['roletree'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('AuthGroup');
  24. $groups = $this->auth->getGroups();
  25. // 取出所有分组
  26. $grouplist = model('AuthGroup')->all(['status' => 'normal']);
  27. $objlist = [];
  28. foreach ($groups as $K => $v)
  29. {
  30. // 取出包含自己的所有子节点
  31. $childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);
  32. $obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);
  33. $objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));
  34. }
  35. $groupdata = [];
  36. foreach ($objlist as $k => $v)
  37. {
  38. $groupdata[$v['id']] = $v['name'];
  39. }
  40. $this->groupdata = $groupdata;
  41. $this->childrenIds = array_keys($groupdata);
  42. $this->view->assign('groupdata', $groupdata);
  43. }
  44. /**
  45. * 查看
  46. */
  47. public function index()
  48. {
  49. if ($this->request->isAjax())
  50. {
  51. $list = [];
  52. foreach ($this->groupdata as $k => $v)
  53. {
  54. $data = $this->model->get($k);
  55. $data->name = $v;
  56. $list[] = $data;
  57. }
  58. $total = count($list);
  59. $result = array("total" => $total, "rows" => $list);
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 添加
  66. */
  67. public function add()
  68. {
  69. if ($this->request->isPost())
  70. {
  71. $params = $this->request->post("row/a", [], 'strip_tags');
  72. $params['rules'] = explode(',', $params['rules']);
  73. if (!in_array($params['pid'], $this->childrenIds))
  74. {
  75. $this->error(__('The parent group can not be its own child'));
  76. }
  77. $parentmodel = model("AuthGroup")->get($params['pid']);
  78. if (!$parentmodel)
  79. {
  80. $this->error(__('The parent group can not found'));
  81. }
  82. // 父级别的规则节点
  83. $parentrules = explode(',', $parentmodel->rules);
  84. // 当前组别的规则节点
  85. $currentrules = $this->auth->getRuleIds();
  86. $rules = $params['rules'];
  87. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  88. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  89. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  90. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  91. $params['rules'] = implode(',', $rules);
  92. if ($params)
  93. {
  94. $this->model->create($params);
  95. $this->success();
  96. }
  97. $this->error();
  98. }
  99. return $this->view->fetch();
  100. }
  101. /**
  102. * 编辑
  103. */
  104. public function edit($ids = NULL)
  105. {
  106. $row = $this->model->get(['id' => $ids]);
  107. if (!$row)
  108. $this->error(__('No Results were found'));
  109. if ($this->request->isPost())
  110. {
  111. $params = $this->request->post("row/a", [], 'strip_tags');
  112. // 父节点不能是它自身的子节点
  113. if (!in_array($params['pid'], $this->childrenIds))
  114. {
  115. $this->error(__('The parent group can not be its own child'));
  116. }
  117. $params['rules'] = explode(',', $params['rules']);
  118. $parentmodel = model("AuthGroup")->get($params['pid']);
  119. if (!$parentmodel)
  120. {
  121. $this->error(__('The parent group can not found'));
  122. }
  123. // 父级别的规则节点
  124. $parentrules = explode(',', $parentmodel->rules);
  125. // 当前组别的规则节点
  126. $currentrules = $this->auth->getRuleIds();
  127. $rules = $params['rules'];
  128. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  129. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  130. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  131. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  132. $params['rules'] = implode(',', $rules);
  133. if ($params)
  134. {
  135. $row->save($params);
  136. $this->success();
  137. }
  138. $this->error();
  139. return;
  140. }
  141. $this->view->assign("row", $row);
  142. return $this->view->fetch();
  143. }
  144. /**
  145. * 删除
  146. */
  147. public function del($ids = "")
  148. {
  149. if ($ids)
  150. {
  151. $ids = explode(',', $ids);
  152. $grouplist = $this->auth->getGroups();
  153. $group_ids = array_map(function($group) {
  154. return $group['id'];
  155. }, $grouplist);
  156. // 移除掉当前管理员所在组别
  157. $ids = array_diff($ids, $group_ids);
  158. // 循环判断每一个组别是否可删除
  159. $grouplist = $this->model->where('id', 'in', $ids)->select();
  160. $groupaccessmodel = model('AuthGroupAccess');
  161. foreach ($grouplist as $k => $v)
  162. {
  163. // 当前组别下有管理员
  164. $groupone = $groupaccessmodel->get(['group_id' => $v['id']]);
  165. if ($groupone)
  166. {
  167. $ids = array_diff($ids, [$v['id']]);
  168. continue;
  169. }
  170. // 当前组别下有子组别
  171. $groupone = $this->model->get(['pid' => $v['id']]);
  172. if ($groupone)
  173. {
  174. $ids = array_diff($ids, [$v['id']]);
  175. continue;
  176. }
  177. }
  178. if (!$ids)
  179. {
  180. $this->error(__('You can not delete group that contain child group and administrators'));
  181. }
  182. $count = $this->model->where('id', 'in', $ids)->delete();
  183. if ($count)
  184. {
  185. $this->success();
  186. }
  187. }
  188. $this->error();
  189. }
  190. /**
  191. * 批量更新
  192. * @internal
  193. */
  194. public function multi($ids = "")
  195. {
  196. // 组别禁止批量操作
  197. $this->error();
  198. }
  199. /**
  200. * 读取角色权限树
  201. *
  202. * @internal
  203. */
  204. public function roletree()
  205. {
  206. $this->loadlang('auth/group');
  207. $model = model('AuthGroup');
  208. $id = $this->request->post("id");
  209. $pid = $this->request->post("pid");
  210. $parentgroupmodel = $model->get($pid);
  211. $currentgroupmodel = NULL;
  212. if ($id)
  213. {
  214. $currentgroupmodel = $model->get($id);
  215. }
  216. if (($pid || $parentgroupmodel) && (!$id || $currentgroupmodel))
  217. {
  218. $id = $id ? $id : NULL;
  219. $ruleList = collection(model('AuthRule')->order('weigh', 'desc')->select())->toArray();
  220. //读取父类角色所有节点列表
  221. $parentRuleList = [];
  222. if (in_array('*', explode(',', $parentgroupmodel->rules)))
  223. {
  224. $parentRuleList = $ruleList;
  225. }
  226. else
  227. {
  228. $parent_rule_ids = explode(',', $parentgroupmodel->rules);
  229. foreach ($ruleList as $k => $v)
  230. {
  231. if (in_array($v['id'], $parent_rule_ids))
  232. {
  233. $parentRuleList[] = $v;
  234. }
  235. }
  236. }
  237. //当前所有正常规则列表
  238. Tree::instance()->init($ruleList);
  239. //读取当前角色下规则ID集合
  240. $admin_rule_ids = $this->auth->getRuleIds();
  241. //是否是超级管理员
  242. $superadmin = $this->auth->isSuperAdmin();
  243. //当前拥有的规则ID集合
  244. $current_rule_ids = $id ? explode(',', $currentgroupmodel->rules) : [];
  245. if (!$id || !in_array($pid, Tree::instance()->getChildrenIds($id, TRUE)))
  246. {
  247. $ruleList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  248. $hasChildrens = [];
  249. foreach ($ruleList as $k => $v)
  250. {
  251. if ($v['haschild'])
  252. $hasChildrens[] = $v['id'];
  253. }
  254. $nodelist = [];
  255. foreach ($parentRuleList as $k => $v)
  256. {
  257. if (!$superadmin && !in_array($v['id'], $admin_rule_ids))
  258. continue;
  259. $state = array('selected' => in_array($v['id'], $current_rule_ids) && !in_array($v['id'], $hasChildrens));
  260. $nodelist[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => $v['title'], 'type' => 'menu', 'state' => $state);
  261. }
  262. $this->success('',null,$nodelist);
  263. }
  264. else
  265. {
  266. $this->error(__('Can not change the parent to child'));
  267. }
  268. }
  269. else
  270. {
  271. $this->error(__('Group not found'));
  272. }
  273. }
  274. }