modified 完善细节

This commit is contained in:
zhaoxiang 2020-05-14 16:45:01 +08:00
parent 3d0cd21a76
commit 7d30a9f11c
4 changed files with 48 additions and 49 deletions

View File

@ -98,29 +98,13 @@ class Auth extends Base {
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function add() {
$rules = [];
$postData = $this->request->post();
if ($postData['rules']) {
$rules = $postData['rules'];
$rules = array_filter($rules);
}
unset($postData['rules']);
$res = AdminAuthGroup::create($postData);
$res = AdminAuthGroup::create([
'name' => $this->request->post('name', ''),
'description' => $this->request->post('description', '')
]);
if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
}
if ($rules) {
$insertData = [];
foreach ($rules as $value) {
if ($value) {
$insertData[] = [
'group_id' => $res->id,
'url' => $value
];
}
}
(new AdminAuthRule())->saveAll($insertData);
}
return $this->buildSuccess();
}
@ -154,12 +138,10 @@ class Auth extends Base {
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function edit() {
$postData = $this->request->post();
if ($postData['rules']) {
$this->editRule();
}
unset($postData['rules']);
$res = AdminAuthGroup::update($postData);
$res = AdminAuthGroup::update([
'name' => $this->request->post('name', ''),
'description' => $this->request->post('description', '')
]);
if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
}
@ -262,30 +244,33 @@ class Auth extends Base {
* @throws \think\exception\DbException
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
private function editRule() {
$postData = $this->request->post();
$needAdd = [];
$has = (new AdminAuthRule())->where(['group_id' => $postData['id']])->select();
$has = Tools::buildArrFromObj($has);
$hasRule = array_column($has, 'url');
$needDel = array_flip($hasRule);
foreach ($postData['rules'] as $key => $value) {
if (!empty($value)) {
if (!in_array($value, $hasRule)) {
$data['url'] = $value;
$data['group_id'] = $postData['id'];
$needAdd[] = $data;
} else {
unset($needDel[$value]);
public function editRule() {
$id = $this->request->post('id', 0);
$rules = $this->request->post('rules', []);
if ($rules) {
$needAdd = [];
$has = (new AdminAuthRule())->where(['group_id' => $id])->select();
$has = Tools::buildArrFromObj($has);
$hasRule = array_column($has, 'url');
$needDel = array_flip($hasRule);
foreach ($rules as $key => $value) {
if (!empty($value)) {
if (!in_array($value, $hasRule)) {
$data['url'] = $value;
$data['group_id'] = $id;
$needAdd[] = $data;
} else {
unset($needDel[$value]);
}
}
}
}
if (count($needAdd)) {
(new AdminAuthRule())->saveAll($needAdd);
}
if (count($needDel)) {
$urlArr = array_keys($needDel);
(new AdminAuthRule())->whereIn('url', $urlArr)->where('group_id', $postData['id'])->delete();
if (count($needAdd)) {
(new AdminAuthRule())->saveAll($needAdd);
}
if (count($needDel)) {
$urlArr = array_keys($needDel);
(new AdminAuthRule())->whereIn('url', $urlArr)->where('group_id', $id)->delete();
}
}
}
}

View File

@ -17,7 +17,7 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">=7.0",
"topthink/framework": "5.1.*",
"topthink/think-migration": "^2.0",
"guzzlehttp/guzzle": "^6.5"

View File

@ -700,6 +700,16 @@ class IniAdminMenu extends Migrator {
'hide' => 0,
'icon' => '',
'level' => 0
], [
'id' => 71,
'name' => '编辑权限细节',
'fid' => 15,
'url' => 'admin/Auth/editRule',
'auth' => 0,
'sort' => 0,
'hide' => 0,
'icon' => '',
'level' => 0
]
];

View File

@ -118,6 +118,10 @@ Route::group('admin', function() {
'admin/Auth/delMember',
['method' => 'get']
],
'editRule' => [
'admin/Auth/editRule',
['method' => 'post']
],
'edit' => [
'admin/Auth/edit',
['method' => 'post']