Use current request for emails (#11775)

* Use current request for emails

* Fix tests
This commit is contained in:
Mole
2021-12-21 07:23:08 +01:00
committed by Bjarke Berg
parent 5123799e32
commit 1b98f8985e
5 changed files with 69 additions and 18 deletions

View File

@@ -102,5 +102,22 @@ namespace Umbraco.Core.Sync
return url.TrimEnd(Constants.CharArrays.ForwardSlash);
}
/// <summary>
/// Will get the application URL from configuration, if none is specified will fall back to URL from request.
/// </summary>
/// <param name="request"></param>
/// <param name="umbracoSettingsSection"></param>
/// <param name="globalSettings"></param>
/// <returns></returns>
public static Uri GetApplicationUriUncached(
HttpRequestBase request,
IUmbracoSettingsSection umbracoSettingsSection)
{
var settingUrl = umbracoSettingsSection.WebRouting.UmbracoApplicationUrl;
return string.IsNullOrEmpty(settingUrl)
? new Uri(request.Url, IOHelper.ResolveUrl(SystemDirectories.Umbraco))
: new Uri(settingUrl);
}
}
}

View File

@@ -16,6 +16,7 @@ using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
@@ -82,7 +83,8 @@ namespace Umbraco.Tests.Web.Controllers
Factory.GetInstance<AppCaches>(),
Factory.GetInstance<IProfilingLogger>(),
Factory.GetInstance<IRuntimeState>(),
helper);
helper,
Factory.GetInstance<IUmbracoSettingsSection>());
return usersController;
}

View File

@@ -14,6 +14,7 @@ using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Membership;
@@ -84,7 +85,8 @@ namespace Umbraco.Tests.Web.Controllers
Factory.GetInstance<AppCaches>(),
Factory.GetInstance<IProfilingLogger>(),
Factory.GetInstance<IRuntimeState>(),
helper);
helper,
Factory.GetInstance<IUmbracoSettingsSection>());
return usersController;
}
@@ -148,7 +150,8 @@ namespace Umbraco.Tests.Web.Controllers
Factory.GetInstance<AppCaches>(),
Factory.GetInstance<IProfilingLogger>(),
Factory.GetInstance<IRuntimeState>(),
helper);
helper,
Factory.GetInstance<IUmbracoSettingsSection>());
return usersController;
}
@@ -183,7 +186,8 @@ namespace Umbraco.Tests.Web.Controllers
Factory.GetInstance<AppCaches>(),
Factory.GetInstance<IProfilingLogger>(),
Factory.GetInstance<IRuntimeState>(),
helper);
helper,
Factory.GetInstance<IUmbracoSettingsSection>());
return usersController;
}
@@ -253,7 +257,8 @@ namespace Umbraco.Tests.Web.Controllers
Factory.GetInstance<AppCaches>(),
Factory.GetInstance<IProfilingLogger>(),
Factory.GetInstance<IRuntimeState>(),
helper);
helper,
Factory.GetInstance<IUmbracoSettingsSection>());
return usersController;
}

View File

@@ -27,6 +27,8 @@ using Umbraco.Web.Composing;
using IUser = Umbraco.Core.Models.Membership.IUser;
using Umbraco.Web.Editors.Filters;
using Microsoft.Owin.Security;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Sync;
namespace Umbraco.Web.Editors
{
@@ -40,12 +42,23 @@ namespace Umbraco.Web.Editors
[DisableBrowserCache]
public class AuthenticationController : UmbracoApiController
{
private readonly IUmbracoSettingsSection _umbracoSettingsSection;
private BackOfficeUserManager<BackOfficeIdentityUser> _userManager;
private BackOfficeSignInManager _signInManager;
public AuthenticationController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper)
public AuthenticationController(
IGlobalSettings globalSettings,
IUmbracoContextAccessor umbracoContextAccessor,
ISqlContext sqlContext,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger logger,
IRuntimeState runtimeState,
UmbracoHelper umbracoHelper,
IUmbracoSettingsSection umbracoSettingsSection)
: base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper, Current.Mapper)
{
_umbracoSettingsSection = umbracoSettingsSection;
}
protected BackOfficeUserManager<BackOfficeIdentityUser> UserManager => _userManager
@@ -552,8 +565,8 @@ namespace Umbraco.Web.Editors
r = code
});
// Construct full URL using configured application URL (which will fall back to request)
var applicationUri = Current.RuntimeState.ApplicationUrl;
// Construct full URL using configured application URL (which will fall back to current request)
var applicationUri = ApplicationUrlHelper.GetApplicationUriUncached(http.Request, _umbracoSettingsSection);
var callbackUri = new Uri(applicationUri, action);
return callbackUri.ToString();
}

