diff --git a/app/admin/controller/auth/Rule.php b/app/admin/controller/auth/Rule.php index 855177f3..e697d350 100644 --- a/app/admin/controller/auth/Rule.php +++ b/app/admin/controller/auth/Rule.php @@ -5,6 +5,7 @@ namespace app\admin\controller\auth; use ba\Tree; use Throwable; use app\admin\model\AdminRule; +use app\admin\model\AdminGroup; use app\common\controller\Backend; class Rule extends Backend @@ -75,6 +76,64 @@ class Rule extends Backend ]); } + /** + * 添加 + */ + public function add(): void + { + if ($this->request->isPost()) { + $data = $this->request->post(); + if (!$data) { + $this->error(__('Parameter %s can not be empty', [''])); + } + + $data = $this->excludeFields($data); + if ($this->dataLimit && $this->dataLimitFieldAutoFill) { + $data[$this->dataLimitField] = $this->auth->id; + } + + $result = false; + $this->model->startTrans(); + try { + // 模型验证 + if ($this->modelValidate) { + $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + if (class_exists($validate)) { + $validate = new $validate; + if ($this->modelSceneValidate) $validate->scene('add'); + $validate->check($data); + } + } + $result = $this->model->save($data); + + // 检查所有非超管的分组是否应该拥有此权限 + if (!empty($data['pid'])) { + $groups = AdminGroup::where('rules', '<>', '*')->select(); + foreach ($groups as $group) { + $rules = explode(',', $group->rules); + if (in_array($data['pid'], $rules) && !in_array($this->model->id, $rules)) { + $rules[] = $this->model->id; + $group->rules = implode(',', $rules); + $group->save(); + } + } + } + + $this->model->commit(); + } catch (Throwable $e) { + $this->model->rollback(); + $this->error($e->getMessage()); + } + if ($result !== false) { + $this->success(__('Added successfully')); + } else { + $this->error(__('No rows were added')); + } + } + + $this->error(__('Parameter error')); + } + /** * 编辑 * @throws Throwable diff --git a/app/admin/controller/user/Rule.php b/app/admin/controller/user/Rule.php index cd556047..ebc52f5c 100644 --- a/app/admin/controller/user/Rule.php +++ b/app/admin/controller/user/Rule.php @@ -5,6 +5,7 @@ namespace app\admin\controller\user; use ba\Tree; use Throwable; use app\admin\model\UserRule; +use app\admin\model\UserGroup; use app\common\controller\Backend; class Rule extends Backend @@ -71,6 +72,63 @@ class Rule extends Backend ]); } + /** + * 添加 + */ + public function add(): void + { + if ($this->request->isPost()) { + $data = $this->request->post(); + if (!$data) { + $this->error(__('Parameter %s can not be empty', [''])); + } + + $data = $this->excludeFields($data); + if ($this->dataLimit && $this->dataLimitFieldAutoFill) { + $data[$this->dataLimitField] = $this->auth->id; + } + + $result = false; + $this->model->startTrans(); + try { + // 模型验证 + if ($this->modelValidate) { + $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + if (class_exists($validate)) { + $validate = new $validate; + if ($this->modelSceneValidate) $validate->scene('add'); + $validate->check($data); + } + } + $result = $this->model->save($data); + + if (!empty($data['pid'])) { + $groups = UserGroup::where('rules', '<>', '*')->select(); + foreach ($groups as $group) { + $rules = explode(',', $group->rules); + if (in_array($data['pid'], $rules) && !in_array($this->model->id, $rules)) { + $rules[] = $this->model->id; + $group->rules = implode(',', $rules); + $group->save(); + } + } + } + + $this->model->commit(); + } catch (Throwable $e) { + $this->model->rollback(); + $this->error($e->getMessage()); + } + if ($result !== false) { + $this->success(__('Added successfully')); + } else { + $this->error(__('No rows were added')); + } + } + + $this->error(__('Parameter error')); + } + /** * 编辑 * @throws Throwable