Group.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 角色组
  7. *
  8. * @icon fa fa-group
  9. * @remark 角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别下级的角色组或管理员
  10. */
  11. class Group extends Backend
  12. {
  13. protected $model = null;
  14. //当前登录管理员所有子节点组别
  15. protected $childrenIds = [];
  16. //当前组别列表数据
  17. protected $groupdata = [];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('AuthGroup');
  22. $groups = $this->auth->getGroups();
  23. // 取出所有分组
  24. $grouplist = model('AuthGroup')->all(['status' => 'normal']);
  25. $objlist = [];
  26. foreach ($groups as $K => $v)
  27. {
  28. // 取出包含自己的所有子节点
  29. $childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);
  30. $obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);
  31. $objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));
  32. }
  33. $groupdata = [];
  34. foreach ($objlist as $k => $v)
  35. {
  36. $groupdata[$v['id']] = $v['name'];
  37. }
  38. $this->groupdata = $groupdata;
  39. $this->childrenIds = array_keys($groupdata);
  40. $this->view->assign('groupdata', $groupdata);
  41. }
  42. /**
  43. * 查看
  44. */
  45. public function index()
  46. {
  47. if ($this->request->isAjax())
  48. {
  49. $list = [];
  50. foreach ($this->groupdata as $k => $v)
  51. {
  52. $data = $this->model->get($k);
  53. $data->name = $v;
  54. $list[] = $data;
  55. }
  56. $total = count($list);
  57. $result = array("total" => $total, "rows" => $list);
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 添加
  64. */
  65. public function add()
  66. {
  67. if ($this->request->isPost())
  68. {
  69. $this->code = -1;
  70. $params = $this->request->post("row/a");
  71. $params['rules'] = explode(',', $params['rules']);
  72. if (!in_array($params['pid'], $this->childrenIds))
  73. {
  74. $this->code = -1;
  75. $this->msg = __('');
  76. return;
  77. }
  78. $parentmodel = model("AuthGroup")->get($params['pid']);
  79. if (!$parentmodel)
  80. {
  81. $this->msg = __('The parent group can not found');
  82. return;
  83. }
  84. // 父级别的规则节点
  85. $parentrules = explode(',', $parentmodel->rules);
  86. // 当前组别的规则节点
  87. $currentrules = $this->auth->getRuleIds();
  88. $rules = $params['rules'];
  89. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  90. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  91. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  92. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  93. $params['rules'] = implode(',', $rules);
  94. if ($params)
  95. {
  96. $this->model->create($params);
  97. $this->code = 1;
  98. }
  99. return;
  100. }
  101. return $this->view->fetch();
  102. }
  103. /**
  104. * 编辑
  105. */
  106. public function edit($ids = NULL)
  107. {
  108. $row = $this->model->get(['id' => $ids]);
  109. if (!$row)
  110. $this->error(__('No Results were found'));
  111. if ($this->request->isPost())
  112. {
  113. $this->code = -1;
  114. $params = $this->request->post("row/a");
  115. // 父节点不能是它自身的子节点
  116. if (!in_array($params['pid'], $this->childrenIds))
  117. {
  118. $this->msg = __('The parent group can not be its own child');
  119. return;
  120. }
  121. $params['rules'] = explode(',', $params['rules']);
  122. $parentmodel = model("AuthGroup")->get($params['pid']);
  123. if (!$parentmodel)
  124. {
  125. $this->msg = __('The parent group can not found');
  126. return;
  127. }
  128. // 父级别的规则节点
  129. $parentrules = explode(',', $parentmodel->rules);
  130. // 当前组别的规则节点
  131. $currentrules = $this->auth->getRuleIds();
  132. $rules = $params['rules'];
  133. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  134. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  135. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  136. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  137. $params['rules'] = implode(',', $rules);
  138. if ($params)
  139. {
  140. $row->save($params);
  141. $this->code = 1;
  142. }
  143. return;
  144. }
  145. $this->view->assign("row", $row);
  146. return $this->view->fetch();
  147. }
  148. /**
  149. * 删除
  150. */
  151. public function del($ids = "")
  152. {
  153. $this->code = -1;
  154. if ($ids)
  155. {
  156. $ids = explode(',', $ids);
  157. $grouplist = $this->auth->getGroups();
  158. $group_ids = array_map(function($group)
  159. {
  160. return $group['id'];
  161. }, $grouplist);
  162. // 移除掉当前管理员所在组别
  163. $ids = array_diff($ids, $group_ids);
  164. // 循环判断每一个组别是否可删除
  165. $grouplist = $this->model->where('id', 'in', $ids)->select();
  166. $groupaccessmodel = model('AuthGroupAccess');
  167. foreach ($grouplist as $k => $v)
  168. {
  169. // 当前组别下有管理员
  170. $groupone = $groupaccessmodel->get(['group_id' => $v['id']]);
  171. if ($groupone)
  172. {
  173. $ids = array_diff($ids, [$v['id']]);
  174. continue;
  175. }
  176. // 当前组别下有子组别
  177. $groupone = $this->model->get(['pid' => $v['id']]);
  178. if ($groupone)
  179. {
  180. $ids = array_diff($ids, [$v['id']]);
  181. continue;
  182. }
  183. }
  184. if (!$ids)
  185. {
  186. $this->msg = __('You can not delete group that contain child group and administrators');
  187. return;
  188. }
  189. $count = $this->model->where('id', 'in', $ids)->delete();
  190. if ($count)
  191. {
  192. $this->code = 1;
  193. }
  194. }
  195. return;
  196. }
  197. /**
  198. * 批量更新
  199. * @internal
  200. */
  201. public function multi($ids = "")
  202. {
  203. // 组别禁止批量操作
  204. $this->code = -1;
  205. return;
  206. }
  207. }