Group.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. namespace app\company\controller\auth;
  3. use app\company\model\AuthGroup;
  4. use app\common\controller\Apic;
  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 Apic
  15. {
  16. /**
  17. * @var \app\company\model\AuthGroup
  18. */
  19. protected $model = null;
  20. //当前登录管理员所有子组别
  21. protected $childrenGroupIds = [];
  22. //当前组别列表数据
  23. protected $grouplist = [];
  24. protected $groupdata = [];
  25. //无需要权限判断的方法
  26. protected $noNeedRight = ['info','simple_list','list_role_menus'];
  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,null,'');
  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. $list = $this->grouplist;
  65. foreach($list as $key => &$val){
  66. //是否是自己坐在的组
  67. $val['is_self'] = 0;
  68. $mygroupids = $this->auth->getGroupIds();
  69. if(in_array($val['id'],$mygroupids)){
  70. $val['is_self'] = 1;
  71. }
  72. //给前端用
  73. $val['parentId'] = $val['pid'];
  74. }
  75. // $total = count($list);
  76. // $result = array("total" => $total, "list" => $list);
  77. $this->success(1,$list);
  78. }
  79. /**
  80. * 添加
  81. */
  82. public function add()
  83. {
  84. $params = [
  85. 'company_id' => $this->auth->company_id,
  86. 'pid' => input('pid',0),
  87. 'name' => input('name',''),
  88. 'rules' => input('rules','','strip_tags'),
  89. 'status' => 'normal',
  90. ];
  91. $params['rules'] = explode(',', $params['rules']);
  92. if (!in_array($params['pid'], $this->childrenGroupIds)) {
  93. $this->error(__('The parent group exceeds permission limit'));
  94. }
  95. $parentmodel = model("AuthGroup")->get($params['pid']);
  96. if (!$parentmodel) {
  97. $this->error(__('The parent group can not found'));
  98. }
  99. // 父级别的规则节点
  100. $parentrules = explode(',', $parentmodel->rules);
  101. // 当前组别的规则节点
  102. $currentrules = $this->auth->getRuleIds();
  103. $rules = $params['rules'];
  104. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  105. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  106. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  107. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  108. $params['rules'] = implode(',', $rules);
  109. if ($params) {
  110. $this->model->create($params);
  111. $this->success();
  112. }
  113. $this->error();
  114. }
  115. /**
  116. * 编辑
  117. */
  118. public function info()
  119. {
  120. $ids = input('id','');
  121. if (!in_array($ids, $this->childrenGroupIds)) {
  122. $this->error(__('You have no permission'));
  123. }
  124. $row = $this->model->get(['id' => $ids]);
  125. if (!$row) {
  126. $this->error(__('No Results were found'));
  127. }
  128. $this->success(1,$row);
  129. }
  130. public function edit()
  131. {
  132. $ids = input('id','');
  133. if (!in_array($ids, $this->childrenGroupIds)) {
  134. $this->error(__('You have no permission1'));
  135. }
  136. $row = $this->model->get(['id' => $ids]);
  137. if (!$row) {
  138. $this->error(__('No Results were found'));
  139. }
  140. $params = [
  141. 'pid' => input('pid',0),
  142. 'name' => input('name',''),
  143. 'rules' => input('rules','','strip_tags'),
  144. 'status' => 'normal',
  145. ];
  146. //父节点不能是非权限内节点
  147. if (!in_array($params['pid'], $this->childrenGroupIds)) {
  148. $this->error(__('The parent group exceeds permission limit'));
  149. }
  150. // 父节点不能是它自身的子节点或自己本身
  151. if (in_array($params['pid'], Tree::instance()->getChildrenIds($row->id, true))) {
  152. $this->error(__('The parent group can not be its own child or itself'));
  153. }
  154. $params['rules'] = explode(',', $params['rules']);
  155. $parentmodel = model("AuthGroup")->get($params['pid']);
  156. if (!$parentmodel) {
  157. $this->error(__('The parent group can not found'));
  158. }
  159. // 父级别的规则节点
  160. $parentrules = explode(',', $parentmodel->rules);
  161. // 当前组别的规则节点
  162. $currentrules = $this->auth->getRuleIds();
  163. $rules = $params['rules'];
  164. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  165. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  166. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  167. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  168. $params['rules'] = implode(',', $rules);
  169. if ($params) {
  170. Db::startTrans();
  171. try {
  172. $row->save($params);
  173. $children_auth_groups = model("AuthGroup")->all(['id' => ['in', implode(',', (Tree::instance()->getChildrenIds($row->id)))]]);
  174. $childparams = [];
  175. foreach ($children_auth_groups as $key => $children_auth_group) {
  176. $childparams[$key]['id'] = $children_auth_group->id;
  177. $childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
  178. }
  179. model("AuthGroup")->saveAll($childparams);
  180. Db::commit();
  181. $this->success();
  182. } catch (Exception $e) {
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. }
  186. }
  187. $this->error();
  188. }
  189. /**
  190. * 删除
  191. */
  192. public function del()
  193. {
  194. $ids = input('id','');
  195. if ($ids) {
  196. $ids = explode(',', $ids);
  197. $grouplist = $this->auth->getGroups();
  198. $group_ids = array_map(function ($group) {
  199. return $group['id'];
  200. }, $grouplist);
  201. // 移除掉当前管理员所在组别
  202. $ids = array_diff($ids, $group_ids);
  203. // 循环判断每一个组别是否可删除
  204. $grouplist = $this->model->where('id', 'in', $ids)->select();
  205. $groupaccessmodel = model('AuthGroupAccess');
  206. foreach ($grouplist as $k => $v) {
  207. // 当前组别下有管理员
  208. $groupone = $groupaccessmodel->get(['group_id' => $v['id']]);
  209. if ($groupone) {
  210. $ids = array_diff($ids, [$v['id']]);
  211. continue;
  212. }
  213. // 当前组别下有子组别
  214. $groupone = $this->model->get(['pid' => $v['id']]);
  215. if ($groupone) {
  216. $ids = array_diff($ids, [$v['id']]);
  217. continue;
  218. }
  219. }
  220. if (!$ids) {
  221. $this->error(__('You can not delete group that contain child group and administrators'));
  222. }
  223. $count = $this->model->where('id', 'in', $ids)->delete();
  224. if ($count) {
  225. $this->success();
  226. }
  227. }
  228. $this->error();
  229. }
  230. /**
  231. * 批量更新
  232. * @internal
  233. */
  234. public function multi($ids = "")
  235. {
  236. // 组别禁止批量操作
  237. $this->error();
  238. }
  239. /**
  240. * 读取角色权限树
  241. *
  242. * @internal
  243. */
  244. //来自admin/auth/group/roletree
  245. public function simple_list()
  246. {
  247. $this->loadlang('auth/group');
  248. $model = model('AuthGroup');
  249. $id = $this->request->post("id");
  250. $pid = $this->request->post("pid");
  251. $parentGroupModel = $model->get($pid);
  252. $currentGroupModel = null;
  253. if ($id) {
  254. $currentGroupModel = $model->get($id);
  255. }
  256. if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
  257. $id = $id ? $id : null;
  258. $ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
  259. //读取父类角色所有节点列表
  260. $parentRuleList = [];
  261. if (in_array('*', explode(',', $parentGroupModel->rules))) {
  262. $parentRuleList = $ruleList;
  263. } else {
  264. $parentRuleIds = explode(',', $parentGroupModel->rules);
  265. foreach ($ruleList as $k => $v) {
  266. if (in_array($v['id'], $parentRuleIds)) {
  267. $parentRuleList[] = $v;
  268. }
  269. }
  270. }
  271. $ruleTree = new Tree();
  272. $groupTree = new Tree();
  273. //当前所有正常规则列表
  274. $ruleTree->init($parentRuleList);
  275. //角色组列表
  276. $groupTree->init(collection(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select())->toArray());
  277. //读取当前角色下规则ID集合
  278. $adminRuleIds = $this->auth->getRuleIds();
  279. //是否是超级管理员
  280. $superadmin = $this->auth->isSuperAdmin();
  281. //当前拥有的规则ID集合
  282. $currentRuleIds = $id ? explode(',', $currentGroupModel->rules) : [];
  283. if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
  284. $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
  285. $hasChildrens = [];
  286. foreach ($parentRuleList as $k => $v) {
  287. if ($v['haschild']) {
  288. $hasChildrens[] = $v['id'];
  289. }
  290. }
  291. $parentRuleIds = array_map(function ($item) {
  292. return $item['id'];
  293. }, $parentRuleList);
  294. $nodeList = [];
  295. $result = [];
  296. foreach ($parentRuleList as $k => $v) {
  297. if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
  298. continue;
  299. }
  300. if ($v['pid'] && !in_array($v['pid'], $parentRuleIds)) {
  301. continue;
  302. }
  303. $state = array('selected' => in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens));
  304. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
  305. $result[] = array('id' => $v['id'], 'parentId' => $v['pid'] ? $v['pid'] : 0, 'name' => $v['title'], 'type' => $v['type']);
  306. }
  307. $this->success(1, $result);
  308. } else {
  309. $this->error(__('Can not change the parent to child'));
  310. }
  311. } else {
  312. $this->error(__('Group not found'));
  313. }
  314. }
  315. //来自admin/auth/group/roletree
  316. public function list_role_menus()
  317. {
  318. $this->loadlang('auth/group');
  319. $model = model('AuthGroup');
  320. $id = $this->request->post("id");
  321. $pid = $this->request->post("pid");
  322. $parentGroupModel = $model->get($pid);
  323. $currentGroupModel = null;
  324. if ($id) {
  325. $currentGroupModel = $model->get($id);
  326. }
  327. if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
  328. $id = $id ? $id : null;
  329. $ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
  330. //读取父类角色所有节点列表
  331. $parentRuleList = [];
  332. if (in_array('*', explode(',', $parentGroupModel->rules))) {
  333. $parentRuleList = $ruleList;
  334. } else {
  335. $parentRuleIds = explode(',', $parentGroupModel->rules);
  336. foreach ($ruleList as $k => $v) {
  337. if (in_array($v['id'], $parentRuleIds)) {
  338. $parentRuleList[] = $v;
  339. }
  340. }
  341. }
  342. $ruleTree = new Tree();
  343. $groupTree = new Tree();
  344. //当前所有正常规则列表
  345. $ruleTree->init($parentRuleList);
  346. //角色组列表
  347. $groupTree->init(collection(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select())->toArray());
  348. //读取当前角色下规则ID集合
  349. $adminRuleIds = $this->auth->getRuleIds();
  350. //是否是超级管理员
  351. $superadmin = $this->auth->isSuperAdmin();
  352. //当前拥有的规则ID集合
  353. $currentRuleIds = $id ? explode(',', $currentGroupModel->rules) : [];
  354. if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
  355. $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
  356. $hasChildrens = [];
  357. foreach ($parentRuleList as $k => $v) {
  358. if ($v['haschild']) {
  359. $hasChildrens[] = $v['id'];
  360. }
  361. }
  362. $parentRuleIds = array_map(function ($item) {
  363. return $item['id'];
  364. }, $parentRuleList);
  365. $nodeList = [];
  366. $selected = [];//需要返回的东西
  367. foreach ($parentRuleList as $k => $v) {
  368. if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
  369. continue;
  370. }
  371. if ($v['pid'] && !in_array($v['pid'], $parentRuleIds)) {
  372. continue;
  373. }
  374. $state = array('selected' => in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens));
  375. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
  376. //需要返回的东西
  377. if(in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens)){
  378. $selected[] = $v['id'];
  379. }
  380. }
  381. $this->success(1, $selected);
  382. } else {
  383. $this->error(__('Can not change the parent to child'));
  384. }
  385. } else {
  386. $this->error(__('Group not found'));
  387. }
  388. }
  389. }