2020-05-25 19:37:16 +10:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
|
using Microsoft.AspNetCore.Html;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using System.Collections.Generic;
|
2015-04-01 16:04:19 +11:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2020-05-25 19:37:16 +10:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Umbraco.Core;
|
2020-04-02 17:41:00 +11:00
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
2020-02-13 07:56:50 +01:00
|
|
|
|
using Umbraco.Core.Hosting;
|
2020-04-02 17:41:00 +11:00
|
|
|
|
using Umbraco.Core.WebAssets;
|
2020-05-25 19:37:16 +10:00
|
|
|
|
using Umbraco.Web.BackOffice.Controllers;
|
2020-06-02 13:28:30 +10:00
|
|
|
|
using Umbraco.Web.Common.Security;
|
2018-03-29 22:13:41 +11:00
|
|
|
|
using Umbraco.Web.Features;
|
2016-04-13 13:51:12 +02:00
|
|
|
|
using Umbraco.Web.Models;
|
2020-05-25 19:37:16 +10:00
|
|
|
|
using Umbraco.Web.WebApi;
|
2020-04-02 21:19:42 +11:00
|
|
|
|
using Umbraco.Web.WebAssets;
|
2015-04-01 16:04:19 +11:00
|
|
|
|
|
2020-05-25 19:37:16 +10:00
|
|
|
|
namespace Umbraco.Extensions
|
2015-04-01 16:04:19 +11:00
|
|
|
|
{
|
|
|
|
|
|
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>
|
2020-05-25 19:37:16 +10:00
|
|
|
|
/// <param name="linkGenerator"></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>
|
2020-04-03 11:03:06 +11:00
|
|
|
|
/// <param name="umbracoVersion"></param>
|
|
|
|
|
|
/// <param name="contentSettings"></param>
|
|
|
|
|
|
/// <param name="treeCollection"></param>
|
|
|
|
|
|
/// <param name="httpContextAccessor"></param>
|
|
|
|
|
|
/// <param name="hostingEnvironment"></param>
|
|
|
|
|
|
/// <param name="settings"></param>
|
|
|
|
|
|
/// <param name="securitySettings"></param>
|
|
|
|
|
|
/// <param name="runtimeMinifier"></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>
|
2020-05-25 19:37:16 +10:00
|
|
|
|
public static async Task<IHtmlContent> BareMinimumServerVariablesScriptAsync(this IHtmlHelper html, BackOfficeServerVariables backOfficeServerVariables)
|
2015-04-01 16:04:19 +11:00
|
|
|
|
{
|
2020-05-25 19:37:16 +10:00
|
|
|
|
var minVars = await backOfficeServerVariables.BareMinimumServerVariablesAsync();
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
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>
|
2020-05-27 18:27:49 +10:00
|
|
|
|
/// <param name="signInManager"></param>
|
2015-04-01 16:04:19 +11:00
|
|
|
|
/// <returns></returns>
|
2020-05-25 19:37:16 +10:00
|
|
|
|
public static async Task<IHtmlContent> AngularValueExternalLoginInfoScriptAsync(this IHtmlHelper html,
|
2020-05-27 18:27:49 +10:00
|
|
|
|
BackOfficeSignInManager signInManager,
|
2020-05-25 19:37:16 +10:00
|
|
|
|
IEnumerable<string> externalLoginErrors)
|
2015-04-01 16:04:19 +11:00
|
|
|
|
{
|
2020-05-27 18:27:49 +10:00
|
|
|
|
var providers = await signInManager.GetExternalAuthenticationSchemesAsync();
|
2020-05-25 19:37:16 +10:00
|
|
|
|
|
|
|
|
|
|
var loginProviders = providers
|
|
|
|
|
|
// TODO: We need to filter only back office enabled schemes.
|
|
|
|
|
|
// Before we used to have a property bag to check, now we don't so need to investigate the easiest/best
|
|
|
|
|
|
// way to do this. We have the type so maybe we check for a marker interface, but maybe there's another way,
|
|
|
|
|
|
// just need to investigate.
|
|
|
|
|
|
//.Where(p => p.Properties.ContainsKey("UmbracoBackOffice"))
|
2015-04-01 16:04:19 +11:00
|
|
|
|
.Select(p => new
|
|
|
|
|
|
{
|
2020-05-25 19:37:16 +10:00
|
|
|
|
authType = p.Name,
|
|
|
|
|
|
caption = p.DisplayName,
|
|
|
|
|
|
// TODO: See above, if we need this property bag in the vars then we'll need to figure something out
|
|
|
|
|
|
//properties = p.Properties
|
2015-04-01 16:04:19 +11:00
|
|
|
|
})
|
|
|
|
|
|
.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>
|
2020-05-25 19:37:16 +10:00
|
|
|
|
public static IHtmlContent AngularValueResetPasswordCodeInfoScript(this IHtmlHelper html, object val)
|
2016-04-13 13:51:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
|
sb.AppendLine(@"var errors = [];");
|
|
|
|
|
|
|
2020-08-31 13:39:29 +02:00
|
|
|
|
if (val is IEnumerable<string> errors)
|
2016-04-13 13:51:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var error in errors)
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.AppendFormat(@"errors.push(""{0}"");", error).AppendLine();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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: ");
|
2020-09-23 11:30:16 +02:00
|
|
|
|
sb.AppendLine(val?.ToString() ?? "null");
|
2016-04-13 13:51:12 +02:00
|
|
|
|
sb.AppendLine(@"});");
|
|
|
|
|
|
|
2019-08-19 14:36:56 +01:00
|
|
|
|
return html.Raw(sb.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-25 19:37:16 +10:00
|
|
|
|
public static async Task<IHtmlContent> AngularValueTinyMceAssetsAsync(this IHtmlHelper html, IRuntimeMinifier runtimeMinifier)
|
2019-08-19 14:36:56 +01:00
|
|
|
|
{
|
2020-05-25 19:37:16 +10:00
|
|
|
|
var files = await runtimeMinifier.GetAssetPathsAsync(BackOfficeWebAssets.UmbracoTinyMceJsBundleName);
|
2019-08-19 14:36:56 +01:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|