从这篇文章,希望您能够了解吉日嘎拉通用权限管理系统菜单项配置、缓存及在前台的展示技巧。
项目中使用了吉日嘎拉的通用权限管理系统,几十个子系统均由该权限管理系统管理。
在系统中配置好相关菜单及非菜单项,配置截图:
菜单权限设置截图
通过下拉菜单进入其中的一个子系统
子系统中的菜单项(菜单项表示该项会在前端需要展示出来,用于用户点击的项目),其中的公开表示所有人均可看到该菜单项目。
子系统中的非菜单项(非菜单项表示该项目不需要在前端展示出来,比如有些在页面中的弹出窗口、按钮等项目)
通过单点登录到子系统以后,通过一个服务获取登录用户拥有的全部菜单,在权限基类页实现,所有需要进行权限判断的页面均集成此基类页。
代码实现如下图: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
新闻热点
疑难解答