Group.php 16 KB

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