從這篇文章,希望您能夠了解吉日嘎拉通用權限管理系統菜單項配置、緩存及在前臺的展示技巧。
項目中使用了吉日嘎拉的通用權限管理系統,幾十個子系統均由該權限管理系統管理。
在系統中配置好相關菜單及非菜單項,配置截圖:
菜單權限設置截圖
通過下拉菜單進入其中的一個子系統
子系統中的菜單項(菜單項表示該項會在前端需要展示出來,用于用戶點擊的項目),其中的公開表示所有人均可看到該菜單項目。
子系統中的非菜單項(非菜單項表示該項目不需要在前端展示出來,比如有些在頁面中的彈出窗口、按鈕等項目)
通過單點登錄到子系統以后,通過一個服務獲取登錄用戶擁有的全部菜單,在權限基類頁實現,所有需要進行權限判斷的頁面均集成此基類頁。
代碼實現如下圖:AuthBasePage.cs基類頁,可參考編寫基類頁
public class AuthBasePage : BasePage { /// <summary> /// 所有的權限菜單:包含菜單項、非菜單項(如程序中的彈出頁、按鈕等) /// </summary> PRotected string menuHtml = string.Empty; /// <summary> /// 獲取所有菜單的方法 用緩存 /// 通過userInfo.OpenId來更新緩存 每次進入會重新獲取一次菜單, /// </summary> /// <param name="userInfo"></param> /// <returns></returns> private string GetmenuHtml(BaseUserInfo userInfo, bool refreshFlag = false) { string cacheKey = "menuHtml_" + userInfo.OpenId; if (refreshFlag) { HttpContext.Current.Cache.Remove(cacheKey); } if (HttpContext.Current.Cache[cacheKey] == null) { lock (this) { if (HttpContext.Current.Cache[cacheKey] == null) { PermissionServiceSoapClient service = new PermissionServiceSoapClient(); string systemCode = ZTOTransferFees.Foundation.ConfigHelper.GetConfigString("SystemCode"); menuHtml = service.GetPermissionListByUser(systemCode, userInfo.Id); menuHtml = menuHtml.Replace("Id", "id").Replace("FullName", "name").Replace("NavigateUrl", "tabUrl").Replace("Parentid", "parentId").Replace( "ImagUrl", "icon").Replace("/"Expand/":1", "open:true"); HttpContext.Current.Cache.Add(cacheKey, menuHtml, null, DateTime.Now.AddMinutes(120), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); } } } return HttpContext.Current.Cache[cacheKey] as string; } protected override void OnInit(EventArgs e) { //判斷是否得到身份認證 未認證或超時時彈出登錄窗口而非跳轉到登錄頁 if (null == HttpContext.Current.User.Identity || !HttpContext.Current.User.Identity.IsAuthenticated) { Response.Write("<script type=/"text/javascript/">"); Response.Write("var topWin = (function (p, c) {while (p != c) {c = p;p = p.parent}return c;})(window.parent, window);"); Response.Write("try{ topWin.openLoginWindow();}catch(e){window.location='/Login.aspx'}"); Response.Write("</script>"); Response.End(); } HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName]; FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); userData = authTicket.UserData; JavascriptSerializer javaScriptSerializer = new JavaScriptSerializer(); userInfo = javaScriptSerializer.Deserialize<BaseUserInfo>(userData); userInfo.ServiceUserName = BaseSystemInfo.ServiceUserName; userInfo.ServicePassWord = BaseSystemInfo.ServicePassword; //獲得全部菜單項、非菜單項的字符串(json)字符串 menuHtml = GetmenuHtml(userInfo); //正在訪問的頁面,通過判斷該頁面是否在menuHtml中來進行權限判斷,按鈕等也一樣 string curUrl = HttpContext.Current.Request.FilePath; if (!menuHtml.Contains(curUrl)) {//權限管理員的聯系方式 string authManagerInfo = ZTOTransferFees.Foundation.ConfigHelper.GetConfigString("authManagerInfo"); HttpContext.Current.Items["ErrorMessage"] = "對不起,您沒有權限訪問頁面:" + curUrl + "<br/>如有疑問,請與權限分配人聯系<br/>" + authManagerInfo; HttpContext.Current.Server.Transfer("~/Prompt/ShowNoRigh.aspx"); } base.OnInit(e); }
在上面我們可以看到,通過服務已經獲取了該用戶所擁有的所有菜單及非菜單項目 menuHtml,menuHtml代碼如下,是一個Json數組, 菜單項太多,折疊起來了。
1 [ 2 { 3 "id": 1000005, 4 "parentId": null, 5 "Code": "991810201", 6 "name": "報價維護", 7 "CategoryCode": null, 8 "ImageIndex": null, 9 "SelectedImageIndex": null, 10 "tabUrl": null, 11 "icon": "/system/libs/js/tree/ztree/img/diy/1_close.png", 12 "Target": "fraContent", 13 "FormName": null, 14 "AssemblyName": null, 15 "PermissionScopeTables": null, 16 "SortCode": 1000005, 17 "Enabled": 1, 18 "DeletionStateCode": 0, 19 "IsMenu": 1, 20 "IsPublic": 1, 21 "IsVisible": 1, 22 "IsScope": 0, 23 "LastCall": null, 24 "Expand": 0, 25 "AllowEdit": 1, 26 "AllowDelete": 1, 27 "Description": null, 28 "CreateOn": "/Date(1400819927000)/", 29 "CreateUserid": "102383", 30 "CreateBy": "宋彪", 31 "ModifiedOn": "/Date(1400830672000)/", 32 "ModifiedUserid": "102383", 33 "ModifiedBy": "宋彪" 34 }, 35 { 36 "id": 1000006, 37 "parentId": 1000005, 38 "Code": "99181020101", 39 "name": "報價查詢", 40 "CategoryCode": null, 41 "ImageIndex": null, 42 "SelectedImageIndex": null, 43 "tabUrl": "/BaoJiaChaXun.aspx", 44 "icon": "/system/skin/titlebar_arrow.gif", 45 "Target": "fraContent", 46 "FormName": null, 47 "AssemblyName": null, 48 "PermissionScopeTables": null, 49 "SortCode": 1000006, 50 "Enabled": 1, 51 "DeletionStateCode": 0, 52 "IsMenu": 1, 53 "IsPublic": 1, 54 "IsVisible": 1, 55 "IsScope": 0, 56 "LastCall": null, 57 "Expand": 0, 58 "AllowEdit": 1, 59 "AllowDelete": 1, 60 "Description": null, 61 "CreateOn": "/Date(1400819973000)/", 62 "CreateUserid": "102383", 63 "CreateBy": "宋彪", 64 "ModifiedOn": "/Date(1400828358000)/", 65 "ModifiedUserid": "102383", 66 "ModifiedBy": "宋彪" 67 }, 68 { 69 "id": 1000008, 70 "parentId": null, 71 "Code": "991810202", 72 "name": "報價審核", 73 "CategoryCode": null, 74 "ImageIndex": null, 75 "SelectedImageIndex": null, 76 "tabUrl": null, 77 "icon": "/system/libs/js/tree/ztree/img/diy/2.png", 78 "Target": "fraContent", 79 "FormName": null, 80 "AssemblyName": null, 81 "PermissionScopeTables": null, 82 "SortCode": 1000008, 83 "Enabled": 1, 84 "DeletionStateCode": 0, 85 "IsMenu": 1, 86 "IsPublic": 1, 87 "IsVisible": 1, 88 "IsScope": 0, 89 "LastCall": null, 90 "Expand": 0, 91 "AllowEdit": 1, 92 "AllowDelete": 1, 93 "Description": null, 94 "CreateOn": "/Date(1400820277000)/", 95 "CreateUserid": "102383", 96 "CreateBy": "宋彪", 97 "ModifiedOn": "/Date(1400828373000)/", 98 "ModifiedUserid": "102383", 99 "M
新聞熱點
疑難解答