using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using log4net.Ext; using Ant.Service.Common; using System.Collections; using Ant.Service.Common.Enums; using Central.Control.Domain; using MES.Production.Service.IService; using MES.Production.Service; namespace ChangFa.Machinery.WebPage.Controllers { /// /// 控制器基类,主要做登录用户、权限认证、日志记录等工作 /// add 作者: 季健国 QQ:181589805 by 2016-05-30 /// public class BaseController : Controller { #region 公用变量 /// /// 查询关键词 /// public string keywords { get; set; } /// /// 视图传递的分页页码 /// public int page { get; set; } /// /// 视图传递的分页条数 /// public int pagesize { get; set; } /// /// 用户容器,公用 /// public IUserManage UserManage = Spring.Context.Support.ContextRegistry.GetContext().GetObject("MES.Production.Service.User") as IUserManage; /// /// 系统ID,很重要 /// public string siteId = System.Configuration.ConfigurationManager.AppSettings["siteid"].ToString(); #endregion #region 用户对象 /// /// 获取当前用户对象 /// public Account CurrentUser { get { if (SessionHelper.GetSession("CurrentUser") != null) { return SessionHelper.GetSession("CurrentUser") as Account; } var account = UserManage.GetAccountByCookie(); SessionHelper.SetSession("CurrentUser", account); return account; } } #endregion /// /// 登录验证 /// protected override void OnActionExecuting(ActionExecutingContext filterContext) { #region 登录用户验证 //1、判断Session对象是否存在 if (filterContext.HttpContext.Session == null) { filterContext.HttpContext.Response.Write( " "); filterContext.RequestContext.HttpContext.Response.End(); filterContext.Result = new EmptyResult(); return; } //2、登录验证 if (this.CurrentUser == null) { filterContext.HttpContext.Response.Write( " "); filterContext.RequestContext.HttpContext.Response.End(); filterContext.Result = new EmptyResult(); return; } #endregion #region 公共Get变量 //分页页码 object p = filterContext.HttpContext.Request["page"]; if (p == null || p.ToString() == "") { page = 1; } else { page = int.Parse(p.ToString()); } //搜索关键词 string search = filterContext.HttpContext.Request.QueryString["Search"]; if (!string.IsNullOrEmpty(search)) { keywords = search; } //显示分页条数 string size = filterContext.HttpContext.Request.QueryString["example_length"]; if (!string.IsNullOrEmpty(size) && System.Text.RegularExpressions.Regex.IsMatch(size.ToString(), @"^\d+$")) { pagesize = int.Parse(size.ToString()); } else { pagesize = 10; } #endregion base.OnActionExecuting(filterContext); } #region log4net日志 /// /// 统一日志变量 /// protected static IExtLog _log = ExtLogManager.GetLogger("dblog"); /// /// 操作日志 /// public void WriteLog(enumOperator action, string message, enumLog4net logLevel) { switch (logLevel) { case enumLog4net.INFO: _log.Info(Utils.GetIP(), this.CurrentUser.Name, Request.Url.ToString(), action.ToString(), message); break; case enumLog4net.WARN: _log.Warn(Utils.GetIP(), this.CurrentUser.Name, Request.Url.ToString(), action.ToString(), message); break; default: _log.Error(Utils.GetIP(), this.CurrentUser.Name, Request.Url.ToString(), action.ToString(), message); break; } } /// /// 异常日志 /// public void WriteLog(enumOperator action, string message, Exception e) { _log.Fatal(Utils.GetIP(), this.CurrentUser.Name, Request.Url.ToString(), action.ToString(), message, e); } #endregion #region 输出消息 /// /// 输出JSON消息 /// /// 泛型对象 public void PrintMessage(object obj) { System.Web.HttpContext Context = System.Web.HttpContext.Current; Context.Response.Charset = "UTF-8"; //设置字符集类型 Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Context.Response.Write(JsonConverter.Serialize(obj)); System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); } /// /// 输出纯字符串消息 /// /// 字符串 public void PrintMessage(string str) { System.Web.HttpContext Context = System.Web.HttpContext.Current; Context.Response.Charset = "UTF-8"; //设置字符集类型 Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Context.Response.Write(str); System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); } #endregion } /// /// 模块权限验证功能 /// 规则:1、根据模块别名验证对应模块 /// 2、根据模块操作Action 验证是否可操作按钮 /// add 作者: 季健国 QQ:181589805 by 2016-05-30 /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] public class UserAuthorizeAttribute : AuthorizeAttribute { #region 字段和属性 /// /// 模块别名,可配置更改 /// public string ModuleAlias { get; set; } /// /// 权限动作 /// public string OperaAction { get; set; } /// /// 权限访问控制器参数 /// private string Sign { get; set; } /// /// 基类实例化 /// public BaseController baseController = new BaseController(); #endregion #region 权限认证 /// /// 权限认证 /// public override void OnAuthorization(AuthorizationContext filterContext) { //1、判断模块是否对应 if (string.IsNullOrEmpty(ModuleAlias)) { filterContext.HttpContext.Response.Write(" "); filterContext.RequestContext.HttpContext.Response.End(); filterContext.Result = new EmptyResult(); return; } //2、判断用户是否存在 if (baseController.CurrentUser == null) { filterContext.HttpContext.Response.Write(" "); filterContext.RequestContext.HttpContext.Response.End(); filterContext.Result = new EmptyResult(); return; } //对比变量,用于权限认证 var alias = ModuleAlias; #region 配置Sign调取控制器标识 Sign = filterContext.RequestContext.HttpContext.Request.QueryString["sign"]; if (!string.IsNullOrEmpty(Sign)) { if (("," + ModuleAlias.ToLower()).Contains("," + Sign.ToLower())) { alias = Sign; filterContext.Controller.ViewData["Sign"] = Sign; } } #endregion //3、调用下面的方法,验证是否有访问此页面的权限,查看加操作 var moduleId = baseController.CurrentUser.Modules.Where(p => p.ALIAS.ToLower() == alias.ToLower()).Select(p => p.ID).FirstOrDefault(); bool _blAllowed = this.IsAllowed(baseController.CurrentUser, moduleId, OperaAction); if (!_blAllowed) { filterContext.HttpContext.Response.Write(" "); filterContext.RequestContext.HttpContext.Response.End(); filterContext.Result = new EmptyResult(); return; } //4、有权限访问页面,将此页面的权限集合传给页面 filterContext.Controller.ViewData["PermissionList"] = GetPermissByJson(baseController.CurrentUser, moduleId); } /// /// 获取操作权限Json字符串,供视图JS判断使用 /// string GetPermissByJson(Account account, int moduleId) { //操作权限 var _varPerListThisModule = account.Permissions.Where(p => p.MODULEID == moduleId).Select(R => new { R.PERVALUE }).ToList(); return JsonConverter.Serialize(_varPerListThisModule); } /// /// 功能描述:判断用户是否有此模块的操作权限 /// bool IsAllowed(Account user, int moduleId, string action) { //判断入口 if (user == null || user.Id <= 0 || moduleId == 0 || string.IsNullOrEmpty(action)) return false; //验证权限 var permission = user.Permissions.Where(p => p.MODULEID == moduleId); action = action.Trim(','); if (action.IndexOf(',') > 0) { permission = permission.Where(p => action.ToLower().Contains(p.PERVALUE.ToLower())); } else { permission = permission.Where(p => p.PERVALUE.ToLower() == action.ToLower()); } return permission.Any(); } #endregion } /// /// 模块去重,非常重要 /// add 作者: 季健国 QQ:181589805 by 2016-08-03 /// public class ModuleDistinct : IEqualityComparer { public bool Equals(SYS_MODULE x, SYS_MODULE y) { return x.ID == y.ID; } public int GetHashCode(SYS_MODULE obj) { return obj.ToString().GetHashCode(); } } }