2017-09-13 17:35:20 +02:00
|
|
|
|
using System.Collections.Generic;
|
2015-04-01 16:04:19 +11:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
|
using Microsoft.Owin.Security;
|
|
|
|
|
|
using Newtonsoft.Json;
|
2017-09-13 17:35:20 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
2015-04-01 16:04:19 +11:00
|
|
|
|
using Umbraco.Web.Editors;
|
2018-03-29 22:13:41 +11:00
|
|
|
|
using Umbraco.Web.Features;
|
2016-04-13 13:51:12 +02:00
|
|
|
|
using Umbraco.Web.Models;
|
2015-04-01 16:04:19 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web
|
|
|
|
|
|
{
|
2017-09-13 17:35:20 +02:00
|
|
|
|
using Core.Configuration;
|
2019-08-19 14:36:56 +01:00
|
|
|
|
using Umbraco.Web.JavaScript;
|
2016-04-12 18:07:25 +02:00
|
|
|
|
|
2015-04-01 16:04:19 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// HtmlHelper extensions for the back office
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class HtmlHelperBackOfficeExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Outputs a script tag containing the bare minimum (non secure) server vars for use with the angular app
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="html"></param>
|
|
|
|
|
|
/// <param name="uri"></param>
|
2015-04-02 14:46:53 +11:00
|
|
|
|
/// <param name="externalLoginsUrl">
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
/// </param>
|
2018-03-29 22:13:41 +11:00
|
|
|
|
/// <param name="features"></param>
|
2018-04-06 13:51:54 +10:00
|
|
|
|
/// <param name="globalSettings"></param>
|
2015-04-01 16:04:19 +11:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// 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.
|
|
|
|
|
|
/// </remarks>
|
2018-04-06 13:51:54 +10:00
|
|
|
|
public static IHtmlString BareMinimumServerVariablesScript(this HtmlHelper html, UrlHelper uri, string externalLoginsUrl, UmbracoFeatures features, IGlobalSettings globalSettings)
|
2015-04-01 16:04:19 +11:00
|
|
|
|
{
|
2018-04-06 13:51:54 +10:00
|
|
|
|
var serverVars = new BackOfficeServerVariables(uri, Current.RuntimeState, features, globalSettings);
|
2017-09-08 19:39:13 +02:00
|
|
|
|
var minVars = serverVars.BareMinimumServerVariables();
|
|
|
|
|
|
|
2015-04-01 16:04:19 +11:00
|
|
|
|
var str = @"<script type=""text/javascript"">
|
|
|
|
|
|
var Umbraco = {};
|
|
|
|
|
|
Umbraco.Sys = {};
|
2017-09-08 19:39:13 +02:00
|
|
|
|
Umbraco.Sys.ServerVariables = " + JsonConvert.SerializeObject(minVars) + @";
|
2015-04-01 16:04:19 +11:00
|
|
|
|
</script>";
|
|
|
|
|
|
|
|
|
|
|
|
return html.Raw(str);
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|
2015-04-01 16:04:19 +11:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-04-13 13:51:12 +02:00
|
|
|
|
/// Used to render the script that will pass in the angular "externalLoginInfo" service/value on page load
|
2015-04-01 16:04:19 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="html"></param>
|
|
|
|
|
|
/// <param name="externalLoginErrors"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2016-04-13 13:51:12 +02:00
|
|
|
|
public static IHtmlString AngularValueExternalLoginInfoScript(this HtmlHelper html, IEnumerable<string> externalLoginErrors)
|
2015-04-01 16:04:19 +11:00
|
|
|
|
{
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
2015-06-16 10:59:47 +02:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
sb.AppendLine();
|
2015-04-01 16:04:19 +11:00
|
|
|
|
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());
|
|
|
|
|
|
}
|
2016-04-13 13:51:12 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used to render the script that will pass in the angular "resetPasswordCodeInfo" service/value on page load
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="html"></param>
|
|
|
|
|
|
/// <param name="val"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static IHtmlString AngularValueResetPasswordCodeInfoScript(this HtmlHelper html, object val)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
|
sb.AppendLine(@"var errors = [];");
|
|
|
|
|
|
|
|
|
|
|
|
var errors = val as IEnumerable<string>;
|
|
|
|
|
|
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"", {");
|
2017-07-20 11:21:28 +02:00
|
|
|
|
sb.AppendLine(@"errors: errors,");
|
2016-04-13 13:51:12 +02:00
|
|
|
|
sb.Append(@"resetCodeModel: ");
|
|
|
|
|
|
sb.AppendLine(JsonConvert.SerializeObject(resetCodeModel));
|
|
|
|
|
|
sb.AppendLine(@"});");
|
|
|
|
|
|
|
2019-08-19 14:36:56 +01:00
|
|
|
|
return html.Raw(sb.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static IHtmlString AngularValueTinyMceAssets(this HtmlHelper html)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ctx = new HttpContextWrapper(HttpContext.Current);
|
|
|
|
|
|
var files = JsInitialization.OptimizeTinyMceScriptFiles(ctx);
|
|
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
sb.AppendLine(@"app.value(""tinyMceAssets"",");
|
|
|
|
|
|
sb.AppendLine(JsonConvert.SerializeObject(files));
|
|
|
|
|
|
sb.AppendLine(@");");
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-13 13:51:12 +02:00
|
|
|
|
return html.Raw(sb.ToString());
|
|
|
|
|
|
}
|
2015-04-01 16:04:19 +11:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|