using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using Microsoft.Owin.Security; using Newtonsoft.Json; using Umbraco.Core.Composing; using Umbraco.Web.Editors; using Umbraco.Web.Models; namespace Umbraco.Web { using Core.Configuration; /// /// HtmlHelper extensions for the back office /// public static class HtmlHelperBackOfficeExtensions { /// /// Outputs a script tag containing the bare minimum (non secure) server vars for use with the angular app /// /// /// /// /// The post url used to sign in with external logins - this can change depending on for what service the external login is service. /// Example: normal back office login or authenticating upgrade login /// /// /// /// These are the bare minimal server variables that are required for the application to start without being authenticated, /// we will load the rest of the server vars after the user is authenticated. /// public static IHtmlString BareMinimumServerVariablesScript(this HtmlHelper html, UrlHelper uri, string externalLoginsUrl) { var serverVars = new BackOfficeServerVariables(uri, Current.RuntimeState); var minVars = serverVars.BareMinimumServerVariables(); var str = @""; return html.Raw(str); } /// /// Used to render the script that will pass in the angular "externalLoginInfo" service/value on page load /// /// /// /// public static IHtmlString AngularValueExternalLoginInfoScript(this HtmlHelper html, IEnumerable externalLoginErrors) { var loginProviders = html.ViewContext.HttpContext.GetOwinContext().Authentication.GetExternalAuthenticationTypes() .Where(p => p.Properties.ContainsKey("UmbracoBackOffice")) .Select(p => new { authType = p.AuthenticationType, caption = p.Caption, properties = p.Properties }) .ToArray(); var sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine(@"var errors = [];"); if (externalLoginErrors != null) { foreach (var error in externalLoginErrors) { sb.AppendFormat(@"errors.push(""{0}"");", error).AppendLine(); } } sb.AppendLine(@"app.value(""externalLoginInfo"", {"); sb.AppendLine(@"errors: errors,"); sb.Append(@"providers: "); sb.AppendLine(JsonConvert.SerializeObject(loginProviders)); sb.AppendLine(@"});"); return html.Raw(sb.ToString()); } /// /// Used to render the script that will pass in the angular "resetPasswordCodeInfo" service/value on page load /// /// /// /// public static IHtmlString AngularValueResetPasswordCodeInfoScript(this HtmlHelper html, object val) { var sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine(@"var errors = [];"); var errors = val as IEnumerable; if (errors != null) { foreach (var error in errors) { sb.AppendFormat(@"errors.push(""{0}"");", error).AppendLine(); } } var resetCodeModel = val as ValidatePasswordResetCodeModel; sb.AppendLine(@"app.value(""resetPasswordCodeInfo"", {"); sb.AppendLine(@"errors: errors,"); sb.Append(@"resetCodeModel: "); sb.AppendLine(JsonConvert.SerializeObject(resetCodeModel)); sb.AppendLine(@"});"); return html.Raw(sb.ToString()); } } }