系列目錄javascript
過了個年回來,回顧一下,咱們上次講了角色管理,咱們這一次來說將權限受權給角色,這一節也是你們比較關心的。由於咱們已經跑通了整個系統,知道權限的流轉,咱們先來看一張圖html
這張圖主要分要3塊,角色組----系統模塊----操做碼受權java
選擇角色組(表示要受權的角色,選擇須要受權的模塊,最後受權操做碼。當存在一個操做碼時候,咱們應該改變SysRight表中的rightflag字段,表示他有權限。不知道你們是否還記得,這個圖也是咱們要作的。因爲圖中用的是JQGrid看起來跟Easyui有點差異,可是方式倒是同樣的)json
回到頭看到SysRightOperate表的IsValid,咱們將受權角色和模塊和操做碼這3張表關聯起來,其中IsValid字段是來標識是否有操做的權限,當第一次受權,那麼是向SysRightOperate添加一條記錄,若是下次更新先判斷是否已經受權若是沒有,那麼刪除或者更新IsValid,我這裏是更新,你也能夠刪除掉,同樣的道理。mvc
以前咱們已經新建過SysRight這個表的DAL層和BLL層了,根據想法,咱們只要創建多兩個方法ide
如今向ISysRightRepository添加2個方法post
//更新 int UpdateRight(SysRightOperate model); //按選擇的角色及模塊加載模塊的權限項 List<P_Sys_GetRightByRoleAndModule_Result> GetRightByRoleAndModule(string roleId, string moduleId);
P_Sys_GetRightByRoleAndModule_Result這個是存儲過程,因爲這2個方法比較複雜,這裏用存儲過程來作ui
Create proc [dbo].[P_Sys_GetRightByRoleAndModule] @roleId varchar(50),@moduleId varchar(50) as --按選擇的角色及模塊加載模塊的權限項 begin select a.Id,a.Name,a.KeyCode,a.ModuleId,ISNULL(b.IsValid,0) as isvalid,a.Sort,@roleId+@moduleId as RightId from SysModuleOperate a left outer join( select c.Id,a.IsValid from SysRightOperate a,SysRight b, SysModuleOperate c where RightId in (select Id From SysRight where RoleId =@roleId and ModuleId =@moduleId) and a.RightId=b.Id and b.ModuleId=c.ModuleId and a.KeyCode =c.KeyCode) b on a.Id = b.Id where a.ModuleId =@moduleId end
因此必需要把這個存儲過程添加到EF,並生成複雜類型的實體P_Sys_GetRightByRoleAndModule_Resulturl
而後建立P_Sys_UpdateSysRightRightFlagspa
Create proc [dbo].[P_Sys_UpdateSysRightRightFlag] @moduleId varchar(200),@roleId varchar(200) as begin --計算上級模塊的rightflag標識 declare @count int --第一層:由操做權限項計算模塊權限 select @count=COUNT(*) from SysRightOperate where RightId=@roleId+@moduleId and IsValid=1 if(@count>0) begin update SysRight set Rightflag=1 where ModuleId=@moduleId and RoleId=@roleId end else begin update SysRight set Rightflag=0 where ModuleId=@moduleId and RoleId=@roleId end --計算下一層 declare @parentId varchar(50) set @parentId=@moduleId while(@parentId<>'0') begin select @parentid=ParentId from SysModule where Id=@parentId if (@parentId is null) begin return end select @count=COUNT(*) from SysRight where ModuleId in (select Id from SysModule where ParentId=@parentId) and RoleId =@roleId and Rightflag=1 if(@count>0) begin update SysRight set Rightflag=1 where ModuleId=@parentId and RoleId=@roleId end else begin update SysRight set Rightflag=0 where ModuleId=@parentId and RoleId=@roleId end end end
這個是計算上級模塊的rightflag標識也就是開頭所說的RightFlag字段,這個字段將決定導航條的顯示,因此每一次受權操做都要執行
下面添加SysRightRepository邏輯代碼
public int UpdateRight(SysRightOperateModel model) { //轉換 SysRightOperate rightOperate = new SysRightOperate(); rightOperate.Id = model.Id; rightOperate.RightId = model.RightId; rightOperate.KeyCode = model.KeyCode; rightOperate.IsValid = model.IsValid; //判斷rightOperate是否存在,若是存在就更新rightOperate,不然就添加一條 using (DBContainer db = new DBContainer()) { SysRightOperate right = db.SysRightOperate.Where(a => a.Id == rightOperate.Id).FirstOrDefault(); if (right != null) { right.IsValid = rightOperate.IsValid; } else { db.SysRightOperate.AddObject(rightOperate); } if (db.SaveChanges() > 0) { //更新角色--模塊的有效標誌RightFlag var sysRight = (from r in db.SysRight where r.Id == rightOperate.RightId select r).First(); db.P_Sys_UpdateSysRightRightFlag(sysRight.ModuleId, sysRight.RoleId); return 1; } } return 0; } //按選擇的角色及模塊加載模塊的權限項 public List<P_Sys_GetRightByRoleAndModule_Result> GetRightByRoleAndModule(string roleId, string moduleId) { List<P_Sys_GetRightByRoleAndModule_Result> result = null; using (DBContainer db = new DBContainer()) { result = db.P_Sys_GetRightByRoleAndModule(roleId,moduleId).ToList(); } return result; }
按照習慣,咱們要向IBLL 和BLL 添加代碼,你們自行添加訪問DAL層的代碼便可
比較繁瑣的仍是Controller層和頁面UI的代碼,這些先貼出
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace App.Models.Sys { public class SysRightModelByRoleAndModuleModel { public string Ids { get; set; }// RightId+ KeyCode ids public string Name{ get; set; } public string KeyCode{ get; set; } public bool? IsValid{ get; set; } public string RightId{ get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; using App.IBLL; using App.Models; using App.Common; using App.Models.Sys; namespace App.Admin.Controllers { public class SysRightController : BaseController { // // GET: /SysRight/ [Dependency] public ISysRightBLL sysRightBLL { get; set; } [Dependency] public ISysRoleBLL sysRoleBLL { get; set; } [Dependency] public ISysModuleBLL sysModuleBLL { get; set; } [SupportFilter] public ActionResult Index() { ViewBag.Perm = GetPermission(); return View(); } //獲取角色列表 [SupportFilter(ActionName = "Index")] [HttpPost] public JsonResult GetRoleList(GridPager pager) { List<SysRoleModel> list = sysRoleBLL.GetList(ref pager, ""); var json = new { total = pager.totalRows, rows = (from r in list select new SysRoleModel() { Id = r.Id, Name = r.Name, Description = r.Description, CreateTime = r.CreateTime, CreatePerson = r.CreatePerson }).ToArray() }; return Json(json); } //獲取模組列表 [SupportFilter(ActionName = "Index")] [HttpPost] public JsonResult GetModelList(string id) { if (id == null) id = "0"; List<SysModuleModel> list = sysModuleBLL.GetList(id); var json = from r in list select new SysModuleModel() { Id = r.Id, Name = r.Name, EnglishName = r.EnglishName, ParentId = r.ParentId, Url = r.Url, Iconic = r.Iconic, Sort = r.Sort, Remark = r.Remark, Enable = r.Enable, CreatePerson = r.CreatePerson, CreateTime = r.CreateTime, IsLast = r.IsLast, state = (sysModuleBLL.GetList(r.Id).Count > 0) ? "closed" : "open" }; return Json(json); } //根據角色與模塊得出權限 [SupportFilter(ActionName = "Index")] [HttpPost] public JsonResult GetRightByRoleAndModule(GridPager pager, string roleId, string moduleId) { pager.rows = 100000; var right = sysRightBLL.GetRightByRoleAndModule(roleId,moduleId); var json = new { total = pager.totalRows, rows = (from r in right select new SysRightModelByRoleAndModuleModel() { Ids= r.RightId+ r.KeyCode, Name= r.Name, KeyCode =r.KeyCode, IsValid=r.isvalid, RightId=r.RightId }).ToArray() }; return Json(json); } //保存 [HttpPost] [SupportFilter(ActionName = "Save")] public int UpdateRight(SysRightOperateModel model) { return sysRightBLL.UpdateRight(model); } } }
@using App.Common; @using App.Admin; @{ ViewBag.Title = "角色受權設置"; Layout = "~/Views/Shared/_Index_Layout.cshtml"; List<App.Models.Sys.permModel> perm = (List<App.Models.Sys.permModel>)ViewBag.Perm; if (perm == null) { perm = new List<App.Models.Sys.permModel>(); } } <div class="mvctool"> @Html.ToolButton("btnSave", "icon-save", "保存", perm, "Save", true) </div> <table style="width: 100%"> <tbody> <tr> <td style="width: 420px; padding-right: 3px; vertical-align: top"> <table id="roleList"></table> </td> <td style="width: 200px; padding-right: 3px; vertical-align: top"> <table id="moduleList"></table> </td> <td> <table id="operateList"></table> </td> </tr> </tbody> </table> <script type="text/javascript"> $(function () { var curModuleId, curRoleId, curModuleName, curRoleName, curSystemId, curSystemName;//選擇的模塊ID,選中的角色ID,選中的模塊名稱,角色名稱 curRoleName = "?"; curModuleName = "?"; $('#roleList').datagrid({ url: '@Url.Action("GetRoleList")', width: 420, methord: 'post', height: $(window).height() - 35, fitColumns: true, sortName: 'CreateTime', sortOrder: 'desc', idField: 'Id', pageSize: 15, pageList: [15, 20, 30, 40, 50], pagination: true, striped: true, //奇偶行是否區分 singleSelect: true,//單選模式 rownumbers: true,//行號 title: '角色列表', columns: [[ { field: 'Id', title: '', width: 80, hidden: true }, { field: 'Name', title: '角色組', width: 80, sortable: true }, { field: 'Description', title: '說明', width: 80, sortable: true }, { field: 'CreateTime', title: '建立時間', width: 80, sortable: true }, { field: 'CreatePerson', title: '', width: 80, sortable: true, hidden: true } ]], onClickRow: function (index, data) { var row = $('#roleList').datagrid('getSelected'); if (row != null) { curRoleName = row.Name; curRoleId = row.Id; $('#operateList').datagrid({ url: "/SysRight/GetRightByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId + "" }); $('#operateList').datagrid({ 'title': "角色組: " + curRoleName + " >> 模塊:" + curModuleName }); } } }); $('#moduleList').treegrid({ url: '@Url.Action("GetModelList")', width: 300, methord: 'post', height: $(window).height() - 35, fitColumns: true, treeField: 'Name', idField: 'Id', pagination: false, striped: true, //奇偶行是否區分 singleSelect: true,//單選模式 title: '模塊列表', columns: [[ { field: 'Id', title: '惟一標識', width: 120, hidden: true }, { field: 'Name', title: '名稱', width: 220, sortable: true }, { field: 'EnglishName', title: '英文名稱', width: 80, sortable: true, hidden: true }, { field: 'ParentId', title: '上級Id', width: 80, sortable: true, hidden: true }, { field: 'Url', title: '連接地址', width: 80, sortable: true, hidden: true }, { field: 'Iconic', title: '圖標', width: 80, sortable: true, hidden: true }, { field: 'Sort', title: '排序號', width: 80, sortable: true, hidden: true }, { field: 'Remark', title: '說明', width: 80, sortable: true, hidden: true }, { field: 'Enable', title: '是否啓用', width: 60, align: 'center', formatter: function (value) { if (value) { return "<img src='/Content/Images/icon/pass.png'/>"; } else { return "<img src='/Content/Images/icon/no.png'/>"; } }, hidden: true }, { field: 'CreatePerson', title: '建立人', width: 80, sortable: true, hidden: true }, { field: 'CreateTime', title: '建立時間', width: 120, sortable: true, hidden: true }, { field: 'IsLast', title: '是否最後一項', align: 'center', width: 100, formatter: function (value) { if (value) { return "是"; } else { return "否"; } }, hidden: true }, ]], onClickRow: function (index, data) { var row = $('#moduleList').treegrid('getSelected'); if (row != null) { curModuleName = row.Name; curModuleId = row.Id; if (curRoleId == null && row.IsLast) { $.messageBox5s('提示', "請再選擇一個角色!"); return; } $('#operateList').datagrid({ url: "/SysRight/GetRightByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId + "" }); $('#operateList').datagrid({ 'title': "角色組: " + curRoleName + " >> 模塊:" + (row.IsLast ? curModuleName : "[請再選擇最後菜單項]") }); } } }); $('#operateList').datagrid({ url: '@Url.Action("GetRightByRoleAndModule")', width: $(window).width() - 736, methord: 'post', height: $(window).height() - 35, fitColumns: true, sortName: 'CreateTime', sortOrder: 'desc', idField: 'Id', striped: true, //奇偶行是否區分 singleSelect: true,//單選模式 title: '受權操做', //rownumbers: true,//行號 columns: [[ { field: 'Ids', title: 'Ids', width: 80, hidden: true }, { field: 'Name', title: '名稱', width: 80, sortable: true }, { field: 'KeyCode', title: '操做碼', width: 80, sortable: true }, { field: 'IsValid', title: "<a href='#' title='@Suggestion.Select' onclick=\"SelAll();\" ><img src='/Content/Images/icon/select.gif'></a> <a href='#' title='@Suggestion.UnSelect' onclick=\"UnSelAll();\" ><img src='/Content/Images/icon/unselect.gif'></a>", align: 'center', width: 30, formatter: function (value) { if (value) { return "<input type='checkbox' checked='checked' value=" + value + " />"; } else { return "<input type='checkbox' value=" + value + " />"; } }, }, { field: 'RightId', title: '模塊ID', width: 80, sortable: true, hidden: true } ]] }); $("#btnSave").click(function () { var updateRows = 0; var rows = $("#operateList").datagrid("getRows"); //這段代碼是獲取當前頁的全部行。 for (var i = 0; i < rows.length; i++) { var setFlag = $("td[field='IsValid'] input").eq(i).prop("checked"); if (rows[i].IsValid != setFlag)//判斷是否有做修改 { $.post("@Url.Action("UpdateRight")", { "Id": rows[i].Ids, "RightId": rows[i].RightId, "KeyCode": rows[i].KeyCode, "IsValid": setFlag }, "json"); updateRows++; } } if (updateRows > 0) { $.messageBox5s('提示', '保存成功!'); } else { $.messageBox5s('提示', '@Suggestion.NoAnyChanges!'); } }); $(window).resize(function () { $('#operateList').datagrid('resize', { width: $(window).width() - 736, height: $(window).height() - 35 }).datagrid('resize', { width: $(window).width() - 736, height: $(window).height() - 35 }); $('#moduleList,#roleList').datagrid('resize', { height: $(window).height() - 35 }).datagrid('resize', { height: $(window).height() - 35 }); }); }); function SelAll() { $("td[field='IsValid'] input").prop("checked", true); $("#btnSave").trigger("click"); return; } function UnSelAll() { $("td[field='IsValid'] input").prop("checked", false); $("#btnSave").trigger("click"); return; } </script>
最後效果圖
此次發佈仍是作得比較認真的。你們能夠詳細細讀代碼和存儲過程。不清楚的歡迎留言,一定回答
接下來是講角色和用戶的互相受權,有興趣的朋友能夠先作作看。
最後更新2個js方法來替換DataGrid中的width和height計算
function SetGridWidthSub(w) { return $(window).width() - w; } function SetGridHeightSub(h) { return $(window).height() - h }