Exemplo n.º 1
0
 /// <summary>
 /// Allows or blocks the display of an action link depending on user authorization level.
 /// </summary>
 public static IHtmlContent ActionLinkAuth(this IHtmlHelper htmlHelper, Enumerations.Role requiredPrivilege, Enumerations.AuthFailedBehaviour authFailedBehaviour, string linkText, string actionName, object routeValues, object htmlAttributes, bool requiresFullAccess = false)
 {
     return(ActionLinkAuth(htmlHelper, requiredPrivilege, authFailedBehaviour, linkText, actionName, null, routeValues, htmlAttributes, null));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Allows or blocks the display of an action link depending on user authorization level.
        /// </summary>
        public static IHtmlContent ActionLinkAuth(this IHtmlHelper htmlHelper, Enumerations.Role requiredPrivilege, Enumerations.AuthFailedBehaviour authFailedBehaviour, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes, string alternateLinkText)
        {
            //If no link text, just return an empty string.
            if (string.IsNullOrEmpty(linkText))
            {
                return(HtmlString.Empty);
            }

            // If user has no privileges refuse access
            bool hasAccess = new UserSessionContext(htmlHelper.GetHttpContext()).UserHasAccess(requiredPrivilege);

            var htmlOptions  = new RouteValueDictionary(htmlAttributes);
            var routeOptions = new RouteValueDictionary(routeValues);

            //Remove disabled attribute if present
            if (htmlOptions.ContainsKey("disabled"))
            {
                htmlOptions.Remove("disabled");
            }

            IHtmlContent result = htmlHelper.ActionLink(linkText, actionName, controllerName, routeOptions, htmlOptions);

            if (!hasAccess)
            {
                switch (authFailedBehaviour)
                {
                case Enumerations.AuthFailedBehaviour.AlternateLink:
                    if (string.IsNullOrEmpty(alternateLinkText))
                    {
                        throw new ArgumentNullException("alternateLinkText", "alternateLinkText cannot be null or empty");
                    }
                    result = htmlHelper.ActionLink(alternateLinkText, actionName, controllerName, routeOptions, htmlOptions);
                    break;

                case Enumerations.AuthFailedBehaviour.EmptyString:
                    result = HtmlString.Empty;
                    break;

                case Enumerations.AuthFailedBehaviour.InnerHtml:
                    result = htmlHelper.Span(linkText, htmlOptions);
                    break;

                default:
                    throw new ArgumentOutOfRangeException("authFailedBehaviour");
                }
            }

            return(result);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Allows or blocks the display of an action link depending on user authorization level.
 /// </summary>
 public static IHtmlContent ActionLinkAuth(this IHtmlHelper htmlHelper, Enumerations.Role requiredPrivilege, Enumerations.AuthFailedBehaviour authFailedBehaviour, string linkText, string actionName, string controllerName, bool requiresFullAccess = false)
 {
     return(ActionLinkAuth(htmlHelper, requiredPrivilege, authFailedBehaviour, linkText, actionName, controllerName, null, null, null));
 }