/// <summary> /// 导航过滤 /// </summary> /// <param name="list">导航集合</param> /// <param name="userId">用户Id</param> /// <param name="currentUrl">当前链接</param> /// <returns>IList<MenuEntity></returns> public IList<MenuEntity> AuthorityFilterByUser(IList<MenuEntity> list, string userId, string currentUrl) { var newList = new List<MenuEntity>(); if (list == null || string.IsNullOrWhiteSpace(userId)) { return newList; } //是否首页选中 var isIndexActive = true; foreach (var menu in list) { if (menu.Visible) { //主菜单 var newMenu = new MenuEntity() { Id = menu.Id, Icon = menu.Icon, Name = menu.Name, Url = menu.Url, Visible = menu.Visible, Authority = menu.Authority, SubMenuEntitys = new List<object>() }; foreach (var sub in menu.SubMenuEntitys) { var submenu = (MenuEntity)sub; submenu.IsCurrent = this.UrlMath(submenu.Url, currentUrl); if (submenu.IsCurrent) { newMenu.IsCurrent = true; isIndexActive = false; } var auts = submenu.Authority.FindAll(m => m.UserId == userId && m.IsAuthorize); if (auts.Count >= 1) { newMenu.SubMenuEntitys.Add(submenu); } } if (newMenu.SubMenuEntitys.Count >= 1 || this.UrlMath(newMenu.Url, "/Home/Index")) { newList.Add(newMenu); } } } if (isIndexActive) { if (newList.Count > 0) { newList[0].IsCurrent = true; } } return newList; }
/// <summary> /// 读取xml导航权限 /// </summary> /// <returns>IList<MenuEntity></returns> public IList <MenuEntity> ReadAuthorityAll() { IList <MenuEntity> menuEntityList = new List <MenuEntity>(); var path = AppDomain.CurrentDomain.BaseDirectory + "authority.xml"; XDocument xdoc = XDocument.Load(path); var elements = xdoc.Root.Elements(); foreach (var item in elements) { var menuEntity = new MenuEntity(); menuEntity = this.GetEntityByElement(item); //子导航 var subElements = item.Elements("menu"); foreach (var subItem in subElements) { var subMenuEntity = new MenuEntity(); subMenuEntity = this.GetEntityByElement(subItem); menuEntity.SubMenuEntitys.Add(subMenuEntity); } foreach (var aut in menuEntity.Authority) { foreach (var subItem in menuEntity.SubMenuEntitys) { var subMenu = (MenuEntity)subItem; var subAut = subMenu.Authority.FindAll(m => m.UserId == aut.UserId); var count = subAut.Count; if (count < 1) { subMenu.Authority.Add(aut); } } } menuEntityList.Add(menuEntity); } return(menuEntityList); }
/// <summary> /// XML节点转Entity /// </summary> /// <param name="element">节点</param> /// <returns>MenuEntity</returns> private MenuEntity GetEntityByElement(XElement element) { var menuEntity = new MenuEntity(); menuEntity.Authority = new List<Authority>(); menuEntity.SubMenuEntitys = new List<object>(); menuEntity.Id = element.Attribute("id").Value; menuEntity.Name = element.Attribute("name").Value; menuEntity.Url = element.Attribute("url").Value; menuEntity.Icon = element.Attribute("icon").Value; menuEntity.Visible = Convert.ToBoolean(element.Attribute("visible").Value); //授权信息 var authoritys = element.Elements("authority"); foreach (var autItem in authoritys) { var authority = new Authority(); authority.UserId = autItem.Attribute("userId").Value; authority.IsAuthorize = Convert.ToBoolean(autItem.Attribute("isAuthorize").Value); menuEntity.Authority.Add(authority); } return menuEntity; }
/// <summary> /// 读取xml导航权限 /// </summary> /// <returns>IList<MenuEntity></returns> public IList<MenuEntity> ReadAuthorityAll() { IList<MenuEntity> menuEntityList = new List<MenuEntity>(); var path = AppDomain.CurrentDomain.BaseDirectory + "authority.xml"; XDocument xdoc = XDocument.Load(path); var elements = xdoc.Root.Elements(); foreach (var item in elements) { var menuEntity = new MenuEntity(); menuEntity = this.GetEntityByElement(item); //子导航 var subElements = item.Elements("menu"); foreach (var subItem in subElements) { var subMenuEntity = new MenuEntity(); subMenuEntity = this.GetEntityByElement(subItem); menuEntity.SubMenuEntitys.Add(subMenuEntity); } foreach (var aut in menuEntity.Authority) { foreach (var subItem in menuEntity.SubMenuEntitys) { var subMenu = (MenuEntity)subItem; var subAut = subMenu.Authority.FindAll(m => m.UserId == aut.UserId); var count = subAut.Count; if (count < 1) { subMenu.Authority.Add(aut); } } } menuEntityList.Add(menuEntity); } return menuEntityList; }