'角色管理', * 'parent' => 'admin/User/default', * 'display'=> true, * 'hasView'=> true, * 'order' => 10000, * 'icon' => '', * 'remark' => '角色管理', * 'param' => '' * ) * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function index() { $content = hook_one('admin_rbac_index_view'); if (!empty($content)) { return $content; } $data = Db::name('role')->order(["list_order" => "ASC", "id" => "DESC"])->select(); $this->assign("roles", $data); return $this->fetch(); } /** * 添加角色 * @adminMenu( * 'name' => '添加角色', * 'parent' => 'index', * 'display'=> false, * 'hasView'=> true, * 'order' => 10000, * 'icon' => '', * 'remark' => '添加角色', * 'param' => '' * ) * @return mixed */ public function roleAdd() { $content = hook_one('admin_rbac_role_add_view'); if (!empty($content)) { return $content; } return $this->fetch(); } /** * 添加角色提交 * @adminMenu( * 'name' => '添加角色提交', * 'parent' => 'index', * 'display'=> false, * 'hasView'=> false, * 'order' => 10000, * 'icon' => '', * 'remark' => '添加角色提交', * 'param' => '' * ) */ public function roleAddPost() { if ($this->request->isPost()) { $data = $this->request->param(); $result = $this->validate($data, 'role'); if ($result !== true) { // 验证失败 输出错误信息 $this->error($result); } else { $result = Db::name('role')->insert($data); if ($result) { $this->success("添加角色成功", url("rbac/index")); } else { $this->error("添加角色失败"); } } } } /** * 编辑角色 * @adminMenu( * 'name' => '编辑角色', * 'parent' => 'index', * 'display'=> false, * 'hasView'=> true, * 'order' => 10000, * 'icon' => '', * 'remark' => '编辑角色', * 'param' => '' * ) * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function roleEdit() { $content = hook_one('admin_rbac_role_edit_view'); if (!empty($content)) { return $content; } $id = $this->request->param("id", 0, 'intval'); if ($id == 1) { $this->error("超级管理员角色不能被修改!"); } $data = Db::name('role')->where("id", $id)->find(); if (!$data) { $this->error("该角色不存在!"); } $this->assign("data", $data); return $this->fetch(); } /** * 编辑角色提交 * @adminMenu( * 'name' => '编辑角色提交', * 'parent' => 'index', * 'display'=> false, * 'hasView'=> false, * 'order' => 10000, * 'icon' => '', * 'remark' => '编辑角色提交', * 'param' => '' * ) * @throws \think\Exception * @throws \think\exception\PDOException */ public function roleEditPost() { $id = $this->request->param("id", 0, 'intval'); if ($id == 1) { $this->error("超级管理员角色不能被修改!"); } if ($this->request->isPost()) { $data = $this->request->param(); $result = $this->validate($data, 'role'); if ($result !== true) { // 验证失败 输出错误信息 $this->error($result); } else { if (Db::name('role')->update($data) !== false) { $this->success("保存成功!", url('rbac/index')); } else { $this->error("保存失败!"); } } } } /** * 删除角色 * @adminMenu( * 'name' => '删除角色', * 'parent' => 'index', * 'display'=> false, * 'hasView'=> false, * 'order' => 10000, * 'icon' => '', * 'remark' => '删除角色', * 'param' => '' * ) * @throws \think\Exception * @throws \think\exception\PDOException */ public function roleDelete() { $id = $this->request->param("id", 0, 'intval'); if ($id == 1) { $this->error("超级管理员角色不能被删除!"); } $count = Db::name('RoleUser')->where('role_id', $id)->count(); if ($count > 0) { $this->error("该角色已经有用户!"); } else { $status = Db::name('role')->delete($id); if (!empty($status)) { $this->success("删除成功!", url('rbac/index')); } else { $this->error("删除失败!"); } } } /** * 设置角色权限 * @adminMenu( * 'name' => '设置角色权限', * 'parent' => 'index', * 'display'=> false, * 'hasView'=> true, * 'order' => 10000, * 'icon' => '', * 'remark' => '设置角色权限', * 'param' => '' * ) * @return mixed */ public function authorize() { $content = hook_one('admin_rbac_authorize_view'); if (!empty($content)) { return $content; } $AuthAccess = Db::name("AuthAccess"); $adminMenuModel = new AdminMenuModel(); //角色ID $roleId = $this->request->param("id", 0, 'intval'); if (empty($roleId)) { $this->error("参数错误!"); } $tree = new Tree(); $tree->icon = ['│ ', '├─ ', '└─ ']; $tree->nbsp = ' '; $result = $adminMenuModel->menuCache(); $newMenus = []; $privilegeData = $AuthAccess->where("role_id", $roleId)->column("rule_name");//获取权限表数据 foreach ($result as $m) { $newMenus[$m['id']] = $m; } foreach ($result as $n => $t) { $result[$n]['checked'] = ($this->_isChecked($t, $privilegeData)) ? ' checked' : ''; $result[$n]['level'] = $this->_getLevel($t['id'], $newMenus); $result[$n]['style'] = empty($t['parent_id']) ? '' : 'display:none;'; $result[$n]['parentIdNode'] = ($t['parent_id']) ? ' class="child-of-node-' . $t['parent_id'] . '"' : ''; } $str = "