View File

@@ -17,6 +17,7 @@ using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
@@ -27,6 +28,7 @@ using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.Security;
using Umbraco.Core.Services;
using Umbraco.Core.Sync;
using Umbraco.Web.Editors.Filters;
using Umbraco.Web.Models;
using Umbraco.Web.Models.ContentEditing;
@@ -46,9 +48,21 @@ namespace Umbraco.Web.Editors
[IsCurrentUserModelFilter]
public class UsersController : UmbracoAuthorizedJsonController
{
public UsersController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper)
private readonly IUmbracoSettingsSection _umbracoSettingsSection;
public UsersController(
IGlobalSettings globalSettings,
IUmbracoContextAccessor umbracoContextAccessor,
ISqlContext sqlContext,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger logger,
IRuntimeState runtimeState,
UmbracoHelper umbracoHelper,
IUmbracoSettingsSection umbracoSettingsSection)
: base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper)
{
_umbracoSettingsSection = umbracoSettingsSection;
}
/// <summary>
@@ -390,7 +404,7 @@ namespace Umbraco.Web.Editors
user = CheckUniqueEmail(userSave.Email, u => u.LastLoginDate != default || u.EmailConfirmedDate.HasValue);
var userMgr = TryGetOwinContext().Result.GetBackOfficeUserManager();
if (!EmailSender.CanSendRequiredEmail && !userMgr.HasSendingUserInviteEventHandler)
{
throw new HttpResponseException(
@@ -462,12 +476,12 @@ namespace Umbraco.Web.Editors
Email = userSave.Email,
Username = userSave.Username
};
}
}
}
else
{
//send the email
await SendUserInviteEmailAsync(display, Security.CurrentUser.Name, Security.CurrentUser.Email, user, userSave.Message);
await SendUserInviteEmailAsync(display, Security.CurrentUser.Name, Security.CurrentUser.Email, user, userSave.Message);
}
display.AddSuccessNotification(Services.TextService.Localize("speechBubbles", "resendInviteHeader"), Services.TextService.Localize("speechBubbles", "resendInviteSuccess", new[] { user.Name }));
@@ -525,9 +539,9 @@ namespace Umbraco.Web.Editors
invite = inviteToken
});
// Construct full URL using configured application URL (which will fall back to request)
var applicationUri = RuntimeState.ApplicationUrl;
var inviteUri = new Uri(applicationUri, action);
// Construct full URL will use the value in settings if specified, otherwise will use the current request URL
var requestUrl = ApplicationUrlHelper.GetApplicationUriUncached(http.Request, _umbracoSettingsSection);
var inviteUri = new Uri(requestUrl, action);
var emailSubject = Services.TextService.Localize("user", "inviteEmailCopySubject",
//Ensure the culture of the found user is used for the email!
@@ -622,7 +636,7 @@ namespace Umbraco.Web.Editors
if (Current.Configs.Settings().Security.UsernameIsEmail && found.Username == found.Email && userSave.Username != userSave.Email)
{
userSave.Username = userSave.Email;
}
}
if (hasErrors)
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
@@ -647,13 +661,13 @@ namespace Umbraco.Web.Editors
}
/// <summary>
///
///
/// </summary>
/// <param name="changingPasswordModel"></param>
/// <returns></returns>
public async Task<ModelWithNotifications<string>> PostChangePassword(ChangingPasswordModel changingPasswordModel)
{
changingPasswordModel = changingPasswordModel ?? throw new ArgumentNullException(nameof(changingPasswordModel));
changingPasswordModel = changingPasswordModel ?? throw new ArgumentNullException(nameof(changingPasswordModel));
if (ModelState.IsValid == false)
{