示例#1
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            // do something before the action executes
            //base.OnActionExecuting(context);
            try
            {
                var userid = Convert.ToInt32(Env.GetUserInfo("Id", context.HttpContext.User.Claims));
                var roleid = Convert.ToInt32(Env.GetUserInfo("RoleId", context.HttpContext.User.Claims));

                var descriptor = context.ActionDescriptor as ControllerActionDescriptor;

                var actionName     = descriptor.ActionName.ToLower();
                var controllerName = descriptor.ControllerName.ToLower();

                var GetOrPost = context.HttpContext.Request.Method.ToString();
                //var checkAreaName = context.HttpContext.Request.RequestContext.RouteData.DataTokens["area"];
                string AreaName = "";
                //if (checkAreaName != null)
                //{
                //    AreaName = checkAreaName.ToString().ToLower() + "/";
                //}

                var cacheItemKey = "AllMenuBarFilter";

                var globle = _cache.Get(cacheItemKey);

                if (GetOrPost == "POST")
                {
                    ///if menupermission create,edit,delete then update value "true" in IsMenuChange file
                    if (controllerName == "menupermission" && (actionName == "create" || actionName == "edit" || actionName == "delete" || actionName == "multiviewindex"))
                    {
                        globle = MenuBarCache(cacheItemKey, globle, "shortcache");
                    }
                }

                if (globle == null)                                            //if cashe is null
                {
                    globle = MenuBarCache(cacheItemKey, globle, "60mincache"); //make cache from db
                }


                var menuaccess = (MenuOfRole[])globle;

                string menuUrl = AreaName + controllerName + "/" + actionName;

                if (IsActionNameEqualToCrudPageName(actionName))
                {
                    menuUrl = AreaName + controllerName;
                }


                var checkUrl = menuaccess.FirstOrDefault(i => (i.MenuURL == AreaName + controllerName + "/" + actionName) || i.MenuURL == menuUrl);
                ///checkUrl: check if menu url Exists in MenuPermission if not exists then will be run
                if (checkUrl != null)
                {
                    var checkControllerActionRoleUserId = menuaccess.FirstOrDefault(i => i.MenuURL == menuUrl && i.RoleId == roleid && i.UserId == userid);
                    ///check menu  && roleid && userid
                    if (checkControllerActionRoleUserId != null)
                    {
                        if (IsActionNameEqualToCrudPageName(actionName))
                        {
                            CheckAccessOfPageAction(context, actionName, checkControllerActionRoleUserId);
                        }
                        else
                        {
                            if (checkControllerActionRoleUserId.IsRead == false || checkControllerActionRoleUserId.IsDelete == false || checkControllerActionRoleUserId.IsCreate == false || checkControllerActionRoleUserId.IsUpdate == false)//if userid !=null && Check Crud
                            {
                                UnAuthoRedirect(context);
                            }
                        }
                    }
                    else
                    {
                        var checkControllerActionRole = menuaccess.FirstOrDefault(i => i.MenuURL == menuUrl && i.RoleId == roleid && i.UserId == null);
                        if (checkControllerActionRole != null)
                        {
                            if (IsActionNameEqualToCrudPageName(actionName))
                            {
                                CheckAccessOfPageAction(context, actionName, checkControllerActionRole);
                            }
                            else
                            {
                                if (checkControllerActionRole.IsRead == false || checkControllerActionRole.IsDelete == false || checkControllerActionRole.IsCreate == false || checkControllerActionRole.IsUpdate == false)//if userid !=null && Check Crud
                                {
                                    UnAuthoRedirect(context);
                                }
                            }
                        }
                        else
                        {
                            if (IsThisAjaxRequest(context) == false)//if userid !=null && Check Crud
                            {
                                UnAuthoRedirect(context);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            { }
        }