Group.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. return;
  76. }
  77. $parentmodel = model("AuthGroup")->get($params['pid']);
  78. if (!$parentmodel)
  79. {
  80. $this->code = -1;
  81. return;
  82. }
  83. // 父级别的规则节点
  84. $parentrules = explode(',', $parentmodel->rules);
  85. // 当前组别的规则节点
  86. $currentrules = $this->auth->getRuleIds();
  87. $rules = $params['rules'];
  88. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  89. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  90. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  91. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  92. $params['rules'] = implode(',', $rules);
  93. if ($params)
  94. {
  95. $this->model->create($params);
  96. $this->code = 1;
  97. }
  98. return;
  99. }
  100. return $this->view->fetch();
  101. }
  102. /**
  103. * 编辑
  104. */
  105. public function edit($ids = NULL)
  106. {
  107. $row = $this->model->get(['id' => $ids]);
  108. if (!$row)
  109. $this->error(__('No Results were found'));
  110. if ($this->request->isPost())
  111. {
  112. $this->code = -1;
  113. $params = $this->request->post("row/a");
  114. // 复节点不能是它自身的子节点
  115. if (!in_array($params['pid'], $this->childrenIds))
  116. {
  117. $this->code = -1;
  118. return;
  119. }
  120. $params['rules'] = explode(',', $params['rules']);
  121. $parentmodel = model("AuthGroup")->get($params['pid']);
  122. if (!$parentmodel)
  123. {
  124. $this->code = -1;
  125. return;
  126. }
  127. // 父级别的规则节点
  128. $parentrules = explode(',', $parentmodel->rules);
  129. // 当前组别的规则节点
  130. $currentrules = $this->auth->getRuleIds();
  131. $rules = $params['rules'];
  132. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  133. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  134. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  135. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  136. $params['rules'] = implode(',', $rules);
  137. if ($params)
  138. {
  139. $row->save($params);
  140. $this->code = 1;
  141. }
  142. return;
  143. }
  144. $this->view->assign("row", $row);
  145. return $this->view->fetch();
  146. }
  147. /**
  148. * 删除
  149. */
  150. public function del($ids = "")
  151. {
  152. $this->code = -1;
  153. if ($ids)
  154. {
  155. $ids = explode(',', $ids);
  156. $grouplist = $this->auth->getGroups();
  157. $group_ids = array_map(function($group)
  158. {
  159. return $group['id'];
  160. }, $grouplist);
  161. // 移除掉当前管理员所在组别
  162. $ids = array_diff($ids, $group_ids);
  163. // 循环判断每一个组别是否可删除
  164. $grouplist = $this->model->where('id', 'in', $ids)->select();
  165. $groupaccessmodel = model('AuthGroupAccess');
  166. foreach ($grouplist as $k => $v)
  167. {
  168. // 当前组别下有管理员
  169. $groupone = $groupaccessmodel->get(['group_id' => $v['id']]);
  170. if ($groupone)
  171. {
  172. $ids = array_diff($ids, [$v['id']]);
  173. continue;
  174. }
  175. // 当前组别下有子组别
  176. $groupone = $this->model->get(['pid' => $v['id']]);
  177. if ($groupone)
  178. {
  179. $ids = array_diff($ids, [$v['id']]);
  180. continue;
  181. }
  182. }
  183. $count = $this->model->where('id', 'in', $ids)->delete();
  184. if ($count)
  185. {
  186. $this->code = 1;
  187. }
  188. }
  189. return;
  190. }
  191. /**
  192. * 批量更新
  193. */
  194. public function multi($ids = "")
  195. {
  196. // 组别禁止批量操作
  197. $this->code = -1;
  198. return;
  199. }
  200. }