Group.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 exceeds permission limit'));
  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. if (!in_array($ids, $this->childrenGroupIds)) {
  115. $this->error(__('You have no permission'));
  116. }
  117. $row = $this->model->get(['id' => $ids]);
  118. if (!$row) {
  119. $this->error(__('No Results were found'));
  120. }
  121. if ($this->request->isPost()) {
  122. $this->token();
  123. $params = $this->request->post("row/a", [], 'strip_tags');
  124. //父节点不能是非权限内节点
  125. if (!in_array($params['pid'], $this->childrenGroupIds)) {
  126. $this->error(__('The parent group exceeds permission limit'));
  127. }
  128. // 父节点不能是它自身的子节点或自己本身
  129. if (in_array($params['pid'], Tree::instance()->getChildrenIds($row->id,true))){
  130. $this->error(__('The parent group can not be its own child or itself'));
  131. }
  132. $params['rules'] = explode(',', $params['rules']);
  133. $parentmodel = model("AuthGroup")->get($params['pid']);
  134. if (!$parentmodel) {
  135. $this->error(__('The parent group can not found'));
  136. }
  137. // 父级别的规则节点
  138. $parentrules = explode(',', $parentmodel->rules);
  139. // 当前组别的规则节点
  140. $currentrules = $this->auth->getRuleIds();
  141. $rules = $params['rules'];
  142. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  143. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  144. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  145. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  146. $params['rules'] = implode(',', $rules);
  147. if ($params) {
  148. Db::startTrans();
  149. try {
  150. $row->save($params);
  151. $children_auth_groups = model("AuthGroup")->all(['id'=>['in',implode(',',(Tree::instance()->getChildrenIds($row->id)))]]);
  152. $childparams = [];
  153. foreach ($children_auth_groups as $key=>$children_auth_group) {
  154. $childparams[$key]['id'] = $children_auth_group->id;
  155. $childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
  156. }
  157. model("AuthGroup")->saveAll($childparams);
  158. Db::commit();
  159. $this->success();
  160. }catch (Exception $e){
  161. Db::rollback();
  162. $this->error($e->getMessage());
  163. }
  164. }
  165. $this->error();
  166. return;
  167. }
  168. $this->view->assign("row", $row);
  169. return $this->view->fetch();
  170. }
  171. /**
  172. * 删除
  173. */
  174. public function del($ids = "")
  175. {
  176. if ($ids) {
  177. $ids = explode(',', $ids);
  178. $grouplist = $this->auth->getGroups();
  179. $group_ids = array_map(function ($group) {
  180. return $group['id'];
  181. }, $grouplist);
  182. // 移除掉当前管理员所在组别
  183. $ids = array_diff($ids, $group_ids);
  184. // 循环判断每一个组别是否可删除
  185. $grouplist = $this->model->where('id', 'in', $ids)->select();
  186. $groupaccessmodel = model('AuthGroupAccess');
  187. foreach ($grouplist as $k => $v) {
  188. // 当前组别下有管理员
  189. $groupone = $groupaccessmodel->get(['group_id' => $v['id']]);
  190. if ($groupone) {
  191. $ids = array_diff($ids, [$v['id']]);
  192. continue;
  193. }
  194. // 当前组别下有子组别
  195. $groupone = $this->model->get(['pid' => $v['id']]);
  196. if ($groupone) {
  197. $ids = array_diff($ids, [$v['id']]);
  198. continue;
  199. }
  200. }
  201. if (!$ids) {
  202. $this->error(__('You can not delete group that contain child group and administrators'));
  203. }
  204. $count = $this->model->where('id', 'in', $ids)->delete();
  205. if ($count) {
  206. $this->success();
  207. }
  208. }
  209. $this->error();
  210. }
  211. /**
  212. * 批量更新
  213. * @internal
  214. */
  215. public function multi($ids = "")
  216. {
  217. // 组别禁止批量操作
  218. $this->error();
  219. }
  220. /**
  221. * 读取角色权限树
  222. *
  223. * @internal
  224. */
  225. public function roletree()
  226. {
  227. $this->loadlang('auth/group');
  228. $model = model('AuthGroup');
  229. $id = $this->request->post("id");
  230. $pid = $this->request->post("pid");
  231. $parentGroupModel = $model->get($pid);
  232. $currentGroupModel = null;
  233. if ($id) {
  234. $currentGroupModel = $model->get($id);
  235. }
  236. if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
  237. $id = $id ? $id : null;
  238. $ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
  239. //读取父类角色所有节点列表
  240. $parentRuleList = [];
  241. if (in_array('*', explode(',', $parentGroupModel->rules))) {
  242. $parentRuleList = $ruleList;
  243. } else {
  244. $parentRuleIds = explode(',', $parentGroupModel->rules);
  245. foreach ($ruleList as $k => $v) {
  246. if (in_array($v['id'], $parentRuleIds)) {
  247. $parentRuleList[] = $v;
  248. }
  249. }
  250. }
  251. $ruleTree = new Tree();
  252. $groupTree = new Tree();
  253. //当前所有正常规则列表
  254. $ruleTree->init($parentRuleList);
  255. //角色组列表
  256. $groupTree->init(collection(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select())->toArray());
  257. //读取当前角色下规则ID集合
  258. $adminRuleIds = $this->auth->getRuleIds();
  259. //是否是超级管理员
  260. $superadmin = $this->auth->isSuperAdmin();
  261. //当前拥有的规则ID集合
  262. $currentRuleIds = $id ? explode(',', $currentGroupModel->rules) : [];
  263. if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
  264. $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
  265. $hasChildrens = [];
  266. foreach ($parentRuleList as $k => $v) {
  267. if ($v['haschild']) {
  268. $hasChildrens[] = $v['id'];
  269. }
  270. }
  271. $parentRuleIds = array_map(function ($item) {
  272. return $item['id'];
  273. }, $parentRuleList);
  274. $nodeList = [];
  275. foreach ($parentRuleList as $k => $v) {
  276. if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
  277. continue;
  278. }
  279. if ($v['pid'] && !in_array($v['pid'], $parentRuleIds)) {
  280. continue;
  281. }
  282. $state = array('selected' => in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens));
  283. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
  284. }
  285. $this->success('', null, $nodeList);
  286. } else {
  287. $this->error(__('Can not change the parent to child'));
  288. }
  289. } else {
  290. $this->error(__('Group not found'));
  291. }
  292. }
  293. }