protected override void OnInit(EventArgs e)
{
if (!IsPostBack)
{
if (LoginHelp.loginSuccessInfo == null)
{
Response.Redirect("Login.aspx");
}
ViewPowerCheck();
}
base.OnInit(e);
}
public void ViewPowerCheck()
{
//当前页面没有浏览权限,不能访问
if (!PowerCheck("浏览"))
{
Response.Write("你没有权限访问此页面!");
Response.End();
}
}
#endregion
#region PageModule
private string module=string.Empty;
/// <summary>
/// 当前页面所属模块
/// </summary>
public string Module
{
get { return module; }
set { module = value; }
}
#endregion
#region 检查操作权限
/// <summary>
/// 检测当前页面是否有操作权限powerName
/// </summary>
/// <param name="powerName">权限名称</param>
/// <returns></returns>
public bool PowerCheck(string powerName)
{
//当前页面没有分配模块,默认拥有所有操作权限
if (string.IsNullOrEmpty(Module))
{
return true;
}
using (var ctx = new HSQDataDataContext())
{
var lModule = from p in ctx.OT_Module
where p.Name == Module
select p.Id;
var lOperate = from p in ctx.OT_Operate
where p.Name == powerName
select p.Id;
if (lModule.Count() > 0 && lOperate.Count() > 0)
{
foreach (var l in LoginHelp.loginSuccessInfo.ListRoleID)
{
var lModuleOperate = from p in ctx.OT_RoleModule
where p.RoleId == l
select p.ModuleOperateId;
if (lModuleOperate.Count() == 0)
return false;
var lpower = from p in ctx.OT_ModuleOperate
where lModuleOperate.Contains(p.Id) && p.ModuleId == lModule.First() && p.OperateId == lOperate.First()
select p;
if (powerName.Count() > 0)
return true;
}
}
}
return false;
}
/// <summary>
/// 检查当前用户是否有指定模块的权限powerName
/// </summary>
/// <param name="moduleName"></param>
/// <param name="powerName"></param>
/// <returns></returns>
public bool PowerCheck(string moduleName, string powerName)
{
if (string.IsNullOrEmpty(Module))
{
return true;
}
using (var ctx = new HSQDataDataContext())
{
var lModule = from p in ctx.OT_Module
where p.Name == moduleName
select p.Id;
var lOperate = from p in ctx.OT_Operate
where p.Name == powerName
select p.Id;
if (lModule.Count() > 0 && lOperate.Count() > 0)
{
foreach (var l in LoginHelp.loginSuccessInfo.ListRoleID)
{
var lModuleOperate = from p in ctx.OT_RoleModule
where p.RoleId == l
select p.ModuleOperateId;
if (lModuleOperate.Count() == 0)
return false;
var lpower = from p in ctx.OT_ModuleOperate
where lModuleOperate.Contains(p.Id) && p.ModuleId == lModule.First() && p.OperateId == lOperate.First()
select p;
if (powerName.Count() > 0)
return true;
}
}
}
return false;
}