Group.php 11 KB

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