The event fires and hits the Handle in the notification/event handler

But it implodes when I inject/add UserManager to this
This commit is contained in:
Warren Buckley
2021-03-03 16:31:17 +00:00
parent b0f078fa4b
commit aa9ff2e670
3 changed files with 27 additions and 23 deletions

View File

@@ -47,7 +47,8 @@ namespace Umbraco.Extensions
.AddPreviewSupport()
.AddHostedServices()
.AddDistributedCache()
.AddModelsBuilderDashboard();
.AddModelsBuilderDashboard()
.AddUnattedInstallCreateUser(); // Put last to test that everything else injected/setup & happy
/// <summary>
/// Adds Umbraco back office authentication requirements

View File

@@ -271,9 +271,7 @@ namespace Umbraco.Extensions
builder.Services.AddUnique<InstallAreaRoutes>();
// This is a lovely file with no real groupings it seems
// Put close to install & upgrade stuff for the notification/event listener for
builder.AddNotificationHandler<UmbracoApplicationStarting, CreateUnattendedUserNotificationHandler>();
@@ -298,6 +296,13 @@ namespace Umbraco.Extensions
return builder;
}
public static IUmbracoBuilder AddUnattedInstallCreateUser(this IUmbracoBuilder builder)
{
builder.AddNotificationHandler<UnattendedInstallNotification, CreateUnattendedUserNotificationHandler>();
//builder.AddNotificationHandler<UmbracoApplicationStarting, CreateUnattendedUserNotificationHandler>();
return builder;
}
// TODO: Does this need to exist and/or be public?
public static IUmbracoBuilder AddWebServer(this IUmbracoBuilder builder)
{

View File

@@ -2,30 +2,27 @@ using System;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Core.Events;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common.Install
{
public class CreateUnattendedUserNotificationHandler : INotificationHandler<UmbracoApplicationStarting>
public class CreateUnattendedUserNotificationHandler : INotificationHandler<UnattendedInstallNotification>
{
private readonly GlobalSettings _globalSettings;
private readonly IUserService _userService;
private readonly IBackOfficeUserManager _userManager;
public CreateUnattendedUserNotificationHandler(IOptions<GlobalSettings> globalSettings, IUserService userService, IBackOfficeUserManager userManager)
public CreateUnattendedUserNotificationHandler(IOptions<GlobalSettings> globalSettings, IUserService userService)
{
_globalSettings = globalSettings.Value;
_userService = userService;
_userManager = userManager;
}
/// Listening for when the UnattendedInstallNotification fired after a sucessfulk
/// </summary>
/// <param name="notification"></param>
public async void Handle(UmbracoApplicationStarting notification)
public async void Handle(UnattendedInstallNotification notification)
{
// Ensure we have the setting enabled (Sanity check)
// In theory this should always be true as the event only fired when a sucessfull
@@ -62,22 +59,23 @@ namespace Umbraco.Cms.Web.Common.Install
// Change Password for the default user we ship out of the box
// Uses same approach as NewInstall Step
var membershipUser = await _userManager.FindByIdAsync(Core.Constants.Security.SuperUserId.ToString());
if (membershipUser == null)
{
throw new InvalidOperationException($"No user found in membership provider with id of {Core.Constants.Security.SuperUserId}.");
}
//To change the password here we actually need to reset it since we don't have an old one to use to change
var resetToken = await _userManager.GeneratePasswordResetTokenAsync(membershipUser);
if (string.IsNullOrWhiteSpace(resetToken))
throw new InvalidOperationException("Could not reset password: unable to generate internal reset token");
// TODO: usermanager why you no inject?!
var resetResult = await _userManager.ChangePasswordWithResetAsync(membershipUser.Id, resetToken, unattendedPassword.Trim());
if (!resetResult.Succeeded)
throw new InvalidOperationException("Could not reset password: " + string.Join(", ", resetResult.Errors.ToErrorMessage()));
//var membershipUser = await _userManager.FindByIdAsync(Core.Constants.Security.SuperUserId.ToString());
//if (membershipUser == null)
//{
// throw new InvalidOperationException($"No user found in membership provider with id of {Core.Constants.Security.SuperUserId}.");
//}
throw new NotImplementedException();
////To change the password here we actually need to reset it since we don't have an old one to use to change
//var resetToken = await _userManager.GeneratePasswordResetTokenAsync(membershipUser);
//if (string.IsNullOrWhiteSpace(resetToken))
// throw new InvalidOperationException("Could not reset password: unable to generate internal reset token");
//var resetResult = await _userManager.ChangePasswordWithResetAsync(membershipUser.Id, resetToken, unattendedPassword.Trim());
//if (!resetResult.Succeeded)
// throw new InvalidOperationException("Could not reset password: " + string.Join(", ", resetResult.Errors.ToErrorMessage()));
}
}