using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.Web; using System.Xml; using System.Collections; using QMAPP.Entity.Sys; namespace QMAPP.Common.Web.Util { /// /// 权限工具 /// public class PowerUtil { /// /// 拥有的权限 /// public List OwnPowers { get; set; } /// /// 拥有的角色 /// public List OwnRoles { get; set; } #region 获取权限列表 /// /// 获取权限信息 /// /// 权限信息 public List GetPowerList(string powerFile) { List list = new List(); try { //获取全部权限 XElement xel = XElement.Load(powerFile); var datas = from x in xel.Descendants("PowerInfo") select x; foreach (XElement d in datas) { if (d.Attribute("Visible").Value.ToLower() == "false") { continue; } PowerInfo info = new PowerInfo(); info.PowerID = d.Attribute("PowerID").Value; info.PowerDes = d.Attribute("PowerDes").Value; info.PowerType = d.Attribute("PowerType").Value; info.Seq = int.Parse(d.Attribute("Seq").Value); info.SuperID = d.Attribute("SuperID").Value; info.Selected = this.IsOwn(info.PowerID); list.Add(info); } return list; } catch (Exception ex) { throw ex; } } #endregion #region 获取权限信息 /// /// 获取权限信息 /// /// 权限信息 public List GetAllPowerInfos(string powerFile) { List list = new List(); List powers = new List(); try { //获取全部权限 list = this.GetPowerList(powerFile); //添加第一级权限 var array = list.Where(i => i.SuperID == ""); //添加第一级权限 foreach (PowerInfo childPower in array) { //添加这个子权限 this.BuildChildItems(childPower, list); powers.Add(childPower); } return powers; } catch (Exception ex) { throw ex; } } #endregion #region 创建子菜单项 /// /// 创建子菜单项 /// /// 父权限子权限 /// /// 创建权限单项 /// /// 父权限子权限 private void BuildChildItems(PowerInfo parentItem, List list) { var array = list.Where(i => i.SuperID == parentItem.PowerID); parentItem.ChildPowers = new List(); foreach (PowerInfo childPower in array) { //如果查询不到下级菜单就结束 if (list.Where(i => i.SuperID == childPower.PowerID).Count() != 0) { //添加子菜单 this.BuildChildItems(childPower, list); } parentItem.ChildPowers.Add(childPower); } } #endregion #region 判断是否拥有 /// /// 判断是否拥有 /// /// 权限主键 /// true:拥有;false:不拥有 private bool IsOwn(string powerID) { bool flag = false; if (this.OwnPowers == null) return flag; flag = this.OwnPowers.Exists(p => p.PowerID == powerID); return flag; } #endregion } }