Group.php 11 KB

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