Group.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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'];
  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. * 批量更新
  233. * @internal
  234. */
  235. public function multi($ids = "")
  236. {
  237. // 组别禁止批量操作
  238. $this->error();
  239. }
  240. /**
  241. * 读取角色权限树
  242. *
  243. * @internal
  244. */
  245. //来自admin/auth/group/roletree
  246. public function simple_list()
  247. {
  248. $this->loadlang('auth/group');
  249. $model = model('AuthGroup');
  250. $id = $this->request->post("id");
  251. $pid = $this->request->post("pid");
  252. $parentGroupModel = $model->get($pid);
  253. $currentGroupModel = null;
  254. if ($id) {
  255. $currentGroupModel = $model->get($id);
  256. }
  257. if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
  258. $id = $id ? $id : null;
  259. $ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
  260. //读取父类角色所有节点列表
  261. $parentRuleList = [];
  262. if (in_array('*', explode(',', $parentGroupModel->rules))) {
  263. $parentRuleList = $ruleList;
  264. } else {
  265. $parentRuleIds = explode(',', $parentGroupModel->rules);
  266. foreach ($ruleList as $k => $v) {
  267. if (in_array($v['id'], $parentRuleIds)) {
  268. $parentRuleList[] = $v;
  269. }
  270. }
  271. }
  272. $ruleTree = new Tree();
  273. $groupTree = new Tree();
  274. //当前所有正常规则列表
  275. $ruleTree->init($parentRuleList);
  276. //角色组列表
  277. $groupTree->init(collection(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select())->toArray());
  278. //读取当前角色下规则ID集合
  279. $adminRuleIds = $this->auth->getRuleIds();
  280. //是否是超级管理员
  281. $superadmin = $this->auth->isSuperAdmin();
  282. //当前拥有的规则ID集合
  283. $currentRuleIds = $id ? explode(',', $currentGroupModel->rules) : [];
  284. if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
  285. $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
  286. $hasChildrens = [];
  287. foreach ($parentRuleList as $k => $v) {
  288. if ($v['haschild']) {
  289. $hasChildrens[] = $v['id'];
  290. }
  291. }
  292. $parentRuleIds = array_map(function ($item) {
  293. return $item['id'];
  294. }, $parentRuleList);
  295. $nodeList = [];
  296. $result = [];
  297. foreach ($parentRuleList as $k => $v) {
  298. if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
  299. continue;
  300. }
  301. if ($v['pid'] && !in_array($v['pid'], $parentRuleIds)) {
  302. continue;
  303. }
  304. $state = array('selected' => in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens));
  305. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
  306. $result[] = array('id' => $v['id'], 'parentId' => $v['pid'] ? $v['pid'] : 0, 'name' => $v['title'], 'type' => $v['type']);
  307. }
  308. $this->success(1, $result);
  309. } else {
  310. $this->error(__('Can not change the parent to child'));
  311. }
  312. } else {
  313. $this->error(__('Group not found'));
  314. }
  315. }
  316. //来自admin/auth/group/roletree
  317. public function list_role_menus()
  318. {
  319. $this->loadlang('auth/group');
  320. $model = model('AuthGroup');
  321. $id = $this->request->post("id");
  322. $pid = $this->request->post("pid");
  323. $parentGroupModel = $model->get($pid);
  324. $currentGroupModel = null;
  325. if ($id) {
  326. $currentGroupModel = $model->get($id);
  327. }
  328. if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
  329. $id = $id ? $id : null;
  330. $ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
  331. //读取父类角色所有节点列表
  332. $parentRuleList = [];
  333. if (in_array('*', explode(',', $parentGroupModel->rules))) {
  334. $parentRuleList = $ruleList;
  335. } else {
  336. $parentRuleIds = explode(',', $parentGroupModel->rules);
  337. foreach ($ruleList as $k => $v) {
  338. if (in_array($v['id'], $parentRuleIds)) {
  339. $parentRuleList[] = $v;
  340. }
  341. }
  342. }
  343. $ruleTree = new Tree();
  344. $groupTree = new Tree();
  345. //当前所有正常规则列表
  346. $ruleTree->init($parentRuleList);
  347. //角色组列表
  348. $groupTree->init(collection(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select())->toArray());
  349. //读取当前角色下规则ID集合
  350. $adminRuleIds = $this->auth->getRuleIds();
  351. //是否是超级管理员
  352. $superadmin = $this->auth->isSuperAdmin();
  353. //当前拥有的规则ID集合
  354. $currentRuleIds = $id ? explode(',', $currentGroupModel->rules) : [];
  355. if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
  356. $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
  357. $hasChildrens = [];
  358. foreach ($parentRuleList as $k => $v) {
  359. if ($v['haschild']) {
  360. $hasChildrens[] = $v['id'];
  361. }
  362. }
  363. $parentRuleIds = array_map(function ($item) {
  364. return $item['id'];
  365. }, $parentRuleList);
  366. $nodeList = [];
  367. $selected = [];//需要返回的东西
  368. foreach ($parentRuleList as $k => $v) {
  369. if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
  370. continue;
  371. }
  372. if ($v['pid'] && !in_array($v['pid'], $parentRuleIds)) {
  373. continue;
  374. }
  375. $state = array('selected' => in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens));
  376. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
  377. //需要返回的东西
  378. if(in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens)){
  379. $selected[] = $v['id'];
  380. }
  381. }
  382. $this->success(1, $selected);
  383. } else {
  384. $this->error(__('Can not change the parent to child'));
  385. }
  386. } else {
  387. $this->error(__('Group not found'));
  388. }
  389. }
  390. }