23.展示所有权限 --前奏
当你分配权限要知道为谁分配权限,获取选中的角色数据,
RelePermission:1.rpRId:角色id,
2.rpPId:角色权限id,也就是部门id;
3.角色id对应权限id;
4.角色权限id也就是部门id.
==========1.扩展展示所有权限的接口===============
IOu_RolePermissionBLL.cs
代码:如下:去掉文件后缀
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IBLL { public partial interface IOu_RolePermissionBLL { MODEL.Ou_RolePermission GetPermissionByRoleId(int roleId); } }
业务层
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BLLA { public partial class Ou_RolePermission : IBLL.IOu_RolePermissionBLL { public MODEL.Ou_RolePermission GetPermissionByRoleId(int roleId) { //rpRId角色id,//对应的权限rpId // this.GetListBy(rp=>rp.rpRId==roleId)://拿到角色对应的权限 //this.GetListBy(rp=>rp.rpRId==roleId).FirstOrDefault().Ou_Permission. ://返回ef的代理里,所以返回有个导航属性Ou_Permission,所以当航属性点所有的就出来了。 this.GetListBy(rp=>rp.rpRId==roleId).FirstOrDefault().Ou_Permission. //拿到角色对应的权限,当前就是要拿到的权限。 return null; //throw new NotImplementedException(); } } }
==========2.扩展展示所有权限===============
那权限的方式1
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BLLA { public partial class Ou_RolePermission : IBLL.IOu_RolePermissionBLL { public MODEL.Ou_RolePermission GetPermissionByRoleId(int roleId) { //rpRId角色id,//对应的权限rpId // this.GetListBy(rp=>rp.rpRId==roleId)://拿到角色对应的权限 //this.GetListBy(rp=>rp.rpRId==roleId).FirstOrDefault().Ou_Permission. ://返回ef的代理里,所以返回有个导航属性Ou_Permission,所以当航属性点所有的就出来了。 this.GetListBy(rp => rp.rpRId == roleId).Select(rp => new MODEL.Ou_Permission() {//1.拿到角色对应的权限,当前就是要拿到的权限。
//2.这里属性可以一个个的写,写出来
});
returnnull;
} } }
==========23.展示所有权限 --前奏1,详情看代码===============
@{ Layout = null; } <!DOCTYPE html> <html> <head> <title>用户角色</title> <link href="~/EasyUI/themes/icon.css" rel="stylesheet" /> <link href="~/EasyUI/themes/default/easyui.css" rel="stylesheet" /> <script type="text/javascript" src="~/Scripts/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="~/EasyUI/jquery.easyui.min.js"></script> <script type="text/javascript" src="~/Scripts/jquery.validate.min.js"></script> <script type="text/javascript" src="~/Scripts/jquery.msgProcess.js"></script> <script type="text/javascript"> //=====================1.加载数据====================== $(function () { $('#Window').window('close');//编辑默认关闭 $("#tbList").datagrid({ width: 'auto', height: 400, striped: true, singleSelect: true, url: '/admin/Role/index', //queryParams:{}, loadMsg: '数据加载中请稍后……', pagination: true,//启用分页,默认每页10行 rownumbers: true,//显示页码,默认 提供 10 - 50 的页容量选择下拉框 pageSize: 10,//设置 页容量为 5 pageList: [10, 15, 20, 25, 30],//设置 页容量下拉框 fitColumns:true, columns: [[ { field: 'rIsShow', title: '显示', width:5,align: "center", formatter: function (value) { return value ? "√":"X"; } }, { field: 'rId', title: 'ID', width: 20, align: "center" }, { field: 'Ou_Department', title: '部门', width: 20, align: "center", formatter: function (value) { return value.depName } }, { field: 'rName', title: '角色名', width: 20, align: "center" }, { field: 'rIsDel', title: '是否删除', width: 20, align: "center", formatter: function (value) { switch (value.toString()) { case "false": return "否"; case "true": return "是"; } } }, { field: 'rRemark', title: '备注', width: 20, align: "center" }, ]], toolbar: [{ iconCls: 'icon-add', text: "新增", handler: AddRow },'-', { iconCls: 'icon-remove', text: "删除", handler: Delperm }, '-', { iconCls: 'icon-edit', text: "编辑", handler: editRow }, '-', { iconCls: 'icon-search', text: "分配权限", handler: providePermission //分配权限方法 } ], //设置选中行 onSelect: function (rowI, rD) { selectRowIndex = rowI;//1.1将中的行设置为全局变量 设置给全局变量 } }); initMoifyForm(); }) ////为表单 设置 参数 function initMoifyForm() { $('#modifyForm').form({ url: "/admin/sys/EditPermission", }); } //1.将中的行设置为全局变量 var selectRowIndex = -1; //编辑按钮方法 function editRow() { //1.获取选中行里的数据! var rowData = $("#tbList").datagrid("getSelected");//getSelected返回一个选中的行 if (rowData){ $('#Window').window('open');//打开 //load发送异步请求权限修改页面。并直接加载到 当前的 修改窗体中 //路径、data、回调函数 $.get("/admin/sys/EditPermission/" + rowData.pid, null, function (jsonObj) { //1.url,2.data,3.回调函数 if (jsonObj.Statu)//如果转了就是json对象,如果没转字符串就没有statu $.procAjaxData(jsonObj); else $('#Window').html(jsonObj); }); } }; //新增按钮方法 function AddRow() { $('#editWindow').window('open');//打开 $.get("/admin/sys/AddPermission/", null, function (jsonObj) { if (jsonObj.Statu) $.procAjaxData(jsonObj); else { $("#Window").html(jsonObj); } });//路径、data、回调函数 } //删除方法 function Delperm() { //1.获取选中行里的数据! var rowData = $("#tbList").datagrid("getSelected"); if (rowData) { //{id:rowData.pid} //$.post("/admin/sys/delpemission", rowData.pid, function () { }) $.post("/admin/sys/DelPermission", "id=" + rowData.pid, function (jsonObj) { $.procAjaxData(jsonObj, function () {//json统一处理方法:1.数据,2.删除成功后回调函数 $("#tbList").datagrid("reload");//删除成功后 刷新页面 }); //成功了 }, "JSON"); } } function viewSon() { var rowData = $("#tbList").datagrid("getSelected");//获取选中行 window.location = "/admin/sys/PermissionSon?pid=" + rowData.pid; } //分配权限 方法 function providePermission() { //获取选中 的 角色数据 var roleData = $("#tbList").datagrid("getSelected"); if (roleData) { //拿到角色id $.get("/admin/role/getroleTrue=id" + roleData.rId, null, function (jsobj) { if (jsobj.Statu) $.procAjaxData(jsobj); else $("#Window").html(jsobj); }); } else { $.alertMsg("请选中您要分配权限的角色数据", "系统提示"); } } </script> </head> <body> <table id="tbList" style="width:400px;height:250px"></table> <div id="Window" class="easyui-window" title="角色权限分配" resizable="true" data-options="iconCls:'icon-save',fit:true,modal:true"> </div> </body> </html>
控制器
方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using MVCOA.Helper; using MODEL.ViewModel; namespace MVCOA.Login.Admin { public class RoleController:Controller { #region 1. 加载角色数据 /// <summary> /// 角色 视图 /// </summary> /// <returns></returns> [HttpGet] public ActionResult index() { return View(); } [HttpPost] public ActionResult index(FormCollection form) { //获取页容量 int pageSize = int.Parse(Request.Form["rows"]); //获取请求的页面 int pageIndex = int.Parse(Request.Form["page"]); var list = OperateContext.Current.BLLSession.IOu_RoleBLL.GetPagedList(pageIndex, pageSize, p => p.rIsDel == false, p => p.rId).Select(p => p.ToPOCO()).ToList(); var rowcont = OperateContext.Current.BLLSession.IOu_RoleBLL.GetListBy(d => d.rIsDel == false).Count(); return Json(new MODEL.EasyUIModel.DataGridModel() { rows = list, total = rowcont }); } #endregion /// <summary> /// 3. 加载 所以的权限树[把对应的权限选中] /// </summary> /// <param name="id">角色id</param> /// <returns></returns> [HttpGet] public ActionResult getroleTrue(int id) { //1.获取已有的角色,2.获取所以的角色 //1.获取角色 权限 var listUserPer = OperateContext.Current.BLLSession.IOu_RolePermissionBLL.GetPermissionByRoleId(id);//扩展获取角色权限 //2.获取所以权限 var listAllper= OperateContext.Current.BLLSession.IOu_PermissionBLL.GetListBy(b => b.pIsDel == false).ToList(); //3.获取父 权限集合 var listparentPer = (from p in listAllper where p.pParent == 1 && p.pIsDel == false select p).ToList(); return View(new RolePermissionTree() { UserPer = listUserPer, AllPer = listAllper, Allparent = listparentPer }); } } }
//扩展获取角色权限
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BLLA { public partial class Ou_RolePermission : IBLL.IOu_RolePermissionBLL { public List< MODEL.Ou_Permission> GetPermissionByRoleId(int roleId) { //rpRId角色id,//对应的权限rpId // this.GetListBy(rp=>rp.rpRId==roleId):拿到角色对应的权限 //this.GetListBy(rp=>rp.rpRId==roleId).FirstOrDefault().Ou_Permission. :返回ef的代理里,所以返回有个导航属性Ou_Permission,所以当航属性点所有的就出来了。 // this.GetListBy(rp => rp.rpRId == roleId).Select(rp => new MODEL.Ou_Permission() { //1查询到权限,生成菜单的时候要权限就行了,不需要中间表 //2查询到的角色,中间表里面的,权限数据提取处理,当前返回的list集合是RolePermssion;角色权限的实体; //3当前要是是rolePermisson里的Permisson就可以了。 //4甚至不要这么多,用MODEL.ViewModel.Permission() return this.GetListBy(rp=>rp.rpRId==roleId).Select(rp=>rp.GetPerissionPart()).ToList(); //这里属性可以一个个的写,写出来 //拿到角色对应的权限,当前就是要拿到的权限。 } } }
权限分配视图:getroleTrue.cshtm
@model MODEL.ViewModel.RolePermissionTree @foreach (var per in Model.Allparent) { <ul id="tt" class="easyui-tree"> <li> <span>@per.pName</span> <!--生成改父节点的子节点--> <ul> @foreach(var son in Model.AllPer.Where(p => p.pParent == per.pid).ToList()) { <li> <span>@son.pName</span> </li> } </ul> </li> </ul> }