Ejemplo n.º 1
0
        public static bool IsAuthorize(string controllerName, string actionName, bool isPost = false)
        {
            var isAuthorize = false;

            ControllerActionRepository controllerActionRepository = new ControllerActionRepository();
            ControllerAction           controllerAction           = controllerActionRepository.GetAction(controllerName, actionName, isPost);

            if (controllerAction != null)
            {
                MVCAuthorizationRepository mvcAuthorizationRepository = new MVCAuthorizationRepository();
                List <string> roles = mvcAuthorizationRepository.GetRolesByControllerAction(controllerAction);

                CustomAuthorize customAuthorize = new CustomAuthorize()
                {
                    AuthorizedRoles = roles.ToArray()
                };

                isAuthorize = customAuthorize.Authorize();
            }

            return(isAuthorize);
        }
Ejemplo n.º 2
0
        public static MvcHtmlString ActionLinkWithRoles <T>(this HtmlHelper html, string linkText, Expression <Func <T, ActionResult> > action, RouteValueDictionary routeValues = null, IDictionary <string, object> htmlAttributes = null, bool isPost = false) where T : Controller
        {
            MvcHtmlString htmlStr = MvcHtmlString.Create("");

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

            MethodCallExpression methodExp = action.Body as MethodCallExpression;

            if (methodExp != null)
            {
                string actionName = methodExp.Method.Name;
                ControllerActionRepository controllerActionRepository = new ControllerActionRepository();
                ControllerAction           controllerAction           = controllerActionRepository.GetAction(controllerName, actionName, isPost);
                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))
                    {
                        if (isPost && htmlAttributes == null)
                        {
                            htmlStr = MvcHtmlString.Create("<input type='submit' value='" + linkText + "' />");
                        }
                        else
                        {
                            htmlStr = html.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
                        }
                    }
                }
            }
            return(htmlStr);
        }
Ejemplo n.º 3
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);
        }