Group.php 7.2 KB

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