Group.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 角色组
  10. *
  11. * @icon fa fa-group
  12. * @remark 角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别下级的角色组或管理员
  13. */
  14. class Group extends Backend
  15. {
  16. /**
  17. * @var \app\admin\model\AuthGroup
  18. */
  19. protected $model = null;
  20. //当前登录管理员所有子组别
  21. protected $childrenGroupIds = [];
  22. //当前组别列表数据
  23. protected $grouplist = [];
  24. protected $groupdata = [];
  25. //无需要权限判断的方法
  26. protected $noNeedRight = ['roletree'];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = model('AuthGroup');
  31. $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  32. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  33. Tree::instance()->init($groupList);
  34. $groupList = [];
  35. if ($this->auth->isSuperAdmin()) {
  36. $groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  37. } else {
  38. $groups = $this->auth->getGroups();
  39. $groupIds = [];
  40. foreach ($groups as $m => $n) {
  41. if (in_array($n['id'], $groupIds) || in_array($n['pid'], $groupIds)) {
  42. continue;
  43. }
  44. $groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid'])));
  45. foreach ($groupList as $index => $item) {
  46. $groupIds[] = $item['id'];
  47. }
  48. }
  49. }
  50. $groupName = [];
  51. foreach ($groupList as $k => $v) {
  52. $groupName[$v['id']] = $v['name'];
  53. }
  54. $this->grouplist = $groupList;
  55. $this->groupdata = $groupName;
  56. $this->assignconfig("admin", ['id' => $this->auth->id, 'group_ids' => $this->auth->getGroupIds()]);
  57. $this->view->assign('groupdata', $this->groupdata);
  58. }
  59. /**
  60. * 查看
  61. */
  62. public function index()
  63. {
  64. if ($this->request->isAjax()) {
  65. $list = $this->grouplist;
  66. $total = count($list);
  67. $result = array("total" => $total, "rows" => $list);
  68. return json($result);
  69. }
  70. return $this->view->fetch();
  71. }
  72. /**
  73. * 添加
  74. */
  75. public function add()
  76. {
  77. if ($this->request->isPost()) {
  78. $this->token();
  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 exceeds permission limit'));
  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. if (!in_array($ids, $this->childrenGroupIds)) {
  112. $this->error(__('You have no permission'));
  113. }
  114. $row = $this->model->get(['id' => $ids]);
  115. if (!$row) {
  116. $this->error(__('No Results were found'));
  117. }
  118. if ($this->request->isPost()) {
  119. $this->token();
  120. $params = $this->request->post("row/a", [], 'strip_tags');
  121. //父节点不能是非权限内节点
  122. if (!in_array($params['pid'], $this->childrenGroupIds)) {
  123. $this->error(__('The parent group exceeds permission limit'));
  124. }
  125. // 父节点不能是它自身的子节点或自己本身
  126. if (in_array($params['pid'], Tree::instance()->getChildrenIds($row->id, true))) {
  127. $this->error(__('The parent group can not be its own child or itself'));
  128. }
  129. $params['rules'] = explode(',', $params['rules']);
  130. $parentmodel = model("AuthGroup")->get($params['pid']);
  131. if (!$parentmodel) {
  132. $this->error(__('The parent group can not found'));
  133. }
  134. // 父级别的规则节点
  135. $parentrules = explode(',', $parentmodel->rules);
  136. // 当前组别的规则节点
  137. $currentrules = $this->auth->getRuleIds();
  138. $rules = $params['rules'];
  139. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  140. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  141. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  142. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  143. $params['rules'] = implode(',', $rules);
  144. if ($params) {
  145. Db::startTrans();
  146. try {
  147. $row->save($params);
  148. $children_auth_groups = model("AuthGroup")->all(['id' => ['in', implode(',', (Tree::instance()->getChildrenIds($row->id)))]]);
  149. $childparams = [];
  150. foreach ($children_auth_groups as $key => $children_auth_group) {
  151. $childparams[$key]['id'] = $children_auth_group->id;
  152. $childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
  153. }
  154. model("AuthGroup")->saveAll($childparams);
  155. Db::commit();
  156. $this->success();
  157. } catch (Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. }
  162. $this->error();
  163. return;
  164. }
  165. $this->view->assign("row", $row);
  166. return $this->view->fetch();
  167. }
  168. /**
  169. * 删除
  170. */
  171. public function del($ids = "")
  172. {
  173. if (!$this->request->isPost()) {
  174. $this->error(__("Invalid parameters"));
  175. }
  176. $ids = $ids ? $ids : $this->request->post("ids");
  177. if ($ids) {
  178. $ids = explode(',', $ids);
  179. $grouplist = $this->auth->getGroups();
  180. $group_ids = array_map(function ($group) {
  181. return $group['id'];
  182. }, $grouplist);
  183. // 移除掉当前管理员所在组别
  184. $ids = array_diff($ids, $group_ids);
  185. // 循环判断每一个组别是否可删除
  186. $grouplist = $this->model->where('id', 'in', $ids)->select();
  187. $groupaccessmodel = model('AuthGroupAccess');
  188. foreach ($grouplist as $k => $v) {
  189. // 当前组别下有管理员
  190. $groupone = $groupaccessmodel->get(['group_id' => $v['id']]);
  191. if ($groupone) {
  192. $ids = array_diff($ids, [$v['id']]);
  193. continue;
  194. }
  195. // 当前组别下有子组别
  196. $groupone = $this->model->get(['pid' => $v['id']]);
  197. if ($groupone) {
  198. $ids = array_diff($ids, [$v['id']]);
  199. continue;
  200. }
  201. }
  202. if (!$ids) {
  203. $this->error(__('You can not delete group that contain child group and administrators'));
  204. }
  205. $count = $this->model->where('id', 'in', $ids)->delete();
  206. if ($count) {
  207. $this->success();
  208. }
  209. }
  210. $this->error();
  211. }
  212. /**
  213. * 批量更新
  214. * @internal
  215. */
  216. public function multi($ids = "")
  217. {
  218. // 组别禁止批量操作
  219. $this->error();
  220. }
  221. /**
  222. * 读取角色权限树
  223. *
  224. * @internal
  225. */
  226. public function roletree()
  227. {
  228. $this->loadlang('auth/group');
  229. $model = model('AuthGroup');
  230. $id = $this->request->post("id");
  231. $pid = $this->request->post("pid");
  232. $parentGroupModel = $model->get($pid);
  233. $currentGroupModel = null;
  234. if ($id) {
  235. $currentGroupModel = $model->get($id);
  236. }
  237. if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
  238. $id = $id ? $id : null;
  239. $ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
  240. //读取父类角色所有节点列表
  241. $parentRuleList = [];
  242. if (in_array('*', explode(',', $parentGroupModel->rules))) {
  243. $parentRuleList = $ruleList;
  244. } else {
  245. $parentRuleIds = explode(',', $parentGroupModel->rules);
  246. foreach ($ruleList as $k => $v) {
  247. if (in_array($v['id'], $parentRuleIds)) {
  248. $parentRuleList[] = $v;
  249. }
  250. }
  251. }
  252. $ruleTree = new Tree();
  253. $groupTree = new Tree();
  254. //当前所有正常规则列表
  255. $ruleTree->init($parentRuleList);
  256. //角色组列表
  257. $groupTree->init(collection(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select())->toArray());
  258. //读取当前角色下规则ID集合
  259. $adminRuleIds = $this->auth->getRuleIds();
  260. //是否是超级管理员
  261. $superadmin = $this->auth->isSuperAdmin();
  262. //当前拥有的规则ID集合
  263. $currentRuleIds = $id ? explode(',', $currentGroupModel->rules) : [];
  264. if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
  265. $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
  266. $hasChildrens = [];
  267. foreach ($parentRuleList as $k => $v) {
  268. if ($v['haschild']) {
  269. $hasChildrens[] = $v['id'];
  270. }
  271. }
  272. $parentRuleIds = array_map(function ($item) {
  273. return $item['id'];
  274. }, $parentRuleList);
  275. $nodeList = [];
  276. foreach ($parentRuleList as $k => $v) {
  277. if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
  278. continue;
  279. }
  280. if ($v['pid'] && !in_array($v['pid'], $parentRuleIds)) {
  281. continue;
  282. }
  283. $state = array('selected' => in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens));
  284. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
  285. }
  286. $this->success('', null, $nodeList);
  287. } else {
  288. $this->error(__('Can not change the parent to child'));
  289. }
  290. } else {
  291. $this->error(__('Group not found'));
  292. }
  293. }
  294. }