Group.php 13 KB

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