コード例 #1
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            #region home
            string soLibID = SoLogin.IsLogin();

            var model = SoLogin.GetUserModel();

            if (string.IsNullOrEmpty(soLibID) || model == null)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())//处理Ajax请求
                {
                    JsonResult jr = new JsonResult();
                    jr.Data = new { Code = -200, Message = "登录超时,请重新登录再操作!", Data = "", Url = ConfigHelper.GetSoUrl };
                    jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                    filterContext.Result   = jr;
                }
                else
                {
                    string url         = filterContext.HttpContext.Request.Url.ToString();
                    string regexString = ConfigHelper.GetHomeUrl + @":\d{0,5}";
                    Regex  regex       = new Regex(regexString);
                    if (regex.IsMatch(url))
                    {
                        url = regex.Replace(url, ConfigHelper.GetHomeUrl);
                    }

                    var rawurl = System.Web.HttpUtility.UrlEncode(url);

                    //string loginSoUrl = ConfigHelper.GetSoLoginUrl;
                    string loginSoUrl = string.Format("{0}?url={1}", ConfigHelper.GetSoUrl, rawurl);
                    string newurl     = loginSoUrl;
                    filterContext.Result = new RedirectResult(newurl);
                }
            }
            else
            {
                using (var db = new EFContext())
                {
                    var query = new List <Menu>().AsQueryable();

                    var selshop = db.selshop.FirstOrDefault(x => x.ID == model.ShopId);

                    var selmanagerowner     = db.selmanager.FirstOrDefault(x => x.ShopID == model.ShopId && x.IsOwner == 1);
                    var selmanagerroleowner = db.selmanager_role.FirstOrDefault(x => x.SelManagerID == selmanagerowner.ID);
                    var rolepemitidowner    = db.selrole_pemit.Where(x => x.SelRoleID == selmanagerroleowner.SelRoleID).Select(x => x.SelPemitID).ToList();

                    var selmanagerrole = db.selmanager_role.FirstOrDefault(x => x.SelManagerID == model.ManagerId);
                    var rolepemitid    = db.selrole_pemit.Where(x => x.SelRoleID == selmanagerrole.SelRoleID).Select(x => x.SelPemitID).ToList();
                    if (model.IsOwner == 1)
                    {
                        query = db.selpemit.Where(x => x.IsMenu == 1)
                                .Where(x => rolepemitid.Contains(x.ID))
                                .Select(x => new Menu {
                            MenuId = x.ID
                        });
                    }
                    else
                    {
                        //管理员权限下面的 有效权限

                        query = db.selpemit.Where(x => x.IsMenu == 1)
                                .Where(x => rolepemitid.Contains(x.ID))
                                .Where(x => rolepemitidowner.Contains(x.ID))
                                .Select(x => new Menu()
                        {
                            MenuId = x.ID
                        });
                    }


                    var listmenu = query.ToList();

                    bool hasAuthorization = false;
                    var  f = listmenu.FirstOrDefault(a => a.MenuId == menuId.ToString());
                    if (f != null)
                    {
                        hasAuthorization = true;
                    }

                    if (!hasAuthorization)//没有权限
                    {
                        string redirecturl = string.Format("{0}/{1}/ErrorNopermit", ConfigHelper.GetHomeUrl, "Common");

                        if (filterContext.HttpContext.Request.IsAjaxRequest())//处理Ajax请求
                        {
                            JsonResult jr = new JsonResult();
                            jr.Data = new { Code = -200, Message = "没有访问权限!", Data = "", Url = redirecturl };
                            jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                            filterContext.Result   = jr;
                        }
                        else
                        {
                            filterContext.Result = new RedirectResult(redirecturl);
                        }
                    }
                    else
                    {
                        base.OnActionExecuting(filterContext);
                    }
                }
            }


            #endregion
        }
