天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

162 lines
4.5 KiB

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
{
/// <summary>
/// 权限工具
/// </summary>
public class PowerUtil
{
/// <summary>
/// 拥有的权限
/// </summary>
public List<PowerInfo> OwnPowers { get; set; }
/// <summary>
/// 拥有的角色
/// </summary>
public List<Role> OwnRoles { get; set; }
#region 获取权限列表
/// <summary>
/// 获取权限信息
/// </summary>
/// <returns>权限信息</returns>
public List<PowerInfo> GetPowerList(string powerFile)
{
List<PowerInfo> list = new List<PowerInfo>();
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 获取权限信息
/// <summary>
/// 获取权限信息
/// </summary>
/// <returns>权限信息</returns>
public List<PowerInfo> GetAllPowerInfos(string powerFile)
{
List<PowerInfo> list = new List<PowerInfo>();
List<PowerInfo> powers = new List<PowerInfo>();
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 创建子菜单项
/// <summary>
/// 创建子菜单项
/// </summary>
/// <param name="parentItem">父权限</param〉
/// <param name="list">子权限</param>
/// <summary>
/// 创建权限单项
/// </summary>
/// <param name="parentItem">父权限</param〉
/// <param name="list">子权限</param>
private void BuildChildItems(PowerInfo parentItem, List<PowerInfo> list)
{
var array = list.Where(i => i.SuperID == parentItem.PowerID);
parentItem.ChildPowers = new List<PowerInfo>();
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 判断是否拥有
/// <summary>
/// 判断是否拥有
/// </summary>
/// <param name="powerID">权限主键</param>
/// <returns>true:拥有;false:不拥有</returns>
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
}
}