Rule.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use ba\Tree;
  4. use Throwable;
  5. use app\admin\model\AdminRule;
  6. use app\admin\model\AdminGroup;
  7. use app\common\controller\Backend;
  8. class Rule extends Backend
  9. {
  10. protected string|array $preExcludeFields = ['create_time', 'update_time'];
  11. protected string|array $quickSearchField = 'title';
  12. /**
  13. * @var object
  14. * @phpstan-var AdminRule
  15. */
  16. protected object $model;
  17. /**
  18. * @var Tree
  19. */
  20. protected Tree $tree;
  21. /**
  22. * 远程select初始化传值
  23. * @var array
  24. */
  25. protected array $initValue;
  26. /**
  27. * 搜索关键词
  28. * @var string
  29. */
  30. protected string $keyword;
  31. /**
  32. * 是否组装Tree
  33. * @var bool
  34. */
  35. protected bool $assembleTree;
  36. /**
  37. * 开启模型验证
  38. * @var bool
  39. */
  40. protected bool $modelValidate = false;
  41. public function initialize(): void
  42. {
  43. parent::initialize();
  44. $this->model = new AdminRule();
  45. $this->tree = Tree::instance();
  46. $isTree = $this->request->param('isTree', true);
  47. $this->initValue = $this->request->get('initValue/a', []);
  48. $this->initValue = array_filter($this->initValue);
  49. $this->keyword = $this->request->request('quickSearch', '');
  50. // 有初始化值时不组装树状(初始化出来的值更好看)
  51. $this->assembleTree = $isTree && !$this->initValue;
  52. }
  53. public function index(): void
  54. {
  55. if ($this->request->param('select')) {
  56. $this->select();
  57. }
  58. $this->success('', [
  59. 'list' => $this->getMenus(),
  60. 'remark' => get_route_remark(),
  61. ]);
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add(): void
  67. {
  68. if ($this->request->isPost()) {
  69. $data = $this->request->post();
  70. if (!$data) {
  71. $this->error(__('Parameter %s can not be empty', ['']));
  72. }
  73. $data = $this->excludeFields($data);
  74. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  75. $data[$this->dataLimitField] = $this->auth->id;
  76. }
  77. $result = false;
  78. $this->model->startTrans();
  79. try {
  80. // 模型验证
  81. if ($this->modelValidate) {
  82. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  83. if (class_exists($validate)) {
  84. $validate = new $validate();
  85. if ($this->modelSceneValidate) $validate->scene('add');
  86. $validate->check($data);
  87. }
  88. }
  89. $result = $this->model->save($data);
  90. // 检查所有非超管的分组是否应该拥有此权限
  91. if (!empty($data['pid'])) {
  92. $groups = AdminGroup::where('rules', '<>', '*')->select();
  93. foreach ($groups as $group) {
  94. $rules = explode(',', $group->rules);
  95. if (in_array($data['pid'], $rules) && !in_array($this->model->id, $rules)) {
  96. $rules[] = $this->model->id;
  97. $group->rules = implode(',', $rules);
  98. $group->save();
  99. }
  100. }
  101. }
  102. $this->model->commit();
  103. } catch (Throwable $e) {
  104. $this->model->rollback();
  105. $this->error($e->getMessage());
  106. }
  107. if ($result !== false) {
  108. $this->success(__('Added successfully'));
  109. } else {
  110. $this->error(__('No rows were added'));
  111. }
  112. }
  113. $this->error(__('Parameter error'));
  114. }
  115. /**
  116. * 编辑
  117. * @throws Throwable
  118. */
  119. public function edit(): void
  120. {
  121. $id = $this->request->param($this->model->getPk());
  122. $row = $this->model->find($id);
  123. if (!$row) {
  124. $this->error(__('Record not found'));
  125. }
  126. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  127. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  128. $this->error(__('You have no permission'));
  129. }
  130. if ($this->request->isPost()) {
  131. $data = $this->request->post();
  132. if (!$data) {
  133. $this->error(__('Parameter %s can not be empty', ['']));
  134. }
  135. $data = $this->excludeFields($data);
  136. $result = false;
  137. $this->model->startTrans();
  138. try {
  139. // 模型验证
  140. if ($this->modelValidate) {
  141. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  142. if (class_exists($validate)) {
  143. $validate = new $validate();
  144. if ($this->modelSceneValidate) $validate->scene('edit');
  145. $validate->check($data);
  146. }
  147. }
  148. if (isset($data['pid']) && $data['pid'] > 0) {
  149. // 满足意图并消除副作用
  150. $parent = $this->model->where('id', $data['pid'])->find();
  151. if ($parent['pid'] == $row['id']) {
  152. $parent->pid = 0;
  153. $parent->save();
  154. }
  155. }
  156. $result = $row->save($data);
  157. $this->model->commit();
  158. } catch (Throwable $e) {
  159. $this->model->rollback();
  160. $this->error($e->getMessage());
  161. }
  162. if ($result !== false) {
  163. $this->success(__('Update successful'));
  164. } else {
  165. $this->error(__('No rows updated'));
  166. }
  167. }
  168. $this->success('', [
  169. 'row' => $row
  170. ]);
  171. }
  172. /**
  173. * 删除
  174. * @param array $ids
  175. * @throws Throwable
  176. */
  177. public function del(array $ids = []): void
  178. {
  179. if (!$this->request->isDelete() || !$ids) {
  180. $this->error(__('Parameter error'));
  181. }
  182. // 子级元素检查
  183. $subData = $this->model->where('pid', 'in', $ids)->column('pid', 'id');
  184. foreach ($subData as $key => $subDatum) {
  185. if (!in_array($key, $ids)) {
  186. $this->error(__('Please delete the child element first, or use batch deletion'));
  187. }
  188. }
  189. parent::del($ids);
  190. }
  191. /**
  192. * 重写select方法
  193. * @throws Throwable
  194. */
  195. public function select(): void
  196. {
  197. $data = $this->getMenus([['type', 'in', ['menu_dir', 'menu']], ['status', '=', '1']]);
  198. if ($this->assembleTree) {
  199. $data = $this->tree->assembleTree($this->tree->getTreeArray($data, 'title'));
  200. }
  201. $this->success('', [
  202. 'options' => $data
  203. ]);
  204. }
  205. /**
  206. * 获取菜单列表
  207. * @throws Throwable
  208. */
  209. protected function getMenus($where = []): array
  210. {
  211. $pk = $this->model->getPk();
  212. $initKey = $this->request->get("initKey/s", $pk);
  213. $ids = $this->auth->getRuleIds();
  214. // 如果没有 * 则只获取用户拥有的规则
  215. if (!in_array('*', $ids)) {
  216. $where[] = ['id', 'in', $ids];
  217. }
  218. if ($this->keyword) {
  219. $keyword = explode(' ', $this->keyword);
  220. foreach ($keyword as $item) {
  221. $where[] = [$this->quickSearchField, 'like', '%' . $item . '%'];
  222. }
  223. }
  224. if ($this->initValue) {
  225. $where[] = [$initKey, 'in', $this->initValue];
  226. }
  227. // 读取用户组所有权限规则
  228. $rules = $this->model
  229. ->where($where)
  230. ->order('weigh desc,id asc')
  231. ->select()->toArray();
  232. // 如果要求树状,此处先组装好 children
  233. return $this->assembleTree ? $this->tree->assembleChild($rules) : $rules;
  234. }
  235. }