/
HtmlHelperBackOfficeExtensions.cs
137 lines (123 loc) · 5.68 KB
/
HtmlHelperBackOfficeExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using ClientDependency.Core.Config;
using Microsoft.Owin.Security;
using Newtonsoft.Json;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Web.Editors;
using Umbraco.Web.Models;
namespace Umbraco.Web
{
using Umbraco.Core.Configuration;
/// <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>
/// <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>
/// <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>
public static IHtmlString BareMinimumServerVariablesScript(this HtmlHelper html, UrlHelper uri, string externalLoginsUrl)
{
var version = UmbracoVersion.GetSemanticVersion().ToSemanticString();
var str = @"<script type=""text/javascript"">
var Umbraco = {};
Umbraco.Sys = {};
Umbraco.Sys.ServerVariables = {
""umbracoUrls"": {
""authenticationApiBaseUrl"": """ + uri.GetUmbracoApiServiceBaseUrl<AuthenticationController>(controller => controller.PostLogin(null)) + @""",
""serverVarsJs"": """ + uri.GetUrlWithCacheBust("ServerVariables", "BackOffice") + @""",
""externalLoginsUrl"": """ + externalLoginsUrl + @"""
},
""umbracoSettings"": {
""allowPasswordReset"": " + (UmbracoConfig.For.UmbracoSettings().Security.AllowPasswordReset ? "true" : "false") + @",
""loginBackgroundImage"": """ + UmbracoConfig.For.UmbracoSettings().Content.LoginBackgroundImage + @"""
},
""application"": {
""applicationPath"": """ + html.ViewContext.HttpContext.Request.ApplicationPath + @""",
""version"": """ + version + @""",
""cdf"": """ + ClientDependencySettings.Instance.Version + @"""
},
""isDebuggingEnabled"" : " + html.ViewContext.HttpContext.IsDebuggingEnabled.ToString().ToLowerInvariant() + @"
};
</script>";
return html.Raw(str);
}
/// <summary>
/// Used to render the script that will pass in the angular "externalLoginInfo" service/value on page load
/// </summary>
/// <param name="html"></param>
/// <param name="externalLoginErrors"></param>
/// <returns></returns>
public static IHtmlString AngularValueExternalLoginInfoScript(this HtmlHelper html, IEnumerable<string> 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());
}
/// <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"", {");
sb.AppendLine(@"errors: errors,");
sb.Append(@"resetCodeModel: ");
sb.AppendLine(JsonConvert.SerializeObject(resetCodeModel));
sb.AppendLine(@"});");
return html.Raw(sb.ToString());
}
}
}