Group.php 10 KB

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