Group.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use ba\Tree;
  4. use Throwable;
  5. use think\facade\Db;
  6. use app\admin\model\AdminRule;
  7. use app\admin\model\AdminGroup;
  8. use app\common\controller\Backend;
  9. class Group extends Backend
  10. {
  11. /**
  12. * 修改、删除分组时对操作管理员进行鉴权
  13. * 本管理功能部分场景对数据权限有要求,修改此值请额外确定以下的 absoluteAuth 实现的功能
  14. * allAuthAndOthers=管理员拥有该分组所有权限并拥有额外权限时允许
  15. */
  16. protected string $authMethod = 'allAuthAndOthers';
  17. /**
  18. * 数据模型
  19. * @var object
  20. * @phpstan-var AdminGroup
  21. */
  22. protected object $model;
  23. protected string|array $preExcludeFields = ['create_time', 'update_time'];
  24. protected string|array $quickSearchField = 'name';
  25. /**
  26. * @var Tree
  27. */
  28. protected Tree $tree;
  29. /**
  30. * 远程select初始化传值
  31. * @var array
  32. */
  33. protected array $initValue;
  34. /**
  35. * 搜索关键词
  36. * @var string
  37. */
  38. protected string $keyword;
  39. /**
  40. * 是否组装Tree
  41. * @var bool
  42. */
  43. protected bool $assembleTree;
  44. /**
  45. * 登录管理员的角色组
  46. * @var array
  47. */
  48. protected array $adminGroups = [];
  49. public function initialize(): void
  50. {
  51. parent::initialize();
  52. $this->model = new AdminGroup();
  53. $this->tree = Tree::instance();
  54. $isTree = $this->request->param('isTree', true);
  55. $this->initValue = $this->request->get("initValue/a", []);
  56. $this->initValue = array_filter($this->initValue);
  57. $this->keyword = $this->request->request("quickSearch", '');
  58. // 有初始化值时不组装树状(初始化出来的值更好看)
  59. $this->assembleTree = $isTree && !$this->initValue;
  60. $this->adminGroups = Db::name('admin_group_access')->where('uid', $this->auth->id)->column('group_id');
  61. }
  62. public function index(): void
  63. {
  64. if ($this->request->param('select')) {
  65. $this->select();
  66. }
  67. $list = $this->getGroups();
  68. // $list = json_decode(json_encode($list), true);
  69. // $arr = [];
  70. // foreach ($list as $k=>$v) {
  71. // foreach ($v as $k1=>$v1) {
  72. // if(!empty($v1['children'])) {
  73. // $v1 = $this->handleGroup($v1['children']);
  74. // }
  75. // $arr[$k][strtolower($k1)] = $v1;
  76. // }
  77. // }
  78. $this->success('', [
  79. 'list' => $list,
  80. 'group' => $this->adminGroups,
  81. 'remark' => get_route_remark(),
  82. ]);
  83. }
  84. public function handleGroup($data)
  85. {
  86. $arr = [];
  87. foreach ($data as $k=>$v) {
  88. if(!empty($v['children'] ?? ''))
  89. {
  90. $arr[$k][strtolower($k)] = $v;
  91. $arr[$k]['children'] = $this->handleGroup($v['children']);
  92. }else{
  93. $arr[$k][strtolower($k)] = $v;
  94. }
  95. }
  96. return $arr;
  97. }
  98. /**
  99. * 添加
  100. * @throws Throwable
  101. */
  102. public function add(): void
  103. {
  104. if ($this->request->isPost()) {
  105. $data = $this->request->post();
  106. if (!$data) {
  107. $this->error(__('Parameter %s can not be empty', ['']));
  108. }
  109. $data = $this->excludeFields($data);
  110. $data = $this->handleRules($data);
  111. $result = false;
  112. $this->model->startTrans();
  113. try {
  114. // 模型验证
  115. if ($this->modelValidate) {
  116. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  117. if (class_exists($validate)) {
  118. $validate = new $validate();
  119. $validate->scene('add')->check($data);
  120. }
  121. }
  122. $result = $this->model->save($data);
  123. $this->model->commit();
  124. } catch (Throwable $e) {
  125. $this->model->rollback();
  126. $this->error($e->getMessage());
  127. }
  128. if ($result !== false) {
  129. $this->success(__('Added successfully'));
  130. } else {
  131. $this->error(__('No rows were added'));
  132. }
  133. }
  134. $this->error(__('Parameter error'));
  135. }
  136. /**
  137. * 编辑
  138. * @param string|null $id
  139. * @return void
  140. * @throws Throwable
  141. */
  142. public function edit(string $id = null): void
  143. {
  144. $row = $this->model->find($id);
  145. if (!$row) {
  146. $this->error(__('Record not found'));
  147. }
  148. $this->checkAuth($id);
  149. if ($this->request->isPost()) {
  150. $data = $this->request->post();
  151. if (!$data) {
  152. $this->error(__('Parameter %s can not be empty', ['']));
  153. }
  154. $adminGroup = Db::name('admin_group_access')->where('uid', $this->auth->id)->column('group_id');
  155. if (in_array($data['id'], $adminGroup)) {
  156. $this->error(__('You cannot modify your own management group!'));
  157. }
  158. $data = $this->excludeFields($data);
  159. $data = $this->handleRules($data);
  160. $result = false;
  161. $this->model->startTrans();
  162. try {
  163. // 模型验证
  164. if ($this->modelValidate) {
  165. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  166. if (class_exists($validate)) {
  167. $validate = new $validate();
  168. $validate->scene('edit')->check($data);
  169. }
  170. }
  171. $result = $row->save($data);
  172. $this->model->commit();
  173. } catch (Throwable $e) {
  174. $this->model->rollback();
  175. $this->error($e->getMessage());
  176. }
  177. if ($result !== false) {
  178. $this->success(__('Update successful'));
  179. } else {
  180. $this->error(__('No rows updated'));
  181. }
  182. }
  183. // 读取所有pid,全部从节点数组移除,父级选择状态由子级决定
  184. $pidArr = AdminRule::field('pid')
  185. ->distinct(true)
  186. ->where('id', 'in', $row->RULES)
  187. ->select()->toArray();
  188. $rules = $row->RULES ? explode(',', $row->RULES) : [];
  189. foreach ($pidArr as $item) {
  190. $ruKey = array_search($item['pid'], $rules);
  191. if ($ruKey !== false) {
  192. unset($rules[$ruKey]);
  193. }
  194. }
  195. $row->rules = array_values($rules);
  196. $arr = [];
  197. foreach($row->toarray() as $k=>$v)
  198. {
  199. $arr[strtolower($k)] = $v;
  200. }
  201. $this->success('', [
  202. 'row' => $arr
  203. ]);
  204. }
  205. /**
  206. * 删除
  207. * @param array $ids
  208. * @throws Throwable
  209. */
  210. public function del(array $ids = []): void
  211. {
  212. if (!$this->request->isDelete() || !$ids) {
  213. $this->error(__('Parameter error'));
  214. }
  215. $pk = $this->model->getPk();
  216. $data = $this->model->where($pk, 'in', $ids)->select();
  217. foreach ($data as $v) {
  218. $this->checkAuth($v->id);
  219. }
  220. $subData = $this->model->where('pid', 'in', $ids)->column('pid', 'id');
  221. foreach ($subData as $key => $subDatum) {
  222. if (!in_array($key, $ids)) {
  223. $this->error(__('Please delete the child element first, or use batch deletion'));
  224. }
  225. }
  226. $adminGroup = Db::name('admin_group_access')->where('uid', $this->auth->id)->column('group_id');
  227. $count = 0;
  228. $this->model->startTrans();
  229. try {
  230. foreach ($data as $v) {
  231. if (!in_array($v['ID'], $adminGroup)) {
  232. $count += $v->where('ID',$v['ID'])->delete();
  233. }
  234. }
  235. $this->model->commit();
  236. } catch (Throwable $e) {
  237. $this->model->rollback();
  238. $this->error($e->getMessage());
  239. }
  240. if ($count) {
  241. $this->success(__('Deleted successfully'));
  242. } else {
  243. $this->error(__('No rows were deleted'));
  244. }
  245. }
  246. /**
  247. * 远程下拉
  248. * @return void
  249. * @throws Throwable
  250. */
  251. public function select(): void
  252. {
  253. $data = $this->getGroups([['status', '=', 1]]);
  254. if ($this->assembleTree) {
  255. $data = $this->tree->assembleTree($this->tree->getTreeArray($data));
  256. }
  257. foreach ($data as $k => $v) {
  258. foreach ($v as $k1 => $v1) {
  259. $data[$k][strtolower($k1)] = $v1;
  260. unset($data[$k][strtoupper($k1)]);
  261. }
  262. }
  263. $this->success('', [
  264. 'options' => $data
  265. ]);
  266. }
  267. /**
  268. * 权限节点入库前处理
  269. * @throws Throwable
  270. */
  271. public function handleRules(array &$data): array
  272. {
  273. if (!empty($data['rules']) && is_array($data['rules'])) {
  274. $rules = AdminRule::select();
  275. $superAdmin = true;
  276. foreach ($rules as $rule) {
  277. if (!in_array($rule['id'], $data['rules'])) {
  278. $superAdmin = false;
  279. }
  280. }
  281. if ($superAdmin) {
  282. $data['rules'] = '*';
  283. } else {
  284. // 禁止添加`拥有自己全部权限`的分组
  285. if (!array_diff($this->auth->getRuleIds(), $data['rules'])) {
  286. $this->error(__('Role group has all your rights, please contact the upper administrator to add or do not need to add!'));
  287. }
  288. $data['rules'] = implode(',', $data['rules']);
  289. }
  290. } else {
  291. unset($data['rules']);
  292. }
  293. return $data;
  294. }
  295. /**
  296. * 获取分组
  297. * @param array $where
  298. * @return array
  299. * @throws Throwable
  300. */
  301. public function getGroups(array $where = []): array
  302. {
  303. $pk = $this->model->getPk();
  304. $initKey = $this->request->get("initKey/s", $pk);
  305. // 下拉选择时只获取:拥有所有权限并且有额外权限的分组
  306. $absoluteAuth = $this->request->get('absoluteAuth/b', false);
  307. if ($this->keyword) {
  308. $keyword = explode(' ', $this->keyword);
  309. foreach ($keyword as $item) {
  310. $where[] = [$this->quickSearchField, 'like', '%' . $item . '%'];
  311. }
  312. }
  313. if ($this->initValue) {
  314. $where[] = [$initKey, 'in', $this->initValue];
  315. }
  316. if (!$this->auth->isSuperAdmin()) {
  317. $authGroups = $this->auth->getAllAuthGroups($this->authMethod);
  318. if (!$absoluteAuth) $authGroups = array_merge($this->adminGroups, $authGroups);
  319. $where[] = ['id', 'in', $authGroups];
  320. }
  321. $field = ['ID','PID','NAME','ID AS id', 'PID AS pid','NAME AS name','RULES AS rules','STATUS AS status','UPDATE_TIME AS update_time','CREATE_TIME AS create_time'];
  322. $data = $this->model->where($where)->field($field)->select()->toArray();
  323. // 获取第一个权限的名称供列表显示-s
  324. foreach ($data as &$datum) {
  325. if ($datum['rules']) {
  326. if ($datum['rules'] == '*') {
  327. $datum['rules'] = __('Super administrator');
  328. } else {
  329. $rules = explode(',', $datum['rules']);
  330. if ($rules) {
  331. $rulesFirstTitle = AdminRule::where('id', $rules[0])->value('TITLE');
  332. $datum['rules'] = count($rules) == 1 ? $rulesFirstTitle : $rulesFirstTitle . '等 ' . count($rules) . ' 项';
  333. }
  334. }
  335. } else {
  336. $datum['rules'] = __('No permission');
  337. }
  338. }
  339. // 获取第一个权限的名称供列表显示-e
  340. // 如果要求树状,此处先组装好 children
  341. return $this->assembleTree ? $this->tree->assembleChild($data) : $data;
  342. }
  343. /**
  344. * 检查权限
  345. * @param $groupId
  346. * @return void
  347. * @throws Throwable
  348. */
  349. public function checkAuth($groupId): void
  350. {
  351. $authGroups = $this->auth->getAllAuthGroups($this->authMethod);
  352. if (!$this->auth->isSuperAdmin() && !in_array($groupId, $authGroups)) {
  353. $this->error(__($this->authMethod == 'allAuth' ? 'You need to have all permissions of this group to operate this group~' : 'You need to have all the permissions of the group and have additional permissions before you can operate the group~'));
  354. }
  355. }
  356. }