/// <summary> /// Create auth button /// </summary> /// <param name="htmlHelper">Html helper</param> /// <param name="options">Button options</param> /// <returns>Return button html content</returns> public static IHtmlContent AuthButton(this IHtmlHelper htmlHelper, AuthButtonOptions options) { if (options == null || (options.UseNowVerifyResult && !options.AllowAccess && options.ForbidStyle != ForbidStyle.Disable)) { return(HtmlString.Empty); } bool allowAccess = true; if (!options.UseNowVerifyResult) { allowAccess = AuthorizationManager.Authorize(new AuthorizeOptions() { Action = options.ActionOptions?.Action, Controller = options.ActionOptions?.Controller, Area = options.ActionOptions?.Area, Application = ApplicationManager.Current, Claims = HttpContextHelper.Current.User.Claims.ToDictionary(c => c.Type, c => c.Value), Method = options.ActionOptions?.Method, ActionContext = htmlHelper?.ViewContext })?.AllowAccess ?? false; } if (!allowAccess && options.ForbidStyle != ForbidStyle.Disable) { return(HtmlString.Empty); } var btnTagBuilder = new TagBuilder("button"); var btnHtmlAttributes = options.HtmlAttributes ?? new Dictionary <string, object>(); if (!btnHtmlAttributes.ContainsKey("type")) { btnHtmlAttributes.Add("type", "button"); } if (!allowAccess && !btnHtmlAttributes.ContainsKey("disabled")) { btnHtmlAttributes.Add("disabled", "disabled"); } btnTagBuilder.MergeAttributes(btnHtmlAttributes); if (options.UseIco) { var icoTagBuilder = new TagBuilder("i"); icoTagBuilder.MergeAttributes(options.IcoHtmlAttributes); btnTagBuilder.InnerHtml.AppendHtml(icoTagBuilder); btnTagBuilder.InnerHtml.Append(" "); } btnTagBuilder.InnerHtml.Append(options.Text); return(btnTagBuilder); }
/// <summary> /// Create auth link /// </summary> /// <param name="htmlHelper">Html helper</param> /// <param name="options">Options</param> /// <returns>Return html content</returns> public static IHtmlContent AuthLink(this IHtmlHelper htmlHelper, AuthButtonOptions options) { if (options == null || (options.UseNowVerifyResult && !options.AllowAccess)) { return(HtmlString.Empty); } if (!options.UseNowVerifyResult) { var allowAccess = AuthorizationManager.Authorize(new AuthorizeOptions() { Action = options.ActionOptions?.Action, Controller = options.ActionOptions?.Controller, Area = options.ActionOptions?.Area, Application = ApplicationManager.Current, Claims = HttpContextHelper.Current.User.Claims.ToDictionary(c => c.Type, c => c.Value), ActionContext = htmlHelper?.ViewContext, Method = options.ActionOptions?.Method })?.AllowAccess ?? false; if (!allowAccess) { return(HtmlString.Empty); } } var btnTagBuilder = new TagBuilder("a"); var btnHtmlAttributes = options.HtmlAttributes ?? new Dictionary <string, object>(); if (!btnHtmlAttributes.ContainsKey("href")) { btnHtmlAttributes.Add("href", "javascript:void(0)"); } btnTagBuilder.MergeAttributes(btnHtmlAttributes); if (options.UseIco) { var icoTagBuilder = new TagBuilder("i"); icoTagBuilder.MergeAttributes(options.IcoHtmlAttributes); btnTagBuilder.InnerHtml.AppendHtml(icoTagBuilder); btnTagBuilder.InnerHtml.Append(" "); } btnTagBuilder.InnerHtml.Append(options.Text); return(btnTagBuilder); }
/// <summary> /// Create auth menu element /// </summary> /// <param name="htmlHelper">Html helper</param> /// <param name="urlHelper">Url helper</param> /// <param name="text">Test</param> /// <param name="url">Url</param> /// <param name="options">Options</param> /// <returns>Return menu element</returns> public static IHtmlContent AuthMenuItem(this IHtmlHelper htmlHelper, IUrlHelper urlHelper, string text, string controlerCode, string actionCode, AuthButtonOptions options = null) { options ??= new AuthButtonOptions(); options.HtmlAttributes ??= new Dictionary <string, object>(); options.HtmlAttributes["action"] = urlHelper.Action(actionCode, controlerCode); options.Text = text; options.ActionOptions = new AuthorizationActionOptions(controlerCode, actionCode); return(htmlHelper.AuthHtmlElement("li", options)); }
/// <summary> /// Create auth html element /// </summary> /// <param name="htmlHelper">Html helper</param> /// <param name="tagName">Tag name</param> /// <param name="options">Options</param> /// <returns>Return html content</returns> public static IHtmlContent AuthHtmlElement(this IHtmlHelper htmlHelper, string tagName, AuthButtonOptions options) { if (options == null || (options.UseNowVerifyResult && !options.AllowAccess)) { return(HtmlString.Empty); } if (!options.UseNowVerifyResult) { var allowAccess = AuthorizationManager.Authorize(new AuthorizeOptions() { Action = options.ActionOptions?.Action, Controller = options.ActionOptions?.Controller, Application = ApplicationManager.Current, Claims = HttpContextHelper.Current.User.Claims.ToDictionary(c => c.Type, c => c.Value), ActionContext = htmlHelper?.ViewContext, Method = options.ActionOptions?.Method, Area = options.ActionOptions?.Area })?.AllowAccess ?? false; if (!allowAccess) { return(HtmlString.Empty); } } var htmlTagBuilder = new TagBuilder(tagName); var htmlAttributes = options.HtmlAttributes ?? new Dictionary <string, object>(); htmlTagBuilder.MergeAttributes(htmlAttributes); if (options.UseIco) { var icoTagBuilder = new TagBuilder("i"); icoTagBuilder.MergeAttributes(options.IcoHtmlAttributes); htmlTagBuilder.InnerHtml.AppendHtml(icoTagBuilder); htmlTagBuilder.InnerHtml.Append(" "); } htmlTagBuilder.InnerHtml.Append(options.Text); return(htmlTagBuilder); }