fix:修复非超管对于新增的子级菜单规则可能显示异常的问题

This commit is contained in:
妙码生花 2024-03-02 22:34:46 +08:00
parent c30e5e9bfe
commit 4bc3e5665f
2 changed files with 117 additions and 0 deletions

View File

@ -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

View File

@ -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