Group.php 11 KB

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