public int Add(int manager_id, string actionType, string controllerName, string remark, string user_ip) { DAO.BLL.B_Manager b_manager = new DAO.BLL.B_Manager(); DAO.BLL.B_Navigation b_nav = new DAO.BLL.B_Navigation(); Domain.Manager_log model = new Domain.Manager_log(); model.user_id = manager_id; var m_manager = b_manager.Get(manager_id); model.user_name = m_manager.user_name; model.action_type = actionType; List <SearchTemplate> st = new List <SearchTemplate>() { new SearchTemplate() { key = "controllerName", value = controllerName, searchType = Common.EnumBase.SearchType.Eq } }; model.navigation = b_nav.GetList(st, null)[0]; model.remark = remark; model.user_ip = user_ip; model.add_time = DateTime.Now; var res = Save(model); return(res); }
public override void OnResultExecuting(ResultExecutingContext filterContext) { //如果记录结束时间,就可能疏漏掉一些被重置的请求 try { var MonLog = filterContext.Controller.ViewData[Key] as MonitorLog; MonLog.ExecuteEndTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff", DateTimeFormatInfo.InvariantInfo)); Logger.Info(MonLog.GetLoginfo()); } catch { } var actionName = filterContext.RouteData.Values["action"].ToString().ToLower(); var controllerName = filterContext.RouteData.Values["controller"].ToString().ToLower(); //如果相同,则是用户第一次打开view的请求,此时并没有数据,页面加载完后,会主动通过GetList请求数据,我们只用把第二次记录下来即可 if (actionName != "index" && !string.IsNullOrEmpty(controllerName)) { var thisIp = Utils.getIp(); try { DAO.BLL.B_Manager_log b_log = new DAO.BLL.B_Manager_log(); DAO.BLL.B_Manager b_manager = new DAO.BLL.B_Manager(); DAO.BLL.B_Navigation b_nav = new DAO.BLL.B_Navigation(); Domain.Manager_log model = new Domain.Manager_log(); model.user_id = Convert.ToInt32(filterContext.HttpContext.User.Identity.Name); var m_manager = b_manager.Get(Convert.ToInt32(filterContext.HttpContext.User.Identity.Name)); model.user_name = m_manager.user_name; model.action_type = ActionType(actionName); List <SearchTemplate> st = new List <SearchTemplate>() { new SearchTemplate() { key = "controllerName", value = controllerName, searchType = Common.EnumBase.SearchType.Eq } }; model.navigation = b_nav.GetList(st, null)[0]; MonitorLog MonLog = new MonitorLog(); model.remark = MonLog.GetCollections(filterContext.HttpContext.Request.Form); model.user_ip = thisIp; model.add_time = DateTime.Now; b_log.Save(model); } catch { } } }
// OnActionExecuted 在执行操作方法后由 ASP.NET MVC 框架调用。 // OnActionExecuting 在执行操作方法之前由 ASP.NET MVC 框架调用。 // OnResultExecuted 在执行操作结果后由 ASP.NET MVC 框架调用。 // OnResultExecuting 在执行操作结果之前由 ASP.NET MVC 框架调用。 /// <summary> /// 在执行操作方法之前由 ASP.NET MVC 框架调用。 /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { #region 记录日志(所有的请求) MonitorLog MonLog = new MonitorLog(); MonLog.ExecuteStartTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff", DateTimeFormatInfo.InvariantInfo)); MonLog.ControllerName = filterContext.RouteData.Values["controller"] as string; MonLog.ActionName = filterContext.RouteData.Values["action"] as string; MonLog.FormCollections = filterContext.HttpContext.Request.Form; //form表单提交的数据 MonLog.QueryCollections = filterContext.HttpContext.Request.QueryString; //Url 参数 //Logger.Info(MonLog.GetLoginfo()); filterContext.Controller.ViewData[Key] = MonLog; #endregion //忽略home的权限验证 var controllerName = filterContext.RouteData.Values["controller"].ToString().ToLower(); if (controllerName == "home") { return; } var actionName = filterContext.RouteData.Values["action"].ToString().ToLower(); //忽略首页index的权限验证 //if (actionName == "index") return; var actionType = ActionType(actionName); DAO.BLL.B_Navigation b_nav = new DAO.BLL.B_Navigation(); List <SearchTemplate> st = new List <SearchTemplate>() { new SearchTemplate() { key = "controllerName", value = controllerName, searchType = Common.EnumBase.SearchType.Eq } }; IList <Domain.Navigation> list_nav = b_nav.GetList(st, null); if (list_nav.Count == 0) { filterContext.Result = new ContentResult { Content = @"抱歉,没有找到该操作!" }; return; } DAO.BLL.B_Manager_role_value b_mrv = new DAO.BLL.B_Manager_role_value(); DAO.BLL.B_Manager b_manager = new DAO.BLL.B_Manager(); var m_manager = b_manager.Get(Convert.ToInt32(filterContext.HttpContext.User.Identity.Name)); st = new List <SearchTemplate>() { new SearchTemplate() { key = "role_id", value = m_manager.manager_role.id, searchType = Common.EnumBase.SearchType.Eq }, new SearchTemplate() { key = "nav_id", value = list_nav[0].id, searchType = Common.EnumBase.SearchType.Eq } }; var list_mrv = b_mrv.GetList(st, null); if (list_mrv.Count == 0) { filterContext.Result = new ContentResult { Content = @"抱歉,你不具有当前操作的权限!" }; return; } if (!list_mrv[0].action_type.Contains(actionType)) { //如果是查看,就返回一个空的视图,否则返回一个json if (actionType == EnumBase.Authorize.查看.Description()) { filterContext.Result = new ContentResult { Content = @"抱歉,你不具有当前操作的权限!" }; // 直接返回 return Content("抱歉,你不具有当前操作的权限!") } else { Common.Json json = new Common.Json(); json.msg = "抱歉,你不具有当前操作的权限!"; json.status = -1; filterContext.Result = new JsonResult() { Data = json }; } } }
/// <summary> /// 初始化权限 /// </summary> public void Initializing() { DAO.BLL.B_Navigation b_nav = new DAO.BLL.B_Navigation(); DAO.BLL.B_Manager_role b_mr = new DAO.BLL.B_Manager_role(); DAO.BLL.B_Manager_role_value b_mrv = new DAO.BLL.B_Manager_role_value(); DAO.BLL.B_Manager b_manager = new DAO.BLL.B_Manager(); DAO.BLL.B_Manager_log b_log = new DAO.BLL.B_Manager_log(); var list_log = b_log.LoadAll(); foreach (var item in list_log) { b_log.Delete(item.id); } //1.删除所有管理员 var list_manager = b_manager.LoadAll(); foreach (var item in list_manager) { b_manager.Delete(item.id); } //2.删除所有角色权限 var list_mrv = b_mrv.LoadAll(); foreach (var item in list_mrv) { b_mrv.Delete(item.id); } //3.删除所有的角色 var list_mr = b_mr.LoadAll(); foreach (var item in list_mr) { b_mr.Delete(item.id); } //4.删除所有的权限 var list_nva = b_nav.LoadAll(); foreach (var item in list_nva) { b_nav.Delete(item.id); } //5.添加权限 #region 权限 Domain.Navigation model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-home\"></i>"; model.title = "主页"; model.link_url = "#"; model.sort_id = 1; model.is_lock = "√"; model.parent_id = 0; model.action_type = "查看"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } var res = b_nav.Save(model); model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-user\"></i>"; model.title = "会员列表"; model.link_url = "user"; model.sort_id = 1; model.is_lock = "√"; model.parent_id = res; model.action_type = "查看,添加,修改,删除,审核"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } b_nav.Save(model); //常用示例 model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-tags\"></i>"; model.title = "常用示例"; model.link_url = "#"; model.sort_id = 1; model.is_lock = "√"; model.parent_id = 0; model.action_type = "查看"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } res = b_nav.Save(model); model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-envelope\"></i>"; model.title = "短信工具"; model.link_url = "send_sms"; model.sort_id = 1; model.is_lock = "√"; model.parent_id = res; model.action_type = "查看,添加,删除"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } b_nav.Save(model); //系统安全 model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-desktop\"></i>"; model.title = "系统安全"; model.link_url = "#"; model.sort_id = 1; model.is_lock = "√"; model.parent_id = 0; model.action_type = "查看"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } res = b_nav.Save(model); model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-star-o\"></i>"; model.title = "数据备份"; model.link_url = "backups"; model.sort_id = 1; model.is_lock = "√"; model.parent_id = res; model.action_type = "查看,添加,删除,下载"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } b_nav.Save(model); model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-book\"></i>"; model.title = "操作日志"; model.link_url = "manager_log"; model.sort_id = 4; model.is_lock = "√"; model.parent_id = res; model.action_type = "查看,删除"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } b_nav.Save(model); //----------------------------------------- model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-cogs\"></i>"; model.title = "系统管理"; model.link_url = "#"; model.sort_id = 99; model.is_lock = "√"; model.parent_id = 0; model.action_type = "查看"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } res = b_nav.Save(model); model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-cog\"></i>"; model.title = "权限列表"; model.link_url = "navigation"; model.sort_id = 1; model.is_lock = "√"; model.parent_id = res; model.action_type = "查看,添加,修改,删除"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } b_nav.Save(model); model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-group\"></i>"; model.title = "角色列表"; model.link_url = "manager_role"; model.sort_id = 2; model.is_lock = "√"; model.parent_id = res; model.action_type = "查看,添加,修改,删除,审核"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } b_nav.Save(model); model = new Domain.Navigation(); model.icon_url = "<i class=\"fa fa-user-secret\"></i>"; model.title = "用户列表"; model.link_url = "manager"; model.sort_id = 3; model.is_lock = "√"; model.parent_id = res; model.action_type = "查看,添加,修改,删除,审核"; if (model.parent_id == 0) { model.channel_id = 1; } else { model.channel_id = 2; } b_nav.Save(model); #endregion //6.添加角色和角色权限 Domain.Manager_role m_mr = new Domain.Manager_role(); m_mr.role_name = "管理员"; m_mr.is_sys = 1; res = b_mr.Save(m_mr); list_nva = b_nav.LoadAll(); foreach (var item in list_nva) { Domain.Manager_role_value m_mrv = new Domain.Manager_role_value(); m_mrv.role_id = res; m_mrv.nav_id = item.id; m_mrv.action_type = item.action_type; b_mrv.Update(m_mrv); } //添加管理员 Domain.Manager m_manager = new Domain.Manager(); m_manager.user_name = "admin"; m_manager.real_name = "提伯斯"; m_manager.mobile = "15019400599"; m_manager.email = "*****@*****.**"; m_manager.password = Common.Encrypt.md5("123456"); m_manager.is_lock = "√"; m_manager.add_time = DateTime.Now; m_manager.manager_role = m_mr; b_manager.Save(m_manager); }