Group.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 权限管理控制器
  15. // +----------------------------------------------------------------------
  16. namespace app\admin\Controller\auth;
  17. use app\admin\model\AuthGroupAccess;
  18. use app\admin\model\AuthGroup as AuthGroupModel;
  19. use app\admin\model\AuthRule as AuthRuleModel;
  20. use app\common\controller\Backend;
  21. use think\exception\ValidateException;
  22. use think\facade\Db;
  23. use util\Tree;
  24. class Group extends Backend
  25. {
  26. //当前登录管理员所有子组别
  27. protected $childrenGroupIds = [];
  28. //当前组别列表数据
  29. protected $grouplist = [];
  30. protected $groupdata = [];
  31. //无需要权限判断的方法
  32. protected $noNeedRight = ['roletree'];
  33. protected function initialize()
  34. {
  35. parent::initialize();
  36. $this->modelClass = new AuthGroupModel;
  37. $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  38. $groupList = AuthGroupModel::where('id', 'in', $this->childrenGroupIds)->select()->toArray();
  39. Tree::instance()->init($groupList);
  40. $groupList = [];
  41. if ($this->auth->isAdministrator()) {
  42. $groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  43. } else {
  44. $groups = $this->auth->getGroups();
  45. $groupIds = [];
  46. foreach ($groups as $m => $n) {
  47. if (in_array($n['id'], $groupIds) || in_array($n['parentid'], $groupIds)) {
  48. continue;
  49. }
  50. $groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['parentid']), 'title'));
  51. foreach ($groupList as $index => $item) {
  52. $groupIds[] = $item['id'];
  53. }
  54. }
  55. }
  56. $groupName = [];
  57. foreach ($groupList as $k => $v) {
  58. $groupName[$v['id']] = $v['title'];
  59. }
  60. $this->grouplist = $groupList;
  61. $this->groupdata = $groupName;
  62. $this->assignconfig("admin", ['id' => $this->auth->id, 'group_ids' => $this->auth->getGroupIds()]);
  63. $this->assign('groupdata', $this->groupdata);
  64. }
  65. //权限管理首页
  66. public function index()
  67. {
  68. if ($this->request->isAjax()) {
  69. $list = $this->grouplist;
  70. $total = count($list);
  71. $result = ["code" => 0, "count" => $total, "data" => $list];
  72. return json($result);
  73. } else {
  74. return $this->fetch();
  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. try {
  84. $this->validate($params, 'app\admin\validate\AuthGroup');
  85. } catch (ValidateException $e) {
  86. $this->error($e->getMessage());
  87. }
  88. if (!in_array($params['parentid'], $this->childrenGroupIds)) {
  89. $this->error('父组别超出权限范围');
  90. }
  91. $parentmodel = AuthGroupModel::find($params['parentid']);
  92. if (!$parentmodel) {
  93. $this->error('父组别未找到');
  94. }
  95. if ($params) {
  96. $this->modelClass->create($params);
  97. $this->success('新增成功');
  98. }
  99. $this->error('参数不能为空');
  100. }
  101. return $this->fetch();
  102. }
  103. //编辑管理员用户组
  104. public function edit()
  105. {
  106. $id = $this->request->param('id/d');
  107. if (!in_array($id, $this->childrenGroupIds)) {
  108. $this->error('你没有权限访问!');
  109. }
  110. $row = $this->modelClass->find($id);
  111. if (!$row) {
  112. $this->error('记录未找到');
  113. }
  114. if ($this->request->isPost()) {
  115. $this->token();
  116. $params = $this->request->post("row/a", [], 'strip_tags');
  117. //父节点不能是非权限内节点
  118. if (!in_array($params['parentid'], $this->childrenGroupIds)) {
  119. $this->error('父组别超出权限范围');
  120. }
  121. // 父节点不能是它自身的子节点或自己本身
  122. if (in_array($params['parentid'], Tree::instance()->getChildrenIds($row->id, true))) {
  123. $this->error('父角色不能是自身!');
  124. }
  125. $params['rules'] = explode(',', $params['rules']);
  126. $parentmodel = AuthGroupModel::find($params['parentid']);
  127. if (!$parentmodel) {
  128. $this->error('父组别未找到');
  129. }
  130. // 父级别的规则节点
  131. $parentrules = explode(',', $parentmodel->rules);
  132. // 当前组别的规则节点
  133. $currentrules = $this->auth->getRuleIds();
  134. $rules = $params['rules'];
  135. // 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
  136. $rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
  137. // 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
  138. $rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
  139. $params['rules'] = implode(',', $rules);
  140. if ($params) {
  141. Db::startTrans();
  142. try {
  143. $row->save($params);
  144. $children_auth_groups = $this->modelClass->whereIn('id', implode(',', (Tree::instance()->getChildrenIds($row->id))))->select();
  145. $childparams = [];
  146. foreach ($children_auth_groups as $key => $children_auth_group) {
  147. $childparams[$key]['id'] = $children_auth_group->id;
  148. $childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
  149. }
  150. $this->modelClass->saveAll($childparams);
  151. Db::commit();
  152. } catch (Exception $e) {
  153. Db::rollback();
  154. $this->error($e->getMessage());
  155. }
  156. $this->success('编辑成功');
  157. }
  158. $this->error('参数不能为空');
  159. }
  160. $this->assign("data", $row);
  161. return $this->fetch();
  162. }
  163. /**
  164. * 删除
  165. */
  166. public function del()
  167. {
  168. if (false === $this->request->isPost()) {
  169. $this->error('未知参数');
  170. }
  171. $ids = $this->request->param('id/a', null);
  172. if (empty($ids)) {
  173. $this->error('参数错误!');
  174. }
  175. if (!is_array($ids)) {
  176. $ids = [0 => $ids];
  177. }
  178. if ($ids) {
  179. $grouplist = $this->auth->getGroups();
  180. $group_ids = array_map(function ($group) {
  181. return $group['id'];
  182. }, $grouplist);
  183. // 移除掉当前管理员所在组别
  184. $ids = array_diff($ids, $group_ids);
  185. // 循环判断每一个组别是否可删除
  186. $grouplist = $this->modelClass->where('id', 'in', $ids)->select()->toArray();
  187. foreach ($grouplist as $k => $v) {
  188. // 当前组别下有管理员
  189. $groupone = AuthGroupAccess::where(['group_id' => $v['id']])->find();
  190. if ($groupone) {
  191. $ids = array_diff($ids, [$v['id']]);
  192. continue;
  193. }
  194. // 当前组别下有子组别
  195. $groupone = $this->modelClass->where(['parentid' => $v['id']])->find();
  196. if ($groupone) {
  197. $ids = array_diff($ids, [$v['id']]);
  198. continue;
  199. }
  200. }
  201. if (!$ids) {
  202. $this->error('你不能删除含有子组和管理员的组');
  203. }
  204. $count = $this->modelClass->where('id', 'in', $ids)->delete();
  205. if ($count) {
  206. $this->success();
  207. }
  208. }
  209. $this->error();
  210. }
  211. /**
  212. * 读取角色权限树
  213. *
  214. * @internal
  215. */
  216. public function roletree()
  217. {
  218. $model = new AuthGroupModel;
  219. $id = $this->request->post("id");
  220. $pid = $this->request->post("parentid");
  221. $parentGroupModel = $model->find($pid);
  222. $currentGroupModel = null;
  223. if ($id) {
  224. $currentGroupModel = $model->find($id);
  225. }
  226. if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
  227. $id = $id ? $id : null;
  228. $ruleList = AuthRuleModel::order('listorder', 'desc')->order('id', 'asc')->select()->toArray();
  229. //读取父类角色所有节点列表
  230. $parentRuleList = [];
  231. if (in_array('*', explode(',', $parentGroupModel->rules))) {
  232. $parentRuleList = $ruleList;
  233. } else {
  234. $parentRuleIds = explode(',', $parentGroupModel->rules);
  235. foreach ($ruleList as $k => $v) {
  236. if (in_array($v['id'], $parentRuleIds)) {
  237. $parentRuleList[] = $v;
  238. }
  239. }
  240. }
  241. $ruleTree = new Tree();
  242. $groupTree = new Tree();
  243. //当前所有正常规则列表
  244. $ruleTree->init($parentRuleList);
  245. //角色组列表
  246. $groupTree->init(AuthGroupModel::where('id', 'in', $this->childrenGroupIds)->select()->toArray());
  247. //读取当前角色下规则ID集合
  248. $adminRuleIds = $this->auth->getRuleIds();
  249. //是否是超级管理员
  250. $superadmin = $this->auth->isAdministrator();
  251. //当前拥有的规则ID集合
  252. $currentRuleIds = $id ? explode(',', $currentGroupModel->rules) : [];
  253. if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
  254. $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
  255. $hasChildrens = [];
  256. foreach ($parentRuleList as $k => $v) {
  257. if ($v['haschild']) {
  258. $hasChildrens[] = $v['id'];
  259. }
  260. }
  261. $parentRuleIds = array_map(function ($item) {
  262. return $item['id'];
  263. }, $parentRuleList);
  264. $nodeList = [];
  265. foreach ($parentRuleList as $k => $v) {
  266. if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
  267. continue;
  268. }
  269. if ($v['parentid'] && !in_array($v['parentid'], $parentRuleIds)) {
  270. continue;
  271. }
  272. $state = ['selected' => in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens)];
  273. $nodeList[] = ['id' => $v['id'], 'parent' => $v['parentid'] ? $v['parentid'] : '#', 'text' => $v['title'], 'type' => 'menu', 'state' => $state];
  274. }
  275. $this->success('', null, $nodeList);
  276. } else {
  277. $this->error('父组别不能是它的子组别');
  278. }
  279. } else {
  280. $this->error('组别未找到');
  281. }
  282. }
  283. //批量更新
  284. public function multi()
  285. {
  286. // 管理员禁止批量操作
  287. $this->error();
  288. }
  289. }