コード例 #2
0
ファイル: BaseController.cs プロジェクト: sxycxwb/YRD.MP
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region test

            //filterContext.HttpContext.Session["ManagerId"] = 3;
            //filterContext.HttpContext.Session["ShopId"] = 1;
            #endregion

            #region home

            var    controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            var    actionName     = filterContext.ActionDescriptor.ActionName;
            string tempurl        = string.Format("/{0}/{1}", controllerName, actionName);
            //var selpemit = DB.selpemit.Value.FirstOrDefault(x => x.Url == tempurl);
            //if (selpemit == null)
            //{
            //    filterContext.Controller.ViewBag.menuid = 0;
            //}
            //else
            //{
            //    filterContext.Controller.ViewBag.menuid = selpemit.ID;
            //}



            string soLibID = SoLogin.IsLogin();

            var model = SoLogin.GetUserModel();

            if (string.IsNullOrEmpty(soLibID) || model == null)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())//处理Ajax请求
                {
                    JsonResult jr = new JsonResult();
                    jr.Data = new { Code = -200, Message = "登录超时,请重新登录再操作!", Data = "", Url = ConfigHelper.GetSoUrl };
                    jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                    filterContext.Result   = jr;
                }
                else
                {
                    string url         = filterContext.HttpContext.Request.Url.ToString();
                    string regexString = ConfigHelper.GetWwwUrl + @":\d{0,5}";
                    Regex  regex       = new Regex(regexString);
                    if (regex.IsMatch(url))
                    {
                        url = regex.Replace(url, ConfigHelper.GetWwwUrl);
                    }

                    var rawurl = System.Web.HttpUtility.UrlEncode(url);

                    //string loginSoUrl = ConfigHelper.GetSoLoginUrl;
                    string loginSoUrl = string.Format("{0}?url={1}", ConfigHelper.GetSoUrl, System.Web.HttpUtility.UrlEncode(rawurl));
                    string newurl     = loginSoUrl;
                    filterContext.Result = new RedirectResult(newurl);
                }
            }
            else
            {
                using (var db = new EFContext())
                {
                    //控制左边菜单显示
                    var query   = new List <ViewPemit>();
                    var selshop = db.selshop.Where(x => x.ID == model.ShopId).Select(x => new { x.ID, x.AuditStatus, x.VersionID }).FirstOrDefault();
                    if (selshop.AuditStatus != 1)
                    {
                        query.Add(new ViewPemit()
                        {
                            ID = "1000", IsDel = 0, PemitType = 0, Title = "完善资料", Sort = 99, Url = "/SM/CompleteInfo", ParentID = "8"
                        });
                    }
                    //查询网店所有者用户拥有的权限

                    var selmanagerowner     = db.selmanager.FirstOrDefault(x => x.ShopID == model.ShopId && x.IsOwner == 1);
                    var selmanagerroleowner = db.selmanager_role.FirstOrDefault(x => x.SelManagerID == selmanagerowner.ID);
                    var rolepemitidowner    = db.selrole_pemit.Where(x => x.SelRoleID == selmanagerroleowner.SelRoleID).Select(x => x.SelPemitID).ToList();

                    var selmanagerrole = db.selmanager_role.FirstOrDefault(x => x.SelManagerID == model.ManagerId);
                    var rolepemitid    = db.selrole_pemit.Where(x => x.SelRoleID == selmanagerrole.SelRoleID).Select(x => x.SelPemitID).ToList();



                    if (model.IsOwner == 1)
                    {
                        query = db.selpemit.Where(x => x.IsMenu == 1)
                                .Where(x => rolepemitid.Contains(x.ID))
                                .Select(x => new ViewPemit()
                        {
                            ID = x.ID, Title = x.Title, ParentID = x.ParentID, IsDel = x.IsDel, ImgUrl = x.ImgUrl, PemitType = x.PemitType, Sort = x.Sort, Url = x.Url
                        }).ToList();
                    }
                    else
                    {
                        //管理员权限下面的 有效权限
                        query = db.selpemit.Where(x => x.IsMenu == 1)
                                .Where(x => rolepemitid.Contains(x.ID))
                                .Where(x => rolepemitidowner.Contains(x.ID))
                                .Select(x => new ViewPemit()
                        {
                            ID = x.ID, Title = x.Title, ParentID = x.ParentID, IsDel = x.IsDel, ImgUrl = x.ImgUrl, PemitType = x.PemitType, Sort = x.Sort, Url = x.Url
                        }).ToList();
                    }


                    filterContext.Controller.ViewBag.MenuList = query;
                    filterContext.Controller.ViewBag.menuid   = 0;
                }

                if (model.UserLoginType == (int)UserLoginType.UserSelf || model.UserLoginType == (int)UserLoginType.Manager)
                {
                    if (filterContext.HttpContext.Session["SoLibID"] == null || filterContext.HttpContext.Session["SoLibID"].ToString() != soLibID)
                    {
                        filterContext.HttpContext.Session["SoLibID"]   = soLibID;
                        filterContext.HttpContext.Session["ManagerId"] = model.ManagerId;
                        filterContext.HttpContext.Session["UserName"]  = model.UserName;
                        filterContext.HttpContext.Session["NickName"]  = model.NickName;
                        filterContext.HttpContext.Session["ShopId"]    = model.ShopId;
                        filterContext.HttpContext.Session["ShopName"]  = model.ShopName;
                        filterContext.HttpContext.Session["IsOwner"]   = model.IsOwner;
                    }
                }
                else if (model.UserLoginType == (int)UserLoginType.SubAccount)
                {
                    if (filterContext.HttpContext.Session["SoLibID"] == null || filterContext.HttpContext.Session["SoLibID"].ToString() != soLibID)
                    {
                        filterContext.HttpContext.Session["SoLibID"]   = soLibID;
                        filterContext.HttpContext.Session["ManagerId"] = model.ManagerId;
                        filterContext.HttpContext.Session["UserName"]  = model.UserName;
                        filterContext.HttpContext.Session["NickName"]  = model.NickName;
                        filterContext.HttpContext.Session["ShopId"]    = model.ShopId;
                        filterContext.HttpContext.Session["ShopName"]  = model.ShopName;
                        filterContext.HttpContext.Session["IsOwner"]   = model.IsOwner;
                    }
                }
                else
                {
                    if (filterContext.HttpContext.Request.IsAjaxRequest())//处理Ajax请求
                    {
                        JsonResult jr = new JsonResult();
                        jr.Data = new { Code = 0, Message = "没有访问权限!", Data = "" };
                        jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                        filterContext.Result   = jr;
                    }
                    else
                    {
                        string wwwUrl = ConfigHelper.GetWwwUrl;
                        string url    = wwwUrl;
                        filterContext.Result = new RedirectResult(url);
                    }
                }
                filterContext.Controller.ViewBag.UserName = model.UserName;

                string VersionName = string.Empty;
                //获取店铺等级
                using (var db = new EFContext())
                {
                    var selshop = db.selshop.FirstOrDefault(x => x.ID == model.ShopId);
                    model.ShopName = selshop.ShopName;
                    var sysversion = db.sysversion.FirstOrDefault(x => x.VersionID == selshop.VersionID);
                    if (sysversion != null)
                    {
                        VersionName = sysversion.Name;
                    }
                }
                filterContext.Controller.ViewBag.ShopName    = model.ShopName;
                filterContext.Controller.ViewBag.VersionName = VersionName;
            }

            #endregion
        }