/// <summary>
        /// 新增一个权限信息 HttpGet
        /// </summary>
        /// <returns></returns>
        public ViewResult Edit(string Id, bool isEdit = false)
        {
            ActionInfo model = actionInfoService.GetEntities(u => u.ID == Id).FirstOrDefault();

            if (model == null)
            {
                RedirectToAction("Index");
            }
            ViewBag.isEdit = isEdit;
            return(View(model));
        }
Пример #2
0
        public List<ActionInfo> GetActionInfos()
        {
            List<ActionInfo> actionList = new List<ActionInfo>();
            //拿到当前用户
            string userId = this.LoginUser.ID;
            var user = usersService.GetEntities(u => u.ID == userId).FirstOrDefault();
            //拿到当前用户所有权限【必须是菜单的权限】
            if (user != null)
            {
                IEnumerable<RoleInfo> allRole = user.RoleInfo;

                //拿到用户对应的所有角色的权限的id
                List<string> allRoleActionIds = (from r in allRole
                    from a in r.ActionInfo
                    //where a.IsMenu == true
                    select a.ID).ToList();
                //拿到用户直接拒绝的权限
                List<string> allDenyActionIds = (from r in user.R_UserInfo_ActionInfo
                    where r.HasPermission == false
                    select r.ActionInfoID).ToList();
                //角色权限-特殊拒绝权限
                List<string> allActionIds = allRoleActionIds.Where(u => !allDenyActionIds.Contains(u)).ToList();
                //特殊允许权限
                List<string> allPremissActionIds =
                    user.R_UserInfo_ActionInfo.Where(u => u.HasPermission == true).Select(u => u.ID).ToList();
                allActionIds.AddRange(allPremissActionIds.AsEnumerable());
                allActionIds = allActionIds.Distinct().ToList();
                actionList =
                    actionService.GetEntities(a => allActionIds.Contains(a.ID) && a.IsMenu)
                        .OrderBy(u => u.ActionName)
                        .ToList();
            }
            return actionList;
        }
Пример #3
0
        /// <summary>
        /// 设置用户特殊权限
        /// </summary>
        /// <param name="id">学生id</param>
        /// <returns></returns>
        public ActionResult SetUserAction(string id)
        {
            Dictionary <string, bool> existActions = new Dictionary <string, bool>();
            UserInfo model = userInfoService.GetEntities(u => u.ID == id).FirstOrDefault();

            ViewBag.AllActions   = actionService.GetEntities(u => true).ToList();
            existActions         = rUserActionService.GetEntities(u => u.UserInfoID == id).ToDictionary(u => u.ActionInfoID, u => u.HasPermission);
            ViewBag.ExistActions = existActions;
            return(View(model));
        }
Пример #4
0
        public ActionResult ActionSet()
        {
            int roleId   = int.Parse(Request["id"]);
            var roleData = roleInfoService.GetEntities(r => r.Id == roleId && r.DelFlag == 0)
                           .Select(r => new { r.Id, r.RoleName, r.SubTime, r.ActionInfo }).FirstOrDefault();
            var roleData2 = roleInfoService.GetEntities(r => r.Id == roleId && r.DelFlag == 0)
                            .Select(r => new { r.Id, r.RoleName, r.SubTime }).FirstOrDefault();
            var actionData = actionInfoService.GetEntities(r => r.DelFlag == 0)
                             .Select(r => new { r.Id, r.ActionName, r.Url, r.Remark, r.SubTime }).ToList();
            var exsitActions = (from a in roleData.ActionInfo
                                select a.Id).ToList();

            return(Json(new { role = roleData2, action = actionData, exAction = exsitActions }, JsonRequestBehavior.AllowGet));
        }
Пример #5
0
 // GET: ActionInfo/Edit/5
 public ActionResult Edit(int id)
 {
     ViewData.Model = actionInfoService.GetEntities(a => a.Id == id && a.DelFlag == 0).FirstOrDefault();
     return(View());
 }
Пример #6
0
        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;
                        }
                    }
                }
            }
        }
Пример #7
0
        //在当前控制器所有方法执行之前执行此代码
        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
                }
            }
        }
Пример #8
0
        /// <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");
                }
            }
        }
Пример #10
0
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            //Action执行之前判断memcache是否有值(用户是否已经登录)
            if (Request.Cookies["usersLoginId"] != null)
            {
                string usersLoginId = Request.Cookies["usersLoginId"].Value;
                object obj          = memcachedHelper.Get(usersLoginId);
                if (obj != null)
                {
                    userInfo = SerializerHelper.DeSerializerToObject <UserInfo>(obj.ToString());//反序列化
                    //模拟Session的滑动过期时间
                    memcachedHelper.Update(usersLoginId, obj, DateTime.Now.AddMinutes(20));
                    isExp = true;
                    //zhengyu可越狱
                    if (userInfo.UName == "zhengyu")
                    {
                        return;
                    }
                    //获取请求的绝对路径和请求方式
                    string requestUrl = Request.Url.AbsolutePath.ToLower();
                    string httpMethod = Request.HttpMethod;
                    //通过容器对象来创建对象,因基类注入不了
                    IApplicationContext ctx = ContextRegistry.GetContext();
                    IActionInfoService  actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService;
                    IUserInfoService    UserInfoService   = ctx.GetObject("UserInfoService") as IUserInfoService;
                    ActionInfo          actionInfo        = actionInfoService.GetEntities(a => a.Url == requestUrl && a.HttpMethd == httpMethod && a.DelFlag == (short)DelFlagEnum.Normal).FirstOrDefault();
                    if (actionInfo == null)
                    {
                        filterContext.Result = new RedirectResult("/Error.html");
                        //Response.Redirect("/Error.html");
                        return;
                    }
                    //第1条线.用户---权限
                    //登录用户
                    UserInfo loginUser = UserInfoService.GetEntities(u => u.ID == userInfo.ID).FirstOrDefault();
                    //判断登录用户请求的地址是否有权限
                    ActionInfo userActionOne = (from r in loginUser.R_UserInfo_ActionInfo
                                                where r.ActionInfoID == actionInfo.ID && r.HasPermission == true
                                                select r.ActionInfo).FirstOrDefault();
                    if (userActionOne == null)
                    {
                        //第2条线.用户---角色---权限
                        //判断登录用户请求的地址是否有权限
                        ActionInfo userActionTwo = (from r in loginUser.RoleInfo
                                                    from a in r.ActionInfo
                                                    where a.ID == actionInfo.ID
                                                    select a).FirstOrDefault();
                        if (userActionTwo == null)
                        {
                            //filterContext.Result = new RedirectResult("/ActionError.html");
                            //Response.Redirect("/ActionError.html");
                            filterContext.Result = new ContentResult()
                            {
                                Content = "您没有此权限!请联系管理员"
                            };         return;
                        }
                    }
                }
            }
            if (!isExp)
            {
                RedirectToAct.RedirectTo();
                //filterContext.HttpContext.Response.Redirect("/UserLogin/Index?return="+Request.Url);
                return;
            }
        }
Пример #11
0
        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");
                        }
                    }
                }
            }
        }
Пример #13
0
        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
            }
        }