修改权限

This commit is contained in:
wuyanwen
2019-12-27 09:52:03 +08:00
parent b94800033f
commit ef30c07f4a
5 changed files with 61 additions and 38 deletions

View File

@@ -49,8 +49,8 @@ class Role extends CatchController
{ {
$this->role->storeBy($request->param()); $this->role->storeBy($request->param());
if (!empty($request->param('permissionids'))) { if (!empty($request->param('permissions'))) {
$this->role->attach($request->param('permissionids')); $this->role->attach($request->param('permissions'));
} }
// 添加角色 // 添加角色
return CatchResponse::success(); return CatchResponse::success();
@@ -58,7 +58,9 @@ class Role extends CatchController
public function read($id) public function read($id)
{ {
$role = $this->role->findBy($id);
$role->permissions = $role->getPermissions();
return CatchResponse::success($role);
} }
/** /**
@@ -69,23 +71,7 @@ class Role extends CatchController
* @return string * @return string
*/ */
public function edit($id) public function edit($id)
{ {}
$role = $this->role->findBy($id);
$form = new CatchForm();
$form->formId('role');
$form->hidden('parent_id')->default($role->parent_id);
$form->text('role_name', '角色名称', true)->default($role->name)->verify('required')->placeholder('请输入角色名称');
$form->textarea('description', '角色描述')->default($role->description)->placeholder('请输入角色描述');
$form->dom('<div id="permissions"></div>', '权限');
$form->formBtn('submitRole');
return $this->fetch([
'form' => $form->render(),
'role_id' => $role->id,
'parent_id' => $role->parent_id
]);
}
/** /**
* *
@@ -103,8 +89,8 @@ class Role extends CatchController
$role->detach(); $role->detach();
if (!empty($request->param('permissionids'))) { if (!empty($request->param('permissions'))) {
$role->attach($request->param('permissionids')); $role->attach($request->param('permissions'));
} }
return CatchResponse::success(); return CatchResponse::success();

View File

@@ -13,11 +13,12 @@ trait HasRolesTrait
return $this->belongsToMany(Roles::class, 'user_has_roles', 'role_id', 'uid'); return $this->belongsToMany(Roles::class, 'user_has_roles', 'role_id', 'uid');
} }
/** /**
* *
* @time 2019年12月08日 * @time 2019年12月08日
* @return mixed * @param array $fields
*/ * @return mixed
*/
public function getRoles() public function getRoles()
{ {
return $this->roles()->select(); return $this->roles()->select();
@@ -54,4 +55,4 @@ trait HasRolesTrait
return $this->roles()->detach($roles); return $this->roles()->detach($roles);
} }
} }

View File

@@ -40,8 +40,15 @@ class Permissions extends CatchModel
$query->where('parent_id', $search['id']) $query->where('parent_id', $search['id'])
->whereOr('id', $search['id']); ->whereOr('id', $search['id']);
}) })
->when($search['permission_ids'] ?? false, function ($query) use ($search){ ->when($search['role_id'] ?? false, function ($query) use ($search){
$query->whereIn('id', $search['permission_ids']); $permissionIds = [];
$permissions = Roles::where('id', $search['role_id'])->find()->getPermissions();
foreach ($permissions as $_permission) {
$permissionIds[] = $_permission->pivot->permission_id;
}
if(!empty($permissionIds)) {
$query->whereIn('id', $permissionIds);
}
}) })
->order('sort', 'desc') ->order('sort', 'desc')
->order('id', 'desc') ->order('id', 'desc')
@@ -53,4 +60,4 @@ class Permissions extends CatchModel
{ {
return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id'); return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id');
} }
} }

View File

@@ -32,7 +32,36 @@ class DataDictionary extends CatchController
{ {
$tables = Db::query('show table status'); $tables = Db::query('show table status');
return CatchResponse::paginate(Paginator::make($tables, $request->get('limit') ?? 10, $request->get('page'), count($tables), false, [])); $tablename = $request->get('tablename');
$engine = $request->get('engine');
$searchTables = [];
$searchMode = false;
if ($tablename || $engine) {
$searchMode = true;
}
foreach ($tables as $key => &$table) {
$table = array_change_key_case($table);
$table['index_length'] = $table['index_length'] > 1024 ? intval($table['index_length']/1024) .'MB' : $table['index_length'].'KB';
$table['data_length'] = $table['data_length'] > 1024 ? intval($table['data_length']/1024) .'MB' : $table['data_length'].'KB';
$table['create_time'] = date('Y-m-d', strtotime($table['create_time']));
// 搜索
if ($tablename && !$engine && stripos($table['name'], $tablename) !== false) {
$searchTables[] = $table;
}
// 搜索
if (!$tablename && $engine && stripos($table['engine'], $engine) !== false) {
$searchTables[] = $table;
}
if ($tablename && $engine && stripos($table['engine'], $engine) !== false && stripos($table['name'], $tablename) !== false) {
$searchTables[] = $table;
}
}
return CatchResponse::paginate(Paginator::make(!$searchMode ? $tables : $searchTables, $request->get('limit') ?? 10, $request->get('page') ?? 1, $searchMode ? count($searchTables) : count($tables), false, []));
} }
/** /**

View File

@@ -57,9 +57,7 @@ class User extends CatchController
{ {
$this->user->storeBy($request->post()); $this->user->storeBy($request->post());
if (!empty($request->param('roleids'))) { $this->user->attach($request->param('roles'));
$this->user->attach($request->param('roleids'));
}
return CatchResponse::success('', '添加成功'); return CatchResponse::success('', '添加成功');
} }
@@ -72,7 +70,9 @@ class User extends CatchController
*/ */
public function read($id) public function read($id)
{ {
return CatchResponse::success($this->user->findBy($id)); $user = $this->user->findBy($id);
$user->roles = $user->getRoles();
return CatchResponse::success($user);
} }
/** /**
@@ -96,8 +96,8 @@ class User extends CatchController
$user->detach(); $user->detach();
if (!empty($request->param('roleids'))) { if (!empty($request->param('roles'))) {
$user->attach($request->param('roleids')); $user->attach($request->param('roles'));
} }
return CatchResponse::success(); return CatchResponse::success();