[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using OIFrameWork.Common;
using OIFrameWork.Entity;
using OIFrameWork.Web.Properties;
using UserPermission = OIFrameWork.BLL.UserPermission;
namespace OIFrameWork.Web.PageBase
{
public class OiPageBase : System.Web.UI.Page
{
#region "页面需要调用的一些变量信息"
protected Int32 ControlCodes = 0;
/// <summary>
/// 标识页面的唯一标识编码
/// </summary>
protected String PageCode = String.Empty;
/// <summary>
/// 标识当前页面所属的Application组的标识编码
/// </summary>
protected String ApplicationCode = String.Empty;
/// <summary>
/// 标识当前页面所属模块的ModuleCode编码
/// </summary>
protected String ModuleCode = String.Empty;
/// <summary>
/// 当前用户的登录票据
/// </summary>
protected FormsAuthenticationTicket AuthTicket;
/// <summary>
/// 系统的系统名称
/// </summary>
static readonly String SystemName = SystemConfigValue.GetInstance().SystemFrameWorkName;
#endregion
#region "页面的重写方法"
/// <summary>
/// 页面初始化执行的方法,主要是判断用户是否经过了验证,如果没有经过验证强制踢出
/// </summary>
/// <param name="e"></param>
protected override void OnPreInit(EventArgs e)
{
//验证用户是否经过的服务器验证
//--用户操作页面,每个页面都要去验证下登录票据是否已经过期,是否经过验证,保证用户有权限来操作这个页面
//--通过Request.IsAuthenticated方法可以获取是否进行了验证请求
if (!Request.IsAuthenticated)
{
//没有验证的话,执行跳转,跳转前,清空登录票据
RedirectUrl();
}
try
{
//判断是否已经对类变量进行了赋值,如果没有赋值就进行赋值操作
if (AuthTicket == null)
{
//获取存储的Cookies
var httpCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (httpCookie != null)
{
//对类变量AuthTicket进行赋值操作
AuthTicket = FormsAuthentication.Decrypt(httpCookie.Value);
//设置页面的Title
Title = SystemName;
}
else
{
//如果httpCookie为空,说明没有登录票据,退出系统
RedirectUrl();
return;
}
}
//对AuthTicket赋值后,如果还为空,那么就退出系统
if (AuthTicket == null)
{
RedirectUrl();
return;
}
//验证用户的权限
//1-首先验证登录用户的页面权限
var userPermission = DataCache.GetInstance().GetCache("Permission_" + AuthTicket.Name);
var lst = (List<UserPermission>)userPermission;
var flage = AuthTicket.UserData == "superadmin" || CheckUserPagePermission(lst);
//对访问的页面没有在用户权限页面中,退出用户
if (!flage)
{
RedirectUrl();
return;
}
//开始验证权限
}
catch
{
RedirectUrl();
}
base.OnInit(e);
}
/// <summary>
/// 验证用户的页面权限
/// </summary>
/// <param name="lstList">用户权限集合</param>
/// <returns>返回是否具有页面权限</returns>
protected bool CheckUserPagePermission(List<UserPermission> lstList)
{
foreach (var permission in lstList.Where(permission => permission.PermissionEntity.PageCode == PageCode))
{
ControlCodes = Convert.ToInt32(permission.PermissionEntity.ControlCode);
return true;
}
return false;
}
/// <summary>
/// 初始化GridView的分页属性
/// </summary>
/// <param name="aspxGridView"></param>
protected void InitGridViewPagerSettings(ASPxGridView aspxGridView)
{
aspxGridView.SettingsPager.PageSizeItemSettings.Visible = true;
aspxGridView.SettingsPager.NextPageButton.Text = Resources.OiPageBase_InitGridViewPagerSettings_NewsPageButton;
aspxGridView.SettingsPager.PrevPageButton.Text = Resources.OiPageBase_InitGridViewPagerSettings_PrevpageButton;
aspxGridView.SettingsPager.LastPageButton.Text = Resources.OiPageBase_InitGridViewPagerSettings_LastPageButton;
aspxGridView.SettingsPager.FirstPageButton.Text = Resources.OiPageBase_InitGridViewPagerSettings_FirstPageButton;
aspxGridView.SettingsPager.Summary.Text = Resources.OiPageBase_InitGridViewPagerSettings_SummaryText;
aspxGridView.SettingsPager.PageSizeItemSettings.Caption = Resources.OiPageBase_InitGridViewPagerSettings_Caption;
aspxGridView.PageSizeChanged += aspxGridView_PageSizeChanged;
}
/// <summary>
/// 当前页显示条数变化时候的方法,此方法需要到引用类中实现
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void aspxGridView_PageSizeChanged(object sender, EventArgs e)
{
//需要到派生类中重写此方法
}
/// <summary>
/// 验证用户的页面操作权限
/// </summary>
/// <param name="aspxGridView">ASPxGridView</param>
protected void CheckUserOpratePermission(ASPxGridView aspxGridView)
{
if (!BLL.CheckPower.GetInstance().CheckUserPermission(ControlCodes, 1))
{
//查看的用户权限,没有就隐藏AspxGridView
aspxGridView.Visible = false;
}
else
{
//新增的用户权限,没有就要屏蔽添加按钮的功能
for (var i = 0; i < aspxGridView.Columns.Count; i++)
{
if (aspxGridView.Columns[i].GetType().Name != "GridViewCommandColumn") continue;
var column = ((GridViewCommandColumn) aspxGridView.Columns[i]);
if (BLL.CheckPower.GetInstance().CheckUserPermission(ControlCodes, 2))
{
column.ShowNewButton = true;
}
if (BLL.CheckPower.GetInstance().CheckUserPermission(ControlCodes, 3))
{
column.ShowEditButton = true;
}
if (BLL.CheckPower.GetInstance().CheckUserPermission(ControlCodes, 4))
{
column.ShowDeleteButton = true;
}
if (column.ShowNewButton == false && column.ShowEditButton == false &&
column.ShowDeleteButton == false)
{
column.Visible = false;
}
}
if (BLL.CheckPower.GetInstance().CheckUserPermission(ControlCodes, 5))
{
aspxGridView.SettingsBehavior.AllowSort = true;
}
}
}
/// <summary>
/// 注册脚本跳转页面
/// </summary>
protected void RedirectUrl()
{
CookieFunction.GetInstance().QuitCookie();
Response.Redirect("/Default.html");
//ClientScript.RegisterStartupScript(GetType(), "js", "<script>parent.location.href='/Default.html'</script>");
}
/// <summary>
/// 页面初始化完成的事件
/// </summary>
/// <param name="e"></param>
protected override void OnInitComplete(EventArgs e)
{
//---从缓存中抽取当前页面的一些公共信息
//---这些信息主要是为了记录页面的操作日志,目的是得到页面所在的应用组和模块编码
var entityPage = BLL.SysPage.GetInstance().GetEntityByCache(PageCode);
//---如果获取的页面实体为空,则说明数据库中不存在这样的页面,系统需进行跳转到登录页面
if (entityPage == null)
{
RedirectUrl();
return;
}
//---设置页面所属的应用模组编码
ApplicationCode = entityPage.AppCode;
//---设置页面所属模块的编码
ModuleCode = entityPage.ModuleCode;
//---如果页面是回传的则不进行处理,如果不是回传的,则根据情况记录页面的访问日志
if (!Page.IsPostBack)
{
switch (BLL.SysBusiness.ReturnLogLevel())
{
//---如果页面日志定义为三级日志,则记录页面的访问记录
case SystemLogLevel.LogLevelThreeNumber:
var logEntity = GetLogEntity("页面访问日志,访问页面:" + entityPage.PageName, SystemLogType.ViewText);
BLL.SysLogInfo.GetInstance().Insert(logEntity);
break;
}
}
base.OnInitComplete(e);
}
/// <summary>
/// 记录系统错误的日志,当页面发生错误时,由OnError捕捉
/// </summary>
/// <param name="e"></param>
protected override void OnError(EventArgs e)
{
var currentError = Server.GetLastError();
var htm = new StringBuilder();
htm.AppendLine("");
htm.AppendLine("错误发生的Url地址:" + Request.Url);
htm.AppendLine("错误的详细信息:" + currentError.Message);
htm.AppendLine("Stack Trace:" + currentError);
htm.AppendLine(currentError.HelpLink);
bool flage;
SystemLog.GetInstance().WriteLogToFile(htm.ToString(), "系统错误日志", StringFunction.GetInstance().GetIpAddress(), Request.Url.ToString(), out flage);
Server.ClearError();
Response.Write("<h1>您的访问不符合系统访问要求,请重试或联系网站管理员咨询!</h1>");
Response.End();
base.OnError(e);
}
#endregion
#region "基类方法"
/// <summary>
/// 获取当前母板页面的lbCurrentPosition控件
/// </summary>
/// <returns></returns>
protected Label GetNavigateLabel()
{
if (Master != null)
return (Label)Master.FindControl("lbCurrentPosition");
return null;
}
/// <summary>
/// 获取母板页中的pButtonContainter控件
/// </summary>
/// <returns></returns>
protected Panel GetNavigatePanel()
{
if (Master != null)
return (Panel)Master.FindControl("pButtonContainter");
return null;
}
/// <summary>
/// 生成日志类实体,填充必要的日志信息
/// </summary>
/// <param name="logContent">日志内容</param>
/// <param name="logType">日志类型</param>
/// <returns>SysLogInfo实体</returns>
protected SysLogInfo GetLogEntity(String logContent, String logType)
{
var entity = new SysLogInfo
{
CreateTime = DateTime.Now,
FromIpAddress = StringFunction.GetInstance().GetIpAddress(),
FromUrl = StringFunction.GetInstance().GetUrlAddress(),
LogCode = StringFunction.GetInstance().GetGuidString(),
UserCode = AuthTicket.Name,
PageCode = PageCode,
ApplicationCode = ApplicationCode,
ModuleCode = ModuleCode,
LogContent = logContent,
LogType = logType,
};
return entity;
}
#endregion
}
}
|