public UserInfoController(IUserinfoService _userinfoService, IRoleInfoService _roleInfoService, IActionInfoService _actionInfoService, IR_UserInfo_ActionInfoService _r_UserInfo_ActionInfoService) { userinfoService = _userinfoService; roleInfoService = _roleInfoService; actionInfoservice = _actionInfoService; r_UserInfo_ActionInfoService = _r_UserInfo_ActionInfoService; }
public UserInfoController(IUserInfoService serviceParam, IRoleInfoService roleServiceParam, IR_UserInfo_ActionInfoService userActionServiceParam, IActionInfoService actionServiceParam) { userInfoService = serviceParam; roleInfoService = roleServiceParam; rUserActionService = userActionServiceParam; actionService = actionServiceParam; }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); IApplicationContext ctx = ContextRegistry.GetContext(); if (IsCheck) { //从Redis缓存中读取数据 if (Request.Cookies["loginuserId"] == null) { filterContext.HttpContext.Response.Redirect("/adminlogin/Login.html"); return; } string userGuid = Request.Cookies["loginuserId"].Value.ToString(); object id = CacheHelper.CacheHelper.GetString(userGuid); //用户长时间不进行操作,超时了 if (id == null) { filterContext.HttpContext.Response.Redirect("/adminlogin/Login.html"); } int userid = int.Parse(id.ToString()); IUserInfoService userInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; UserInfo userInfo = userInfoService.GetEntities(u => u.Id == userid).FirstOrDefault(); //将查出的用户赋值给当前登录用户 LoginUserInfo = userInfo; //设置滑动窗口机制,一旦登陆了,就给当前用户+20min CacheHelper.CacheHelper.SetCache(userGuid, userid, DateTime.Now.AddMinutes(20)); //给admin留后门 if (LoginUserInfo.UserName == "admin") { return; } else { string url = Request.Url.AbsolutePath.ToLower(); string httpmethod = Request.HttpMethod.ToLower(); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService r_UserInfo_ActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; var actioninfo = actionInfoService.GetEntities(a => a.HttpMethod.ToLower() == httpmethod && a.Url.ToLower() == url).FirstOrDefault(); if (actioninfo == null) { ContentResult content = new ContentResult(); content.ContentType = "text/javascript"; content.Content = "{data:500}"; filterContext.Result = content; } else { //第一条线,直接去判断这个权限是否属于登录用户 //1、首先拿到用户所拥有的权限 var actionlist = r_UserInfo_ActionInfoService.GetEntities(u => u.UserInfoId == LoginUserInfo.Id); //拿到要访问的那一条权限 var visitAction = (from r in actionlist where r.ActionInfoId == actioninfo.Id select r).FirstOrDefault(); if (visitAction != null) { //3、判断该条权限是否被允许 if (visitAction.HasPermission == true) { return; } else { ContentResult content = new ContentResult(); content.ContentType = "text/javascript"; content.Content = "{data:500}"; filterContext.Result = content; } } //第二条线 //1、先拿到该用户所有的角色 var userinfo = userInfoService.GetEntities(u => u.Id == LoginUserInfo.Id).FirstOrDefault(); var allroles = from r in userinfo.RoleInfo select r; //拿到这些角色所拥有的权限 var actions = from r in allroles from a in r.ActionInfo select a; //当前权限是否在角色对应的权限集合中 var count = (from a in actions where a.Id == actioninfo.Id select a).Count(); if (count <= 0) { ContentResult content = new ContentResult(); content.ContentType = "text/javascript"; content.Content = "{data:500}"; filterContext.Result = content; } } } } }
//在当前控制器所有方法执行之前执行此代码 protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); //登录时不需要验证是否登录 //#region 测试信息 ////TODO:测试结束后删除 //return; //#endregion if (IsCheck) { //从mm缓存中读取数据 if (Request.Cookies["loginuserId"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } string userGuid = Request.Cookies["loginuserId"].Value.ToString(); UserInfo user = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo; //用户长时间不进行操作,超时了 if (user == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } LoginUserInfo = user; //设置滑动窗口机制,一旦登陆了,就给当前用户+20min Common.Cache.CacheHelper.SetCache(userGuid, user, DateTime.Now.AddMinutes(20)); //给admin留后门,首页查询权限之后直接显示图标 if (LoginUserInfo.Uname == "admin") { return; } else { string url = Request.Url.AbsolutePath.ToLower(); string httpMethod = Request.HttpMethod.ToLower(); //通过一个容器创建对象 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService r_UserInfo_ActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; IUserInfoService userInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; var actionInfo =//拿到当前请求对应的权限 actionInfoService.GetEntities(u => u.Url.ToLower() == url && u.HttpMethod.ToLower() == httpMethod).FirstOrDefault(); if (actionInfo == null) { Response.Redirect("/Error.html"); } #region 第一条线 var action = r_UserInfo_ActionInfoService.GetEntities(u => u.UserInfoId == LoginUserInfo.Id); var item = (from s in action where s.ActionInfoId == actionInfo.Id select s).FirstOrDefault(); if (item != null) { if (item.HasPermission == true) { return; } else { Response.Redirect("/Error.html"); } } #endregion #region 第二条线 var userinfo = userInfoService.GetEntities(u => u.Id == LoginUserInfo.Id).FirstOrDefault(); //拿到所有角色 var roles = from r in userinfo.RoleInfo select r; //拿到所有角色对应的权限 var actions = from r in roles from a in r.ActionInfo select a; //当前权限是否在角色对应的权限集合中 var temp = (from a in actions where a.Id == actionInfo.Id select a).Count(); if (temp <= 0) { Response.Redirect("/Error.html"); } #endregion } } }
/// <summary> /// Area of Influence: internal of the inherited controller /// This method will run before other methods /// </summary> protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (ActivateCheck) { #region Store Login GUID in Cache // use memcache-Cookie instead of session if (Request.Cookies["userLoginGuid"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); } string userGuid = Request.Cookies["userLoginGuid"].Value; UserInfo userInfo = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo; if (userInfo == null) { // The cache data is expired/overtime, please login again filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); } LoginUser = userInfo; // Extend the cache time for 20 minutes Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); #endregion #region Permission check if (LoginUser.UserName == "a") { return; } string currentUrl = Request.Url.AbsolutePath.ToLower(); string currentHttpMethod = Request.HttpMethod.ToLower(); IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService ActionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService R_UserInfo_ActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as R_UserInfo_ActionInfoService; IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; // 1 check if the user has a special permission to the url with the httpmethod // check if the Url and the HttpMethod exist var actionInfo = ActionInfoService.GetEntities(a => a.Url.ToLower() == currentUrl && a.HttpMethod.ToLower() == currentHttpMethod).FirstOrDefault(); if (actionInfo == null) { // Url or HttpMethod not exist Response.Redirect("/Error.html"); } // check if the current user has permission to the page with the httpmethod var rUAInfo = R_UserInfo_ActionInfoService.GetEntities(u => u.UserInfoId == LoginUser.Id && u.ActionInfoId == actionInfo.Id && u.DelFlag == (short)DelFlagEnum.Normal).FirstOrDefault(); if (rUAInfo != null) { if (rUAInfo.HasPermission == true) { return; } else { Response.Redirect("/Error.html"); } } // 2 Check whether the user's corresponding role has this permission var user = UserInfoService.GetEntities(u => u.Id == LoginUser.Id && u.DelFlag == (short)DelFlagEnum.Normal).FirstOrDefault(); // get all user roles var allRoles = from r in user.RoleInfo select r; // get all role actions var actions = from r in allRoles from a in r.ActionInfo select a; // Detect if there is a role-action that matches the current action var result = (from a in actions where a.Id == actionInfo.Id select a).Count(); if (result <= 0) { Response.Redirect("/Error.html"); } #endregion #region Use Session for login check //if (filterContext.HttpContext.Session["loginUser"] == null) //{ // filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); //} //else //{ // LoginUser = filterContext.HttpContext.Session["loginUser"] as UserInfo; //} #endregion } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { //mvc请求来了之后,根据请求地址,创建控制器工厂(Spring.Net),控制器工厂创建控制器,执行方法。 //Spring.Net base.OnActionExecuting(filterContext); var items = filterContext.RouteData.Values; if (IsCheckUserLogin) { //使用mm+cookie代替session //校验用户是否已经登录 //从缓存中拿到当前的登录的用户信息。 if (Request.Cookies["userLoginId"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } string userGuid = Request.Cookies["userLoginId"].Value; UserInfo userInfo = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo; if (userInfo == null) { //用户长时间不操作,。超时。 filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } LoginUser = userInfo; //滑动窗口机制。 Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); //if (filterContext.HttpContext.Session["loginUser"] == null) //{ // filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); //} //else //{ // LoginUser = filterContext.HttpContext.Session["loginUser"] as UserInfo; //} //校验权限 //把当前请求对应的权限数据拿到。 if (LoginUser.UName == "admin") { return;//侯梦 } string url = Request.Url.AbsolutePath.ToLower(); string httpMethod = Request.HttpMethod.ToLower(); //默认都有流程的操作了 if (url.Contains("WFInstance".ToLower())) { return; } bool isGetMethodWithParameter = url.Count(ch => ch == '/') != 2; if (isGetMethodWithParameter) { int lastIndex = url.LastIndexOf('/'); url = url.Substring(0, lastIndex); } //通过容器创建一个对象。 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService rUserInfoActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; var actionInfo =//拿到当前请求对应的权限数据 actionInfoService.GetEntities(a => a.DelFlag == DeleteFlag.DelflagNormal && a.Url.ToLower() == url && a.HttpMethd.ToLower() == httpMethod) .FirstOrDefault(); if (actionInfo == null) { Response.Redirect("/Error.html"); } //一号线 var rUAs = rUserInfoActionInfoService.GetEntities(u => u.DelFlag == DeleteFlag.DelflagNormal && u.UserInfoID == LoginUser.ID); var item = (from a in rUAs where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (item != null) { if (item.HasPermission == true) { return; } else { Response.Redirect("/Error.html"); } } //2号 var user = UserInfoService.GetEntities(u => u.DelFlag == DeleteFlag.DelflagNormal && u.ID == LoginUser.ID).FirstOrDefault(); //拿到所有的角色 var allRoles = from r in user.RoleInfo select r; //通过角色拿到所有的权限 var actions = from r in allRoles from a in r.ActionInfo select a; //看当前权限是否在 角色对应权限集合中。 var temp = (from a in actions where a.ID == actionInfo.ID select a).Count(); if (temp <= 0) { Response.Redirect("/Error.html"); } } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (IsCheckUserLogin) { #region 用户登陆校验 //Memchache+Cookie方式 if (Request.Cookies["userLoginId"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } string userGuid = Request.Cookies["userLoginId"].Value; UserInfo userInfo = (UserInfo)Common.Cache.CacheHelper.GetCache (userGuid);//as UserInfo if (userInfo == null) { //长时间为操作 缓存已超时 filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } LoginUser = userInfo; //滑动窗口机制 (响应后刷新缓冲时间) Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); #region Session方式 //if (filterContext.HttpContext.Session["LoginUser"] == null && IsCheckedUserLogin) //{ // filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); //} //else //{ // LoginUser= filterContext.HttpContext.Session["LoginUser"] as UserInfo; //} #endregion #endregion #region 用户权限校验 //校验权限 //获取当前请求对应的权限数据 if (LoginUser.UName == "Moshang") { return;//Moshang`s backdoor } string url = Request.Url.AbsolutePath; string[] splitArr = url.Split('/'); string newStrurl = splitArr[0] + "/" + splitArr[1] + "/" + splitArr[2]; string httpMethod = Request.HttpMethod.ToLower(); //通过容器获取 IApplicationContext ctx = ContextRegistry.GetContext(); //ctx.GetObject("CacheHelper"); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService rUserInfoActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; //真·奥义·权限校验 var actionInfo = actionInfoService.GetEntities(a => a.Url.ToLower() == newStrurl && a.HttpMethd.ToLower() == httpMethod).FirstOrDefault(); if (actionInfo == null) { Response.Redirect("/Error.html"); } var rUAs = rUserInfoActionInfoService.GetEntities(u => u.UserInfoID == LoginUser.ID); var item = (from a in rUAs where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (item != null) { if (item.HasPermission == true) { return; } else { Response.Redirect("/Error.html"); } } //真·奥义·角色校验 var user = UserInfoService.GetEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); var allRoles = from r in user.RoleInfo select r; var actions = from r in allRoles from a in r.ActionInfo select a; var temp = (from a in actions where a.ID == actionInfo.ID select a).Count(); if (temp <= 0) { Response.Redirect("/Error.html"); } #endregion } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.AppendHeader("P3P", "CP=CAO PSA OUR"); base.OnActionExecuting(filterContext); //if (IsCheck) //{ // //检验用户是否登陆 // if (filterContext.HttpContext.Session["LoginUser"] == null) // { // filterContext.HttpContext.Response.Redirect("/Login/Index"); // } //} //else //{ // LoginUser = filterContext.HttpContext.Session["LoginUser"] as UserInfo; //} if (IsCheck) { if (filterContext.HttpContext.Request.Cookies["userid"] == null) { filterContext.HttpContext.Response.Redirect("/Login/Index"); } else { string userid = filterContext.HttpContext.Request.Cookies["userid"].Value.ToString(); var s = CacheHelper.GetCache("userid"); if (CacheHelper.GetCache(userid) != null) { LoginUser = (UserInfo)CacheHelper.GetCache(filterContext.HttpContext.Request.Cookies["userid"].Value); //滑动窗口 CacheHelper.SetCache(userid, LoginUser, DateTime.Now.AddMinutes(20)); } else { filterContext.HttpContext.Response.Redirect("/Login/Index"); } } if (LoginUser.UName == "wangzhen") { return; } else { string url = filterContext.HttpContext.Request.Url.AbsolutePath; string httpmethod = filterContext.HttpContext.Request.HttpMethod.ToLower(); //与当前登录的用户的权限进行对比 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService ActionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService UAInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; var action = ActionInfoService.GetEntities(a => a.Url.ToLower() == url && a.HttpMethod.ToLower() == httpmethod).FirstOrDefault(); if (action == null) { filterContext.HttpContext.Response.Redirect("/Error.html"); } //特殊权限校验 var rUAs = UAInfoService.GetEntities(u => u.UserInfoID == LoginUser.ID); var item = (from a in rUAs where a.ActionInfoID == action.ID select a).FirstOrDefault(); if (item != null) { if (item.IsPass == true) { return; } else { filterContext.HttpContext.Response.Redirect("/Error.html"); } } } } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); #region 测试:去掉登录验证 //return; #endregion if (IsCheckUserLogin) { #region 用户登录校验 //校验用户是否已经登录 //if (filterContext.HttpContext.Session["loginUser"] == null) //{ // filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); //} //else //{ // LoginUser = filterContext.HttpContext.Session["loginUser"] as UserInfo; //} //使用memcache+cookie代替session //从缓存中拿到当前登录的用户信息 if (Request.Cookies["userLoginId"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } string userGuid = Request.Cookies["userLoginId"].Value; UserInfo userInfo = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo; if (userInfo == null) { //用户长时间不操作,超时了 filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } LoginUser = userInfo; //滑动窗口机制 Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); #endregion #region 权限校验 if (LoginUser.UName == "wei" || LoginUser.UName == "张三" || LoginUser.UName == "李四") { return; //后门 } string url = Request.Url.AbsolutePath.ToLower(); string httpMethod = Request.HttpMethod.ToLower(); // 基类注入必须通过子类,这里先不用属性注入,用spring容器直接获取 IApplicationContext context = ContextRegistry.GetContext(); IActionInfoService actionInfoService = context.GetObject("ActionInfoService") as IActionInfoService; //直接通过容器 IR_UserInfo_ActionInfoService rUserInfoActionInfoService = context.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; IUserInfoService userInfoService = context.GetObject("UserInfoService") as IUserInfoService; //拿到当前请求的权限数据 var actionInfo = actionInfoService.GetEntities(u => u.Url.ToLower() == url && u.HttpMethod.ToLower() == httpMethod && u.DelFlag == this.delFlagNormal).FirstOrDefault(); if (actionInfo == null) { Response.Redirect("/Error.html"); } //拿到当前用户的特殊权限,然后看一下是否包括上述请求权限 var rUAs = rUserInfoActionInfoService.GetEntities( u => u.UserInfoId == LoginUser.Id && u.DelFlag == this.delFlagNormal); var item = (from r in rUAs where r.ActionInfoId == actionInfo.Id && r.DelFlag == this.delFlagNormal select r).FirstOrDefault(); if (item != null) { if (item.HasPermission == true) { return; //说明有这个权限,放行 } else { Response.Redirect("/Error.html"); //说明限制了这个权限,直接到错误页 } } //拿到当前用户的普通权限 var user = userInfoService.GetEntities(u => u.Id == LoginUser.Id && u.DelFlag == this.delFlagNormal).FirstOrDefault(); var allRoles = from r in user.RoleInfo where r.DelFlag == this.delFlagNormal select r; var actions = from r in allRoles from a in r.ActionInfo where a.DelFlag == this.delFlagNormal select a; var temp = (from a in actions where a.Id == actionInfo.Id select a).Count(); if (temp <= 0) { Response.Redirect("/Error.html"); //说明没有这个权限 } #endregion } }