Beispiel #1
0
        public static MvcHtmlString ActionLinkWithRoles_Old <T>(this HtmlHelper html, string linkText, Expression <Func <T, ActionResult> > action) where T : Controller
        {
            ControllerActionRepository actionAuthorizationRepo = new ControllerActionRepository();

            actionAuthorizationRepo.UpdateActionList();

            MvcHtmlString htmlStr = null;

            ReflectedControllerDescriptor controllerDes = new ReflectedControllerDescriptor(typeof(T));
            string controllerName = controllerDes.ControllerName;

            MethodCallExpression methodExp = action.Body as MethodCallExpression;

            if (methodExp != null)
            {
                string actionName = methodExp.Method.Name;
                //List<ActionDescriptor> actionDescriptorList = controllerDes.GetCanonicalActions().Where(r => r.ActionName == actionName).ToList();
                //ActionDescriptor actionDes = actionDescriptorList.FirstOrDefault();

                ControllerActionRepository controllerActionRepository = new ControllerActionRepository();
                ControllerAction           controllerAction           = controllerActionRepository.GetActionWithVerbHttpGet(controllerName, actionName);
                if (controllerAction != null)
                {
                    MVCAuthorizationRepository mvcAuthorizationRepository = new MVCAuthorizationRepository();
                    List <string> roles = mvcAuthorizationRepository.GetRolesByControllerAction(controllerAction);

                    CustomAuthorize customAuthorize = new CustomAuthorize()
                    {
                        AuthorizedRoles = roles.ToArray()
                    };
                    if (customAuthorize.Authorize(html.ViewContext.HttpContext))
                    {
                        htmlStr = html.ActionLink(linkText, actionName, controllerName);
                    }
                }

                //    if (actionDes != null)
                //    {
                //        List<CustomAuthorize> customAuthorizeList = actionDes.GetFilters().AuthorizationFilters.Where(r => r is CustomAuthorize).Select(r => r as CustomAuthorize).ToList();

                //        if (customAuthorizeList.Count > 0)
                //        {
                //            if (customAuthorizeList.Select(r => r.Authorize(html.ViewContext.HttpContext)).Any(r => r))
                //            {
                //                htmlStr = html.ActionLink(linkText, actionName, controllerName);
                //            }
                //        }
                //        else
                //        {
                //            htmlStr = html.ActionLink(linkText, actionName, controllerName);
                //        }

                //        //List<CustomAuthorize> ofController = controllerDes.GetCustomAttributes(typeof(CustomAuthorize), true).Select(r => r as CustomAuthorize).ToList();
                //        //List<CustomAuthorize> ofAction = actionDes.GetCustomAttributes(typeof(CustomAuthorize), true).Select(r => r as CustomAuthorize).ToList();

                //        //List<CustomAuthorize> ofAll = new List<CustomAuthorize>();
                //        //ofAll.AddRange(ofController);
                //        //ofAll.AddRange(ofAction);

                //        //string[] roles = ofAll.SelectMany(r => r.AuthorizedRoles).Distinct().ToArray();
                //        //string[] users = ofAll.SelectMany(r => r.AuthorizedUsers).Distinct().ToArray();

                //        //if (HttpContext.Current.User.HasAnyRole(roles)
                //        //    || users.Contains(OAMSSetting.Username))
                //        //{
                //        //    htmlStr = html.ActionLink(linkText, actionName, controllerName);
                //        //}
                //    }
            }

            return(htmlStr);
        }