Beispiel #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);
        }
Beispiel #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);
        }