Group.php 11 KB

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