Group.php 9.5 KB

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