索引javascript
簡述css
今天咱們來作權限的管理,這篇比較多 但願新手朋友慢慢消化html
項目準備前端
咱們用的工具是:VS 2013 + SqlServer 2012 + IIS7.5java
但願你們對ASP.NET MVC有一個初步的理解,理論性的東西咱們不作過多解釋,有些地方不理解也不要緊,會用就好了,用的多了,用的久了,天然就理解了。node
項目開始jquery
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace WebPage.Areas.SysManage.Controllers 8 { 9 public class PermissionController : Controller 10 { 11 // GET: SysManage/Permission 12 public ActionResult Index() 13 { 14 return View(); 15 } 16 } 17 }
1 public class PermissionController : BaseController 2 { 3 #region 聲明容器 4 /// <summary> 5 /// 系統管理 6 /// </summary> 7 ISystemManage SystemManage { get; set; } 8 /// <summary> 9 /// 權限管理 10 /// </summary> 11 IPermissionManage PermissionManage { get; set; } 12 /// <summary> 13 /// 模塊管理 14 /// </summary> 15 IModuleManage ModuleManage { get; set; } 16 #endregion 17 18 public ActionResult Index() 19 { 20 return View(); 21 } 22 }
配置注入:ajax
完整代碼:spring
1 <?xml version="1.0" encoding="utf-8" ?> 2 <objects xmlns="http://www.springframework.net"> 3 <description>Spring注入控制器,容器指向Service層封裝的接口</description> 4 <!--系統管理 Begin--> 5 <!--主頁控制器--> 6 <object type="WebPage.Areas.SysManage.Controllers.HomeController,WebPage" singleton="false"> 7 <property name="ModuleManage" ref="Service.Module"/> 8 </object> 9 <!--登陸控制器--> 10 <object type="WebPage.Areas.SysManage.Controllers.AccountController,WebPage" singleton="false"> 11 <property name="UserManage" ref="Service.User"/> 12 </object> 13 <!--模塊管理--> 14 <object type="WebPage.Areas.SysManage.Controllers.ModuleController,WebPage" singleton="false"> 15 <property name="ModuleManage" ref="Service.Module"/> 16 <property name="PermissionManage" ref="Service.Permission"/> 17 <property name="SystemManage" ref="Service.System"/> 18 </object> 19 <!--權限管理--> 20 <object type="WebPage.Areas.SysManage.Controllers.PermissionController,WebPage" singleton="false"> 21 <property name="ModuleManage" ref="Service.Module"/> 22 <property name="SystemManage" ref="Service.System"/> 23 <property name="PermissionManage" ref="Service.Permission"/> 24 </object> 25 <!--系統管理 end--> 26 </objects>
OK,這樣 控制器和容器聲明 咱們就完成了~express
咱們把權限管理的頁面 分左右兩欄 左欄是全部的模塊 右欄是權限
效果是這樣的:
咱們先來作左欄 加載系統模塊,咱們添加一個下拉菜單,讓用戶選擇操做的系統
1 <select id="sel-system" > 2 <option value="選擇系統"></option> 3 </select>
這裏的下拉選項,咱們是應該輸出到頁面上的,因此 咱們在 視圖Home 下面獲取這個系統集合
1 /// <summary> 2 /// 權限管理 默認頁面 3 /// </summary> 4 /// <returns></returns> 5 [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")] 6 public ActionResult Home() 7 { 8 try 9 { 10 //獲取用戶可操做的系統列表 11 ViewData["Systemlist"] = this.SystemManage.LoadSystemInfo(CurrentUser.System_Id); 12 } 13 catch(Exception e) 14 { 15 WriteLog(Common.Enums.enumOperator.Select, "對模塊權限按鈕的管理加載導航頁:", e); 16 } 17 18 return View(); 19 }
咱們修改一下select 的 option 經過接收 後臺的ViewData["Systemlist"] 輸出 下拉選項
1 <select id="sel-system"> 2 @{ 3 foreach (var item in ViewData["Systemlist"] as dynamic) 4 { 5 <option value="@item.ID">@item.NAME</option> 6 } 7 } 8 </select>
而後就是樹形菜單了,之前用的是jquery.ztree.core-3.5 今天換一換 用 jstree,咱們看一下jstree json的格式
1 $('#using_json_2').jstree({ 'core' : { 2 'data' : [ 3 { "id" : "ajson1", "parent" : "#", "text" : "Simple root node" }, 4 { "id" : "ajson2", "parent" : "#", "text" : "Root node 2" }, 5 { "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" }, 6 { "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" }, 7 ] 8 } });
固然,這是默認關閉的 展開呢 就是在屬性里加上"state": { "opened": true },我要關閉的,我就不加這個了,那麼按照這個格式,咱們寫一個方法,返回咱們模塊的json數據
這個方法 咱們起名叫作 GetTree
1 /// <summary> 2 /// 獲取模塊樹形菜單 3 /// </summary> 4 public ActionResult GetTree() 5 { 6 var json = new JsonHelper() { Msg = "Success", Status = "y" }; 7 8 //獲取系統ID 9 var sysId = Request.Form["sysId"]; 10 11 //判斷系統ID是否傳入 12 if (string.IsNullOrEmpty(sysId)) 13 { 14 json.Status = "n"; 15 json.Msg = "獲取模塊失敗!"; 16 return Json(json); 17 } 18 try 19 { 20 //獲取系統下的模塊列表 按照 SHOWORDER字段 升序排列 21 var query = this.ModuleManage.LoadAll(p => p.FK_BELONGSYSTEM == sysId).OrderBy(p => p.SHOWORDER).ToList(); 22 23 //這裏就是按照jsTree的格式 輸出一下 模塊信息 24 var result = query.Select(m => new 25 { 26 id = m.ID, 27 parent = m.PARENTID>0?m.PARENTID.ToString():"#", 28 text = m.NAME, 29 icon = m.LEVELS == 0 ? "fa fa-circle text-danger" : "fa fa-circle text-navy" 30 }).ToList(); 31 32 json.Data = result; 33 } 34 catch (Exception e) 35 { 36 json.Status = "n"; 37 json.Msg = "服務器忙,請稍後再試!"; 38 WriteLog(Common.Enums.enumOperator.Select, "權限管理,獲取模塊樹:", e); 39 } 40 return Json(json); 41 }
再回到咱們Home視圖頁,用jstree呢 首先咱們要引入它的css
而後引入它的js
咱們在頁面中新建一個DIV 來存放這個樹形菜單
寫個簡潔的ajax獲取數據 而且填充給上面那個DIV
$.post("/permission/gettree", { sysId: $("#sel-system").val() }, function (res) { if (res.Status == "y") { $("#ModuleTree").jstree({ "core": { "multiple": false, "data": res.Data } }).on("changed.jstree", function (e, data) { alert(data.instance.get_node(data.selected).text); }); } else { dig.error(res.Msg); } });
由於咱們這個是要選擇系統 而後列出系統下的樹形菜單的,因此咱們把這個ajax方法 寫到一個function方法裏
1 function ShowMoudle() 2 { 3 $("#ModuleTree").data('jstree', false).empty(); 4 $.post("/permission/gettree", { sysId: $("#sel-system").val() }, function (res) { 5 if (res.Status == "y") { 6 $("#ModuleTree").jstree({ 7 "core": { "multiple": false, "data": res.Data } 8 }).on("changed.jstree", function (e, data) { 9 alert(data.instance.get_node(data.selected).text); 10 }); 11 } 12 else { 13 dig.error(res.Msg); 14 } 15 }); 16 }
頁面打開和系統下拉菜單select 更改的時候 加載這個菜單,下面是完整的
OK,是否是出來了
那接下來,咱們要點擊模塊以後 操做模塊的權限,咱們列出了樹形菜單,上面作了一個方法就是 選中 菜單的時候 彈出 菜單的text
咱們要作的是,點擊菜單的是後展現這個模塊的權限,因此,咱們先新建一個權限列表頁
1 /// <summary> 2 /// 權限管理 權限列表 3 /// </summary> 4 /// <returns></returns> 5 [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")] 6 public ActionResult Index() 7 { 8 return View(); 9 }
1 /// <summary> 2 /// 權限管理 權限列表 3 /// </summary> 4 /// <returns></returns> 5 [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")] 6 public ActionResult Index() 7 { 8 try 9 { 10 return View(); 11 } 12 catch (Exception e) 13 { 14 WriteLog(Common.Enums.enumOperator.Select, "對模塊權限按鈕的管理加載主頁:", e); 15 throw e.InnerException; 16 } 17 }
1 /// <summary> 2 /// 權限管理 權限列表 3 /// </summary> 4 /// <returns></returns> 5 [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")] 6 public ActionResult Index() 7 { 8 try 9 { 10 //獲取模塊ID 11 var moduleId = Request.QueryString["moduleId"] ?? (Request["moduleId"] ?? ""); 12 13 //若是模塊ID不爲空或NULL 14 if(!string.IsNullOrEmpty(moduleId)) 15 { 16 17 } 18 19 return View(); 20 } 21 catch (Exception e) 22 { 23 WriteLog(Common.Enums.enumOperator.Select, "對模塊權限按鈕的管理加載主頁:", e); 24 throw e.InnerException; 25 } 26 }
1 /// <summary> 2 /// 權限管理 權限列表 3 /// </summary> 4 /// <returns></returns> 5 [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "View")] 6 public ActionResult Index() 7 { 8 try 9 { 10 //獲取模塊ID 11 var moduleId = Request.QueryString["moduleId"] ?? (Request["moduleId"] ?? ""); 12 13 //若是模塊ID不爲空或NULL 14 if(!string.IsNullOrEmpty(moduleId)) 15 { 16 //把模塊ID轉爲Int 17 int module_Id = int.Parse(moduleId); 18 19 //模塊信息 20 var module = this.ModuleManage.Get(p => p.ID == module_Id); 21 22 //綁定列表 23 var query = this.PermissionManage.LoadAll(p => p.MODULEID == module.ID); 24 25 //關鍵字查詢 26 if (!string.IsNullOrEmpty(keywords)) 27 { 28 query = query.Where(p => p.NAME.Contains(keywords)); 29 } 30 //輸出結果 31 var result = query.OrderBy(p => p.SHOWORDER).ToList(); 32 33 ViewBag.Search = base.keywords; 34 35 ViewBag.Module = module; 36 37 return View(result); 38 } 39 40 return View(); 41 } 42 catch (Exception e) 43 { 44 WriteLog(Common.Enums.enumOperator.Select, "對模塊權限按鈕的管理加載主頁:", e); 45 throw e.InnerException; 46 } 47 }
1 @{ 2 Layout = "~/Views/Shared/_Layout.cshtml"; 3 } 4 @model List<Domain.SYS_PERMISSION> 5 <div class="wrapper wrapper-content animated fadeInRight"> 6 <div class="row"> 7 <div class="col-sm-12"> 8 <div class="ibox float-e-margins"> 9 <div class="ibox-title"> 10 @{ 11 if (ViewBag.Module != null) 12 { 13 Domain.SYS_MODULE module = ViewBag.Module as Domain.SYS_MODULE; 14 if (module != null) 15 { 16 <h5>@(module.NAME) - 權限</h5> 17 @Html.Hidden("moduleId", module.ID) 18 @Html.Hidden("moduleType", module.MODULETYPE) 19 } 20 21 <div class="ibox-tools"> 22 <a class="btn btn-primary btn-xs p310" id="reset" action="reset"><i class="im-plus"></i> 初始化權限</a> 23 <a class="btn btn-primary btn-xs p210" id="insert" action="add"><i class="fa fa-plus-circle fa-fw"></i> 建立新分類</a> 24 <a class="btn btn-warning btn-xs p210" id="modify" action="edit"><i class="fa fa-pencil fa-fw"></i> 編輯</a> 25 <a class="btn btn-danger btn-xs p210" id="delete" action="remove"><i class="fa fa-trash-o fa-fw"></i> 刪除</a> 26 <a class="reload-link" style="color: #c4c4c4" href="javascript:dig.reload()" data-toggle="tooltip" data-placement="left" title="刷新"> 27 <i class="fa fa-repeat fa-lg"></i> 28 </a> 29 </div> 30 } 31 else 32 { 33 <h5>權限管理</h5> 34 } 35 } 36 </div> 37 <div class="ibox-content"> 38 @using (Ajax.BeginForm("Index", null, new AjaxOptions() { }, new { @id = "form1", @class = "form-horizontal", @method = "get" })) 39 { 40 <div class="row"> 41 <div class="col-sm-9"> 42 </div> 43 <div class="col-sm-3"> 44 <div class="input-group"> 45 @Html.TextBox("Search", null, new { @class = "input-sm form-control", @placeholder = "請輸入查詢關鍵詞" }) 46 <span class="input-group-btn"> 47 <button type="submit" onclick="submit()" class="btn btn-sm btn-primary"> 搜索</button> 48 </span> 49 </div> 50 </div> 51 </div> 52 } 53 <div class="row"> 54 <table id="dataTable" class="table table-striped table-bordered table-hover dataTables-example" style="text-align:center;"> 55 <thead> 56 <tr> 57 <th class="tn" style="width: 50px !important"><input name="checkall" class="icheck_box" type="checkbox" value=""></th> 58 <th>所屬系統</th> 59 <th>權限動做</th> 60 <th>圖標</th> 61 <th>順序</th> 62 </tr> 63 </thead> 64 <tbody> 65 @{ 66 if (Model != null) 67 { 68 @Html.Hidden("percount", Model.Count) 69 foreach (var item in Model) 70 { 71 <tr> 72 <td class="tn"><input name="checkbox_name" class="icheck_box" type="checkbox" value="@item.ID"></td> 73 <td><a href="javascript:modify('@item.ID')" listaction="detail"> @item.NAME</a></td> 74 <td>@item.PERVALUE</td> 75 <td style="color:#1ab394; "><i class="@item.ICON"></i></td> 76 <td>@item.SHOWORDER</td> 77 </tr> 78 } 79 } 80 } 81 </tbody> 82 </table> 83 </div> 84 </div> 85 </div> 86 </div> 87 </div> 88 </div> 89 @section scripts{ 90 <script type="text/javascript"> 91 </script> 92 }
iFrameHeight()是設置Iframe的高度,咱們把這個方法寫在home視圖裏
1 function iFrameHeight() { 2 var ifm = document.getElementById("DeployBase"); 3 var subWeb = document.frames ? document.frames["DeployBase"].document : ifm.contentDocument; 4 if (ifm != null && subWeb != null) { 5 ifm.height = subWeb.body.scrollHeight + 30; 6 } 7 }
OK,到這裏 咱們 Home應該是完成了,下面就是 權限的添加、修改、刪除了
1 /// <summary> 2 /// 加載權限詳情 3 /// </summary> 4 /// <param name="id"></param> 5 /// <returns></returns> 6 [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Detail")] 7 public ActionResult Detail(int? id) 8 { 9 try 10 { 11 var _entity = this.PermissionManage.Get(p => p.ID == id) ?? new Domain.SYS_PERMISSION(); 12 13 //獲取模塊ID 14 var moduleId = Request.QueryString["moduleId"]; 15 16 if (!string.IsNullOrEmpty(moduleId)) 17 { 18 int newmoduleid = int.Parse(moduleId); 19 _entity.MODULEID = newmoduleid; 20 } 21 22 return View(_entity); 23 } 24 catch (Exception e) 25 { 26 WriteLog(Common.Enums.enumOperator.Select, "對模塊權限按鈕的管理加載詳情:", e); 27 throw e.InnerException; 28 } 29 }
咱們要用到表SYS_CODE你們可能已經有這個表了,可是沒有數據 我給你們一些默認數據
1 USE [wkmvc_db] 2 GO 3 /****** Object: Table [dbo].[SYS_CODE] Script Date: 2016/6/1 17:37:41 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 CREATE TABLE [dbo].[SYS_CODE]( 9 [ID] [int] IDENTITY(1,1) NOT NULL, 10 [CODETYPE] [nvarchar](50) NULL, 11 [NAMETEXT] [nvarchar](200) NULL, 12 [CODEVALUE] [nvarchar](100) NULL, 13 [SHOWORDER] [int] NULL, 14 [ISCODE] [int] NULL, 15 [REMARK] [nvarchar](2000) NULL, 16 [CREATEDATE] [datetime] NULL, 17 [CREATEUSER] [nvarchar](36) NULL, 18 [UPDATEDATE] [datetime] NULL, 19 [UPDATEUSER] [nvarchar](36) NULL, 20 [PARENTID] [int] NULL, 21 CONSTRAINT [PK_SYS_CODE] PRIMARY KEY CLUSTERED 22 ( 23 [ID] ASC 24 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 25 ) ON [PRIMARY] 26 27 GO 28 SET IDENTITY_INSERT [dbo].[SYS_CODE] ON 29 30 GO 31 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (1, N'ROLEVALUE', N'刪除', N'Remove', 4, 1, N'刪除所用到的權限值', CAST(0x0000A57500A49432 AS DateTime), N'管理員', CAST(0x0000A57500A49432 AS DateTime), N'管理員', 0) 32 GO 33 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (2, N'ROLEVALUE', N'導出', N'Export', 5, 1, N'導出所用到的權限值', CAST(0x0000A57500A49437 AS DateTime), N'管理員', CAST(0x0000A57500A49437 AS DateTime), N'管理員', 0) 34 GO 35 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (3, N'ROLEVALUE', N'導入', N'Import', 6, 1, N'導入所用到的權限值', CAST(0x0000A57500A49438 AS DateTime), N'管理員', CAST(0x0000A57500A49438 AS DateTime), N'管理員', 0) 36 GO 37 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (4, N'ROLEVALUE', N'審覈', N'Audit', 7, 1, N'審覈用到的權限值', CAST(0x0000A57500A49438 AS DateTime), N'管理員', CAST(0x0000A57500A49438 AS DateTime), N'管理員', 0) 38 GO 39 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (5, N'ROLEVALUE', N'回覆', N'Reply', 8, 1, N'回覆用到的權限值', CAST(0x0000A57500A49438 AS DateTime), N'管理員', CAST(0x0000A57500A49438 AS DateTime), N'管理員', 0) 40 GO 41 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (6, N'ROLEVALUE', N'分配', N'Allocation', 9, 1, N'分配用到的權限值', CAST(0x0000A57500A49438 AS DateTime), N'管理員', CAST(0x0000A57500A49438 AS DateTime), N'管理員', 0) 42 GO 43 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (7, N'ROLEVALUE', N'選擇', N'Select', 10, 1, N'選擇用到的權限值', CAST(0x0000A57500A49438 AS DateTime), N'管理員', CAST(0x0000A57500A49438 AS DateTime), N'管理員', 0) 44 GO 45 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (8, N'ROLEVALUE', N'上傳', N'Upload', 11, 1, N'上傳用到的權限值', CAST(0x0000A57500A49438 AS DateTime), N'管理員', CAST(0x0000A57500A49438 AS DateTime), N'管理員', 0) 46 GO 47 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (9, N'ROLEVALUE', N'下載', N'Download', 12, 1, N'下載用到的權限值', CAST(0x0000A57500A49439 AS DateTime), N'管理員', CAST(0x0000A57500A49439 AS DateTime), N'管理員', 0) 48 GO 49 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (10, N'ROLEVALUE', N'列表', N'List', 1, 1, N'列表用到的權限值', CAST(0x0000A57500A49439 AS DateTime), N'管理員', CAST(0x0000A57500A49439 AS DateTime), N'管理員', 0) 50 GO 51 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (14, N'ROLEVALUE', N'查看', N'View', 0, 1, N'查看所用到的權限值', CAST(0x0000A57500A49439 AS DateTime), N'管理員', CAST(0x0000A57500A49439 AS DateTime), N'管理員', 0) 52 GO 53 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (15, N'ROLEVALUE', N'詳情', N'Detail', 1, 1, N'詳情所用到的權限值', CAST(0x0000A57500A49439 AS DateTime), N'管理員', CAST(0x0000A57500A49439 AS DateTime), N'管理員', 0) 54 GO 55 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (16, N'ROLEVALUE', N'添加', N'Add', 2, 1, N'添加所用到的權限值', CAST(0x0000A57500A49439 AS DateTime), N'管理員', CAST(0x0000A57500A49439 AS DateTime), N'管理員', 0) 56 GO 57 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (17, N'ROLEVALUE', N'編輯', N'Edit', 3, 1, N'編輯用到的權限值', CAST(0x0000A57500A49439 AS DateTime), N'管理員', CAST(0x0000A57500A49439 AS DateTime), N'管理員', 0) 58 GO 59 INSERT [dbo].[SYS_CODE] ([ID], [CODETYPE], [NAMETEXT], [CODEVALUE], [SHOWORDER], [ISCODE], [REMARK], [CREATEDATE], [CREATEUSER], [UPDATEDATE], [UPDATEUSER], [PARENTID]) VALUES (18, N'ROLEVALUE', N'發佈', N'Publish', 14, 1, N'發佈所需權限', CAST(0x0000A57500A49439 AS DateTime), N'管理員', CAST(0x0000A57500A49439 AS DateTime), N'管理員', 0) 60 GO 61 62 SET IDENTITY_INSERT [dbo].[SYS_CODE] OFF 63 GO 64 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主鍵ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'ID' 65 GO 66 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'代碼類型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'CODETYPE' 67 GO 68 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'代碼顯示文本' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'NAMETEXT' 69 GO 70 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'代碼值' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'CODEVALUE' 71 GO 72 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'排序值' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'SHOWORDER' 73 GO 74 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否爲編碼(0否1是)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'ISCODE' 75 GO 76 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'備註' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'REMARK' 77 GO 78 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'建立時間' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'CREATEDATE' 79 GO 80 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'建立者' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'CREATEUSER' 81 GO 82 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改時間' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'UPDATEDATE' 83 GO 84 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改者' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'UPDATEUSER' 85 GO 86 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'父級ID(使用時注意ISCODE爲0)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SYS_CODE', @level2type=N'COLUMN',@level2name=N'PARENTID' 87 GO
1 using System.Collections.Generic; 2 using System.Linq; 3 4 namespace Service.IService 5 { 6 /// <summary> 7 /// Service層代碼配置接口 8 /// add yuangang by 2015-05-22 9 /// </summary> 10 public interface ICodeManage : IRepository<Domain.SYS_CODE> 11 { 12 /// <summary> 13 /// 根據編碼類型獲取編碼集合 14 /// </summary> 15 /// <param name="codetype">編碼類型</param> 16 /// <param name="codevalue">編碼值</param> 17 List<Domain.SYS_CODE> GetCode(string codetype, params string[] codevalue); 18 /// <summary> 19 /// 經過字典查詢字典指向的編碼集合 20 /// </summary> 21 IQueryable<Domain.SYS_CODE> GetDicType(); 22 /// <summary> 23 /// 根據字典ID與類型獲取一條數據 24 /// </summary> 25 string GetCodeByID(int id, string codetype); 26 /// <summary> 27 /// 根據字典編碼值與類型獲取一條數據 28 /// </summary> 29 string GetCodeNameByCodeValue(string codeType, string codevalue); 30 } 31 }
1 using Common; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Data.Entity; 7 8 namespace Service.ServiceImp 9 { 10 /// <summary> 11 /// Service層代碼配置 12 /// add yuangang by 2015-05-22 13 /// </summary> 14 public class CodeManage : RepositoryBase<Domain.SYS_CODE>, IService.ICodeManage 15 { 16 /// <summary> 17 /// 根據編碼類型獲取編碼集合 18 /// </summary> 19 public List<Domain.SYS_CODE> GetCode(string codetype, params string[] codevalue) 20 { 21 var predicate = PredicateBuilder.True<Domain.SYS_CODE>(); 22 predicate = predicate.And(p => p.CODETYPE == codetype); 23 if (codevalue != null && codevalue.Length > 0) 24 { 25 var str = codevalue.ToList(); 26 predicate = predicate.And(p => str.Contains(p.CODEVALUE)); 27 } 28 return this.LoadAll(predicate).OrderBy(p => p.SHOWORDER).ToList(); 29 } 30 31 /// <summary> 32 /// 經過系統字典獲取編碼值 33 /// </summary> 34 public IQueryable<Domain.SYS_CODE> GetDicType() 35 { 36 Dictionary<string, string> code = Common.Enums.ClsDic.DicCodeType; 37 string dic = code.Aggregate(string.Empty, (current, item) => current + (item.Value + ",")).TrimEnd(','); 38 return this.LoadAll(p => dic.Contains(p.CODETYPE)).OrderBy(p => p.SHOWORDER); 39 } 40 41 /// <summary> 42 /// 根據字典ID與類型獲取一條數據 43 /// </summary> 44 public string GetCodeByID(int id, string codetype) 45 { 46 return (this.Get(p => p.ID == id) ?? new Domain.SYS_CODE()).NAMETEXT; 47 } 48 49 /// <summary> 50 /// 根據字典編碼值與類型獲取一條數據 51 /// </summary> 52 public string GetCodeNameByCodeValue(string codeType, string codevalue) 53 { 54 if (string.IsNullOrEmpty(codevalue)) 55 return ""; 56 var entity = this.Get(p => p.CODETYPE == codeType && p.CODEVALUE == codevalue); 57 if (entity == null) return ""; 58 return entity.NAMETEXT; 59 } 60 } 61 }
注意:這裏咱們用到了一個 謂詞表達式構建器
我把這個類 貼給你們,你們放到Common類庫下面就能夠
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Linq.Expressions; 5 using System.Text; 6 7 namespace Common 8 { 9 /// <summary> 10 /// 謂詞表達式構建器 11 /// add yuangang by 2015-09-08 12 /// </summary> 13 public static class PredicateBuilder 14 { 15 /// <summary> 16 /// 機關函數應用True時:單個AND有效,多個AND有效;單個OR無效,多個OR無效;混應時寫在AND後的OR有效 17 /// </summary> 18 /// <typeparam name="T"></typeparam> 19 /// <returns></returns> 20 public static Expression<Func<T, bool>> True<T>() { return f => true; } 21 22 /// <summary> 23 /// 機關函數應用False時:單個AND無效,多個AND無效;單個OR有效,多個OR有效;混應時寫在OR後面的AND有效 24 /// </summary> 25 /// <typeparam name="T"></typeparam> 26 /// <returns></returns> 27 public static Expression<Func<T, bool>> False<T>() { return f => false; } 28 29 public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, 30 Expression<Func<T, bool>> expr2) 31 { 32 return expr1.Compose(expr2, Expression.Or); 33 } 34 35 public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, 36 Expression<Func<T, bool>> expr2) 37 { 38 return expr1.Compose(expr2, Expression.And); 39 } 40 public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge) 41 { 42 // build parameter map (from parameters of second to parameters of first) 43 var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f); 44 45 // replace parameters in the second lambda expression with parameters from the first 46 var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); 47 48 // apply composition of lambda expression bodies to parameters from the first expression 49 return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters); 50 } 51 } 52 public class ParameterRebinder : ExpressionVisitor 53 { 54 private readonly Dictionary<ParameterExpression, ParameterExpression> map; 55 56 public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) 57 { 58 this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>(); 59 } 60 61 public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) 62 { 63 return new ParameterRebinder(map).Visit(exp); 64 } 65 66 protected override Expression VisitParameter(ParameterExpression p) 67 { 68 ParameterExpression replacement; 69 if (map.TryGetValue(p, out replacement)) 70 { 71 p = replacement; 72 } 73 return base.VisitParameter(p); 74 } 75 } 76 }
1 <!--預置編碼管理--> 2 <object id="Service.Code" type="Service.ServiceImp.CodeManage,Service" singleton="false"> 3 </object>
1 @{ 2 Layout = "~/Views/Shared/_Layout.cshtml"; 3 } 4 @model Domain.SYS_PERMISSION 5 <style type="text/css"> 6 .gray-bg {background-color: white;} 7 .changeicon {float: right;margin-right: -52px;border: none;background-color: #18a689;color: #FFFFFF;height: 34px;margin-top: -7px;} 8 .removeicon {background-color: #f8ac59;float: right;margin-right: -92px;border: none;color: #FFFFFF;height: 34px;margin-top: -7px;} 9 </style> 10 @using (Ajax.BeginForm("Save", null, new AjaxOptions() 11 { 12 HttpMethod = "Post", 13 OnBegin = "SubAjax.Loading", 14 OnComplete = "SubAjax.Complate", 15 OnFailure = "SubAjax.Failure", 16 OnSuccess = "SubAjax.Success" 17 }, 18 new { @class = "form-horizontal dig-from", @role = "form" })) 19 { 20 @Html.HiddenFor(p => p.ID) 21 @Html.HiddenFor(p => p.MODULEID) 22 <div class="wrapper wrapper-content animated fadeInUp"> 23 <div class="row"> 24 <div class="ibox-detail-title"> 25 <i class="fa fa-pencil-square-o"></i>添加/修改權限 26 </div> 27 <div class="ibox-content"> 28 <div class="form-group"> 29 <label class="col-sm-3 control-label">權限名稱:</label> 30 <div class="col-sm-8"> 31 @Html.TextBoxFor(p => p.NAME, new { @class = "form-control", @id = "modulename", @placeholder = "請輸入權限名稱", @datatype = "*", @nullmsg = "請輸入權限名稱!", @errormsg = "請輸入權限名稱!" }) 32 </div> 33 </div> 34 <div class="hr-line-dashed"></div> 35 <div class="row"> 36 <div class="col-sm-6 b-r"> 37 <div class="form-group"> 38 <label class="col-sm-3 control-label">權限值(英文):</label> 39 <div class="col-sm-8"> 40 <select id="PERVALUE" name="PERVALUE" class="form-control input-s-sm"> 41 <option value="">請選擇</option> 42 @{ 43 var per = ViewData["PresetValue"] as List<Domain.SYS_CODE>; 44 if (per != null && per.Count > 0) 45 { 46 foreach (var item in per) 47 { 48 <option value="@item.CODEVALUE" @(Model.PERVALUE == item.CODEVALUE ? "selected" : "")>@item.NAMETEXT</option> 49 } 50 } 51 } 52 </select> 53 </div> 54 </div> 55 </div> 56 <div class="col-sm-6"> 57 <div class="form-group"> 58 <div class="col-sm-12"> 59 @Html.TextBox("NEXTPERVALUE", "", new { @class = "form-control input-s-sm" }) 60 </div> 61 </div> 62 </div> 63 </div> 64 <div class="hr-line-dashed"></div> 65 <div class="form-group"> 66 <label class="col-sm-3 control-label">權限圖標:</label> 67 <div class="col-sm-8"> 68 <span id="icon" class="form-control input-s-sm"> 69 @Html.Hidden("ICON", Model.ICON) 70 <a class="btn btn-primary btn-xs"><i class="@Model.ICON"></i></a> 71 <button class="changeicon" type="button"> 72 選擇 73 </button> 74 <button class="removeicon" type="button"> 75 移除 76 </button> 77 </span> 78 </div> 79 </div> 80 <div class="hr-line-dashed"></div> 81 <div class="form-group"> 82 <label class="col-sm-3 control-label">顯示順序:</label> 83 <div class="col-sm-8"> 84 @Html.TextBoxFor(p => p.SHOWORDER, new { @class = "form-control", @placeholder = "請輸入顯示順序", @datatype = "n1-5", @nullmsg = "請輸入顯示順序!", @errormsg = "顯示順序只能是數字!" }) 85 </div> 86 </div> 87 <div class="hr-line-dashed"></div> 88 <div class="text-center"> 89 <button class="btn btn-primary btn-save" type="submit"><i class="fa fa-check"></i> <span>肯定保存</span></button> 90 <button class="btn btn-warning" id="btn-dig-close" type="button"><i class="fa fa-reply-all"></i> 取消返回</button> 91 </div> 92 </div> 93 </div> 94 </div> 95 } 96 @section scripts{ 97 <script type="text/javascript"> 98 $(function () { 99 $('.form-horizontal').initValidform(); 100 if ($('#PERVALUE').val() == '' && $('#ID').val() != '') { 101 $('#NEXTPERVALUE').val('@Model.PERVALUE'); 102 } 103 }); 104 //移除圖標 105 $('.removeicon').click(function () { 106 $('#icon').find('a>i').attr('class', ''); 107 $('#ICON').val(''); 108 }); 109 //選擇圖標 110 $('.changeicon').click(function () { 111 var oldicon = $('#icon').find('a>i'); 112 top.dialog({ 113 title: '選擇圖標', 114 url: '/Sys/Module/ShowIcon', 115 width: 700, 116 height: 380, 117 data: oldicon.attr('class'), // 給 iframe 的數據 118 onclose: function () { 119 this.returnValue && oldicon.attr('class', this.returnValue) && $('#ICON').val(this.returnValue); 120 }, 121 oniframeload: function () { 122 } 123 }).showModal(); 124 return false; 125 }); 126 </script> 127 }
1 /// <summary> 2 /// 保存權限 3 /// </summary> 4 [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Add,Edit")] 5 public ActionResult Save(Domain.SYS_PERMISSION entity) 6 { 7 bool isEdit = false; 8 JsonHelper json = new JsonHelper() { Msg = "保存權限成功", Status = "n" }; 9 try 10 { 11 if (entity != null) 12 { 13 if (System.Text.Encoding.GetEncoding("gb2312").GetBytes(entity.NAME.Trim()).Length > 50) 14 { 15 json.Msg = "權限的名稱長度不能超過50個字符"; 16 return Json(json); 17 } 18 entity.ICON = Request.Form["ICON"]; 19 var nextpervalue = Request.Form["NEXTPERVALUE"]; 20 if (!string.IsNullOrEmpty(nextpervalue)) 21 { 22 if (!Regex.IsMatch(nextpervalue, @"^[A-Za-z0-9]{1,20}$")) 23 { 24 json.Msg = "權限值只能以英文數字組成,長度不能超過20個字符"; 25 return Json(json); 26 } 27 entity.PERVALUE = nextpervalue; 28 } 29 //添加 30 if (entity.ID <= 0) 31 { 32 entity.CREATEDATE = DateTime.Now; 33 entity.UPDATEDATE = DateTime.Now; 34 entity.UPDATEUSER = this.CurrentUser.Name; 35 entity.CREATEUSER = this.CurrentUser.Name; 36 } 37 else //編輯 38 { 39 entity.UPDATEUSER = this.CurrentUser.Name; 40 entity.UPDATEDATE = DateTime.Now; 41 isEdit = true; 42 } 43 //同一模塊下權限不能重複 44 if (!this.PermissionManage.IsExist(p => p.NAME.Equals(entity.NAME) && p.ID != entity.ID && p.MODULEID == entity.MODULEID)) 45 { 46 if (PermissionManage.SaveOrUpdate(entity, isEdit)) 47 { 48 json.Status = "y"; 49 } 50 else 51 { 52 json.Msg = "保存失敗"; 53 } 54 } 55 else 56 { 57 json.Msg = "權限" + entity.NAME + "同一模塊下已存在,不能重複添加"; 58 } 59 } 60 else 61 { 62 json.Msg = "未找到要保存的權限記錄"; 63 } 64 if (isEdit) 65 { 66 WriteLog(Common.Enums.enumOperator.Edit, "修改權限,結果:" + json.Msg, Common.Enums.enumLog4net.INFO); 67 } 68 else 69 { 70 WriteLog(Common.Enums.enumOperator.Add, "添加權限,結果:" + json.Msg, Common.Enums.enumLog4net.INFO); 71 } 72 } 73 catch (Exception e) 74 { 75 json.Msg = "保存權限發生內部錯誤!"; 76 WriteLog(Common.Enums.enumOperator.None, "對模塊權限按鈕的管理保存權限:", e); 77 } 78 return Json(json); 79 }
/// <summary> /// 刪除權限 /// </summary> [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Remove")] public ActionResult Delete(string idList) { var json = new JsonHelper() { Msg = "刪除權限成功", Status = "n" }; try { if (!string.IsNullOrEmpty(idList)) { var idList1 = idList.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList(); //判斷查找角色是否調用 if (!this.RolePermissionManage.IsExist(p => idList1.Any(e => e == p.PERMISSIONID))) { //判斷查找用戶是否調用 if (!this.UserPermissionManage.IsExist(p => idList1.Any(e => e == p.FK_PERMISSIONID))) { this.PermissionManage.Delete(p => idList1.Any(e => e == p.ID)); json.Status = "y"; } else { json.Msg = "有用戶正在使用該權限,不能刪除!"; } } else { json.Msg = "有角色正在使用該權限,不能刪除!"; } } else { json.Msg = "未找到要刪除的權限記錄"; } WriteLog(Common.Enums.enumOperator.Remove, "刪除權限,結果:" + json.Msg, Common.Enums.enumLog4net.WARN); } catch (Exception e) { json.Msg = e.InnerException.Message; WriteLog(Common.Enums.enumOperator.Remove, "對模塊權限按鈕的管理刪除權限:", e); } return Json(json); }
須要注意的是,咱們在刪除權限的時候,須要判斷 角色是否調用了權限、用戶是否調用了權限,因此 咱們要再聲明兩個接口
注意注入:
UserPermissionManage 這個有了,可是RolePermissionManage 咱們貌似沒作,上面講過了 添加CodeManage 接口實現類和配置,都是同樣的 我把接口和實現類貼給你們,你們嘗試一下 本身添加
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Service.IService 7 { 8 /// <summary> 9 /// Service層角色受權關係接口 10 /// add yuangang by 2015-05-22 11 /// </summary> 12 public interface IRolePermissionManage : IRepository<Domain.SYS_ROLE_PERMISSION> 13 { 14 /// <summary> 15 /// 保存角色權限 16 /// </summary> 17 /// <param name="roleId">角色ID</param> 18 /// <param name="newper">權限字符串</param> 19 /// <param name="sysId">系統ID</param> 20 /// <returns></returns> 21 bool SetRolePermission(int roleId, string newper, string sysId); 22 } 23 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Service.IService; 6 7 namespace Service.ServiceImp 8 { 9 /// <summary> 10 /// Service層角色受權關係接口 11 /// add yuangang by 2015-05-22 12 /// </summary> 13 public class RolePermissionManage : RepositoryBase<Domain.SYS_ROLE_PERMISSION>, IService.IRolePermissionManage 14 { 15 IPermissionManage PermissionManage { get; set; } 16 /// <summary> 17 /// 保存角色權限 18 /// </summary> 19 public bool SetRolePermission(int roleId, string newper, string sysId) 20 { 21 try 22 { 23 //一、獲取當前系統的模塊ID集合 24 var permissionId = this.PermissionManage.GetPermissionIdBySysId(sysId).Cast<int>().ToList(); 25 //二、獲取角色權限,是否存在,存在即刪除,只刪除當前選擇的系統 26 if (this.IsExist(p => p.ROLEID == roleId && permissionId.Any(e => e == p.PERMISSIONID))) 27 { 28 //三、刪除角色權限 29 this.Delete(p => p.ROLEID == roleId && permissionId.Any(e => e == p.PERMISSIONID)); 30 } 31 //四、添加角色權限 32 if (string.IsNullOrEmpty(newper)) return true; 33 //Trim 保證數據安全 34 var str = newper.Trim(',').Split(','); 35 foreach (var per in str.Select(t => new Domain.SYS_ROLE_PERMISSION() 36 { 37 PERMISSIONID = int.Parse(t), 38 ROLEID = roleId 39 })) 40 { 41 this.dbSet.Add(per); 42 } 43 //五、Save 44 return this.Context.SaveChanges() > 0; 45 } 46 catch (Exception e) { throw e.InnerException; } 47 } 48 } 49 }
OK,====,好像還有個初始化權限,在Index視圖頁 咱們有個ajax 初始化權限,咱們在PermissionController 控制器裏新建個初始化權限的方法 驗證權限是Reset
1 /// <summary> 2 /// 初始化權限,默認增刪改查詳情 3 /// <param name="id">模塊ID</param> 4 /// </summary> 5 [UserAuthorizeAttribute(ModuleAlias = "Permission", OperaAction = "Reset")] 6 public ActionResult Reset(string id) 7 { 8 var json = new JsonHelper() { Status = "n", Msg = "初始化完畢" }; 9 try 10 { 11 //判斷模塊ID 是否符合規範 12 if (string.IsNullOrEmpty(id) || !Regex.IsMatch(id, @"^\d+$")) 13 { 14 json.Msg = "模塊參數錯誤"; 15 WriteLog(Common.Enums.enumOperator.Allocation, "初始化權限,結果:" + json.Msg, Common.Enums.enumLog4net.ERROR); 16 return Json(json); 17 } 18 //將 ID 轉爲 Int 19 int newid = int.Parse(id); 20 21 //判斷權限裏 模塊是否有了權限 22 if (this.PermissionManage.IsExist(p => p.MODULEID == newid)) 23 { 24 json.Msg = "該模塊已存在權限,沒法初始化"; 25 WriteLog(Common.Enums.enumOperator.Allocation, "初始化權限,結果:" + json.Msg, Common.Enums.enumLog4net.ERROR); 26 return Json(json); 27 } 28 //添加默認權限 29 var per = new string[] { "查看,View", "列表,List", "詳情,Detail", "添加,Add", "修改,Edit", "刪除,Remove" }; 30 var list = new List<Domain.SYS_PERMISSION>(); 31 foreach (var item in per) 32 { 33 list.Add(new Domain.SYS_PERMISSION() 34 { 35 CREATEDATE = DateTime.Now, 36 CREATEUSER = this.CurrentUser.Name, 37 NAME = item.Split(',')[0], 38 PERVALUE = item.Split(',')[1], 39 UPDATEDATE = DateTime.Now, 40 UPDATEUSER = this.CurrentUser.Name, 41 MODULEID = newid, 42 SHOWORDER = 0 43 }); 44 } 45 //批量添加 46 if (this.PermissionManage.SaveList(list) > 0) 47 { 48 json.Status = "y"; 49 } 50 else 51 { 52 json.Msg = "初始化失敗"; 53 } 54 WriteLog(Common.Enums.enumOperator.Allocation, "初始化權限,結果:" + json.Msg, Common.Enums.enumLog4net.INFO); 55 } 56 catch (Exception e) 57 { 58 json.Msg = e.InnerException.Message; 59 WriteLog(Common.Enums.enumOperator.Allocation, "對模塊權限按鈕的管理初始化權限:", e); 60 } 61 return Json(json); 62 }
如今OK了,權限的管理咱們就完成了~~~
原創文章 轉載請尊重勞動成果 http://yuangang.cnblogs.com