Merge pull request #5107 from umbraco/v8/feature/284-no-viewbag

Replace dynamic ViewBag with ViewData
This commit is contained in:
Shannon Deminick
2019-04-01 16:47:13 +11:00
committed by GitHub
7 changed files with 95 additions and 32 deletions

View File

@@ -7,7 +7,7 @@
<!doctype html>
<html lang="en">
<head>
<base href="@ViewBag.UmbracoBaseFolder/" />
<base href="@ViewData.GetUmbracoBaseFolder()/" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -66,8 +66,8 @@
var Umbraco = {};
Umbraco.Sys = {};
Umbraco.Sys.ServerVariables = {
"installApiBaseUrl": "@ViewBag.InstallApiBaseUrl",
"umbracoBaseUrl": "@ViewBag.UmbracoBaseFolder"
"installApiBaseUrl": "@ViewData.GetInstallApiBaseUrl()",
"umbracoBaseUrl": "@ViewData.GetUmbracoBaseFolder()"
};
</script>
<script src="lib/lazyload-js/lazyload.min.js"></script>

View File

@@ -51,7 +51,7 @@
@{
var externalLoginUrl = Url.Action("ExternalLogin", "BackOffice", new
{
area = ViewBag.UmbracoPath,
area = ViewData.GetUmbracoPath(),
//Custom redirect URL since we don't want to just redirect to the back office since this is for authing upgrades
redirectUrl = Url.Action("AuthorizeUpgrade", "BackOffice")
});
@@ -61,7 +61,7 @@
<script type="text/javascript">
document.angularReady = function (app) {
@Html.AngularValueExternalLoginInfoScript((IEnumerable<string>)ViewBag.ExternalSignInError)
@Html.AngularValueExternalLoginInfoScript(ViewData.GetExternalSignInError())
@Html.AngularValueResetPasswordCodeInfoScript(ViewData["PasswordResetCode"])
}

View File

@@ -112,12 +112,12 @@
on-login="hideLoginScreen()">
</umb-login>
@Html.BareMinimumServerVariablesScript(Url, Url.Action("ExternalLogin", "BackOffice", new { area = ViewBag.UmbracoPath }), Model.Features, Current.Configs.Global())
@Html.BareMinimumServerVariablesScript(Url, Url.Action("ExternalLogin", "BackOffice", new { area = ViewData.GetUmbracoPath() }), Model.Features, Current.Configs.Global())
<script>
document.angularReady = function(app) {
@Html.AngularValueExternalLoginInfoScript((IEnumerable<string>)ViewBag.ExternalSignInError)
@Html.AngularValueExternalLoginInfoScript(ViewData.GetExternalSignInError())
@Html.AngularValueResetPasswordCodeInfoScript(ViewData["PasswordResetCode"])
//required for the noscript trick
document.getElementById("mainwrapper").style.display = "inherit";

View File

@@ -45,10 +45,6 @@ namespace Umbraco.Web.Editors
private BackOfficeUserManager<BackOfficeIdentityUser> _userManager;
private BackOfficeSignInManager _signInManager;
private const string TokenExternalSignInError = "ExternalSignInError";
private const string TokenPasswordResetCode = "PasswordResetCode";
private static readonly string[] TempDataTokenNames = { TokenExternalSignInError, TokenPasswordResetCode };
public BackOfficeController(ManifestParser manifestParser, UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper)
: base(globalSettings, umbracoContextAccessor, services, appCaches, profilingLogger, umbracoHelper)
{
@@ -294,13 +290,13 @@ namespace Umbraco.Web.Editors
if (result)
{
//Add a flag and redirect for it to be displayed
TempData[TokenPasswordResetCode] = new ValidatePasswordResetCodeModel { UserId = userId, ResetCode = resetCode };
TempData[ViewDataExtensions.TokenPasswordResetCode] = new ValidatePasswordResetCodeModel { UserId = userId, ResetCode = resetCode };
return RedirectToLocal(Url.Action("Default", "BackOffice"));
}
}
//Add error and redirect for it to be displayed
TempData[TokenPasswordResetCode] = new[] { Services.TextService.Localize("login/resetCodeExpired") };
TempData[ViewDataExtensions.TokenPasswordResetCode] = new[] { Services.TextService.Localize("login/resetCodeExpired") };
return RedirectToLocal(Url.Action("Default", "BackOffice"));
}
@@ -314,7 +310,7 @@ namespace Umbraco.Web.Editors
if (loginInfo == null)
{
//Add error and redirect for it to be displayed
TempData[TokenExternalSignInError] = new[] { "An error occurred, could not get external login info" };
TempData[ViewDataExtensions.TokenExternalSignInError] = new[] { "An error occurred, could not get external login info" };
return RedirectToLocal(Url.Action("Default", "BackOffice"));
}
@@ -325,7 +321,7 @@ namespace Umbraco.Web.Editors
}
//Add errors and redirect for it to be displayed
TempData[TokenExternalSignInError] = result.Errors;
TempData[ViewDataExtensions.TokenExternalSignInError] = result.Errors;
return RedirectToLocal(Url.Action("Default", "BackOffice"));
}
@@ -341,17 +337,12 @@ namespace Umbraco.Web.Editors
if (defaultResponse == null) throw new ArgumentNullException("defaultResponse");
if (externalSignInResponse == null) throw new ArgumentNullException("externalSignInResponse");
ViewBag.UmbracoPath = GlobalSettings.GetUmbracoMvcArea();
ViewData.SetUmbracoPath(GlobalSettings.GetUmbracoMvcArea());
//check if there is the TempData with the any token name specified, if so, assign to view bag and render the view
foreach (var tempDataTokenName in TempDataTokenNames)
{
if (TempData[tempDataTokenName] != null)
{
ViewData[tempDataTokenName] = TempData[tempDataTokenName];
return defaultResponse();
}
}
if (ViewData.FromTempData(TempData, ViewDataExtensions.TokenExternalSignInError) ||
ViewData.FromTempData(TempData, ViewDataExtensions.TokenPasswordResetCode))
return defaultResponse();
//First check if there's external login info, if there's not proceed as normal
var loginInfo = await OwinContext.Authentication.GetExternalLoginInfoAsync(
@@ -416,7 +407,7 @@ namespace Umbraco.Web.Editors
{
if (await AutoLinkAndSignInExternalAccount(loginInfo, autoLinkOptions) == false)
{
ViewData[TokenExternalSignInError] = new[] { "The requested provider (" + loginInfo.Login.LoginProvider + ") has not been linked to an account" };
ViewData.SetExternalSignInError(new[] { "The requested provider (" + loginInfo.Login.LoginProvider + ") has not been linked to an account" });
}
//Remove the cookie otherwise this message will keep appearing
@@ -440,7 +431,7 @@ namespace Umbraco.Web.Editors
//we are allowing auto-linking/creating of local accounts
if (loginInfo.Email.IsNullOrWhiteSpace())
{
ViewData[TokenExternalSignInError] = new[] { "The requested provider (" + loginInfo.Login.LoginProvider + ") has not provided an email address, the account cannot be linked." };
ViewData.SetExternalSignInError(new[] { "The requested provider (" + loginInfo.Login.LoginProvider + ") has not provided an email address, the account cannot be linked." });
}
else
{
@@ -448,7 +439,7 @@ namespace Umbraco.Web.Editors
var foundByEmail = Services.UserService.GetByEmail(loginInfo.Email);
if (foundByEmail != null)
{
ViewData[TokenExternalSignInError] = new[] { "A user with this email address already exists locally. You will need to login locally to Umbraco and link this external provider: " + loginInfo.Login.LoginProvider };
ViewData.SetExternalSignInError(new[] { "A user with this email address already exists locally. You will need to login locally to Umbraco and link this external provider: " + loginInfo.Login.LoginProvider });
}
else
{
@@ -477,21 +468,21 @@ namespace Umbraco.Web.Editors
if (userCreationResult.Succeeded == false)
{
ViewData[TokenExternalSignInError] = userCreationResult.Errors;
ViewData.SetExternalSignInError(userCreationResult.Errors);
}
else
{
var linkResult = await UserManager.AddLoginAsync(autoLinkUser.Id, loginInfo.Login);
if (linkResult.Succeeded == false)
{
ViewData[TokenExternalSignInError] = linkResult.Errors;
ViewData.SetExternalSignInError(linkResult.Errors);
//If this fails, we should really delete the user since it will be in an inconsistent state!
var deleteResult = await UserManager.DeleteAsync(autoLinkUser);
if (deleteResult.Succeeded == false)
{
//DOH! ... this isn't good, combine all errors to be shown
ViewData[TokenExternalSignInError] = linkResult.Errors.Concat(deleteResult.Errors);
ViewData.SetExternalSignInError(linkResult.Errors.Concat(deleteResult.Errors));
}
}
else

View File

@@ -60,10 +60,10 @@ namespace Umbraco.Web.Install.Controllers
}
// gen the install base url
ViewBag.InstallApiBaseUrl = Url.GetUmbracoApiService("GetSetup", "InstallApi", "UmbracoInstall").TrimEnd("GetSetup");
ViewData.SetInstallApiBaseUrl(Url.GetUmbracoApiService("GetSetup", "InstallApi", "UmbracoInstall").TrimEnd("GetSetup"));
// get the base umbraco folder
ViewBag.UmbracoBaseFolder = IOHelper.ResolveUrl(SystemDirectories.Umbraco);
ViewData.SetUmbracoBaseFolder(IOHelper.ResolveUrl(SystemDirectories.Umbraco));
_installHelper.InstallStatus(false, "");

View File

@@ -216,6 +216,7 @@
<Compile Include="Models\TemplateQuery\OperatorFactory.cs" />
<Compile Include="UmbracoContextFactory.cs" />
<Compile Include="UmbracoContextReference.cs" />
<Compile Include="ViewDataExtensions.cs" />
<Compile Include="WebApi\Filters\AdminUsersAuthorizeAttribute.cs" />
<Compile Include="WebApi\Filters\OnlyLocalRequestsAttribute.cs" />
<Compile Include="PropertyEditors\MultiUrlPickerConfiguration.cs" />

View File

@@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.Web.Mvc;
namespace Umbraco.Web
{
public static class ViewDataExtensions
{
public const string TokenUmbracoPath = "UmbracoPath";
public const string TokenInstallApiBaseUrl = "InstallApiBaseUrl";
public const string TokenUmbracoBaseFolder = "UmbracoBaseFolder";
public const string TokenExternalSignInError = "ExternalSignInError";
public const string TokenPasswordResetCode = "PasswordResetCode";
public static bool FromTempData(this ViewDataDictionary viewData, TempDataDictionary tempData, string token)
{
if (tempData[token] == null) return false;
viewData[token] = tempData[token];
return true;
}
public static string GetUmbracoPath(this ViewDataDictionary viewData)
{
return (string)viewData[TokenUmbracoPath];
}
public static void SetUmbracoPath(this ViewDataDictionary viewData, string value)
{
viewData[TokenUmbracoPath] = value;
}
public static string GetInstallApiBaseUrl(this ViewDataDictionary viewData)
{
return (string)viewData[TokenInstallApiBaseUrl];
}
public static void SetInstallApiBaseUrl(this ViewDataDictionary viewData, string value)
{
viewData[TokenInstallApiBaseUrl] = value;
}
public static string GetUmbracoBaseFolder(this ViewDataDictionary viewData)
{
return (string)viewData[TokenUmbracoBaseFolder];
}
public static void SetUmbracoBaseFolder(this ViewDataDictionary viewData, string value)
{
viewData[TokenUmbracoBaseFolder] = value;
}
public static IEnumerable<string> GetExternalSignInError(this ViewDataDictionary viewData)
{
return (IEnumerable<string>)viewData[TokenExternalSignInError];
}
public static void SetExternalSignInError(this ViewDataDictionary viewData, IEnumerable<string> value)
{
viewData[TokenExternalSignInError] = value;
}
public static string GetPasswordResetCode(this ViewDataDictionary viewData)
{
return (string)viewData[TokenPasswordResetCode];
}
public static void SetPasswordResetCode(this ViewDataDictionary viewData, string value)
{
viewData[TokenPasswordResetCode] = value;
}
}
}