Group.php 9.4 KB

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