Group.php 9.9 KB

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