Group.php 11 KB

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