Group.php 6.9 KB

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