adminAuthRuleModel = Db::name('nw_admin_auth_rule'); $this->adminRoleModel = Db::name('nw_admin_role'); $this->adminRoleUserModel = Db::name('nw_admin_role_user'); } public function index() { $where = []; $role_id = 0; //非超级管理员时 if(!in_array(session('ADMIN_ID') ,[1,19])){ $role_id = $this->adminRoleUserModel->where(['user_id'=>session('ADMIN_ID')])->value('role_id'); } $data = $this->adminRoleModel->field('id,parent_id,name,status,create_time,remark')->order('id')->select(); $tree = new Tree(); $tree->init($data); $childrenlist = $tree->getTreeList($tree->getTreeArrayO($role_id)); $this->assign("roles", $childrenlist); return $this->fetch(); } public function roleAdd() { $role_id = (int)$this->request->param("id", 0, 'intval'); if (!$role_id && !in_array(session('ADMIN_ID') ,[1,19]) ) { $role_id = $this->adminRoleUserModel->where(['user_id'=>session('ADMIN_ID')])->value('role_id'); } if ( $role_id) { $name = $this->adminRoleModel->where('id',$role_id)->value('name'); }else{ $name = '最高管理员'; } $this->assign("role_id", $role_id); $this->assign("name", $name); return $this->fetch('roleadd'); } public function roleAddPost() { if ($this->request->isPost()) { $result = $this->validate($_POST, 'Role'); if ($result !== true) { // 验证失败 输出错误信息 $this->error($result); } else { $_POST['update_time'] = $_POST['create_time'] = time(); if ( !empty($_POST['remark']) && strlen($_POST['remark']) > 200) { $_POST['remark'] = substr($_POST['remark'], 0, 200); } // 判断用户是否有权限 $res = $this->_isPermission($_POST['parent_id']); if ( !$res) { $this->error("添加角色失败,没有权限"); } $result = $this->adminRoleModel->insert($_POST); if ($result) { $statusStr = $_POST['status'] ? '启用' : '禁用'; $LogStr = "新增角色:" .$_POST['name']. ",状态:" . $statusStr; $this->insertLog($this->current_node,$LogStr,162); $this->success("添加角色成功", url("rbac/index")); } else { $this->error("添加角色失败"); } } } } public function roleEdit() { $id = $this->request->param("id", 0, 'intval'); $data = $this->adminRoleModel->where(["id" => $id])->find(); if (!$data) { $this->error("该角色不存在!"); } if ( $data['parent_id']) { $data['parent_name'] = $this->adminRoleModel->where('id',$data['parent_id'])->value('name'); }else{ $data['parent_name'] = '最高管理员'; } $this->assign("data", $data); return $this->fetch('roleedit'); } public function roleEditPost() { $id = $this->request->param("id", 0, 'intval'); if (empty($id)) { $this->error("角色id不能为空!"); } if ($this->request->isPost()) { $result = $this->validate($_POST, 'Role'); if ($result !== true) { // 验证失败 输出错误信息 $this->error($result); } else { if ( !empty($_POST['remark']) && strlen($_POST['remark']) > 200) { $_POST['remark'] = substr($_POST['remark'], 0, 200); } $data = [ 'id' => $id, 'name' => $_POST['name'], 'status' => $_POST['status'], 'remark' => $_POST['remark'], 'update_time' => time(), ]; if ($this->adminRoleModel->update($data) !== false) { $statusStr = $_POST['status'] ? '启用' : '禁用'; $LogStr = "编辑角色:" .$_POST['name']. ",状态:" . $statusStr; $this->insertLog($this->current_node,$LogStr,162); $this->success("保存成功!", url('rbac/index')); } else { $this->error("保存失败!"); } } } } public function roleDelete() { $id = (int)$this->request->param("id", 0, 'intval'); $type = (int)$this->request->param("type", 0, 'intval'); if (empty($id)) { $this->error("角色id不能为空!"); } $data = $this->adminRoleModel->field('id,parent_id,name')->order('id')->select(); $tree = new Tree(); $tree->init($data); $childrenlist = $tree->getChildrenIds($id,true); $count = $this->adminRoleUserModel->where(['role_id' => ['in', $childrenlist]])->count(); if ($count > 0) { $this->error("当前角色或其下级角色里有用户,无法删除!"); } else { if ( !$type && count($childrenlist) > 1) { $this->success("是否删除该角色?删除后它的所有下级角色也将被删除!"); } $name = $this->adminRoleModel->where('id',$id)->value('name'); $status = $this->adminRoleModel->delete($childrenlist); if (!empty($status)) { $nameStr = count($childrenlist) > 1 ? $name . '及其下级角色' : $name; $LogStr = "删除角色:" . $nameStr; $this->insertLog($this->current_node,$LogStr,162); $this->success("删除成功!", url('rbac/index')); } else { $this->error("删除失败!"); } } } /** * 权限设置页 * @return mixed|string */ public function authorize() { //角色ID $roleId = $this->request->param("id", 0, 'intval'); if (empty($roleId)) { $this->error("参数错误!"); } $tree = new Tree(); $tree->icon = ['│ ', '├─ ', '└─ ']; $tree->nbsp = ' '; $newMenus = []; //当前角色信息 $authInfo = $this->adminRoleModel->field('auth_ids,parent_id,name')->where(["id" => $roleId])->find(); $authIdsArr = explode(',', $authInfo['auth_ids']); if($authInfo['parent_id']>0) { //父级角色信息 $parentRoleInfo = $this->adminRoleModel->field('auth_ids')->where(["id" => $authInfo['parent_id']])->find(); //总的菜单树 $result = $this->adminAuthRuleModel->where(['id'=>['in',$parentRoleInfo['auth_ids']]])->select(); } else{ //总的菜单树 $result = $this->adminAuthRuleModel->select(); } $privilegeData = $this->adminAuthRuleModel->where('id', 'in', $authIdsArr)->column('auth_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 = "