From aa9ff2e670287523da7709a9269cf38463de1dc2 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 3 Mar 2021 16:31:17 +0000 Subject: [PATCH] The event fires and hits the Handle in the notification/event handler But it implodes when I inject/add UserManager to this --- .../UmbracoBuilderExtensions.cs | 3 +- .../UmbracoBuilderExtensions.cs | 11 ++++-- ...CreateUnattendedUserNotificationHandler.cs | 36 +++++++++---------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Umbraco.Web.BackOffice/DependencyInjection/UmbracoBuilderExtensions.cs b/src/Umbraco.Web.BackOffice/DependencyInjection/UmbracoBuilderExtensions.cs index e28c8e4196..e205336678 100644 --- a/src/Umbraco.Web.BackOffice/DependencyInjection/UmbracoBuilderExtensions.cs +++ b/src/Umbraco.Web.BackOffice/DependencyInjection/UmbracoBuilderExtensions.cs @@ -47,7 +47,8 @@ namespace Umbraco.Extensions .AddPreviewSupport() .AddHostedServices() .AddDistributedCache() - .AddModelsBuilderDashboard(); + .AddModelsBuilderDashboard() + .AddUnattedInstallCreateUser(); // Put last to test that everything else injected/setup & happy /// /// Adds Umbraco back office authentication requirements diff --git a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs index c782b46d8c..7a92eebd3b 100644 --- a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs +++ b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs @@ -271,9 +271,7 @@ namespace Umbraco.Extensions builder.Services.AddUnique(); - // 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(); + @@ -298,6 +296,13 @@ namespace Umbraco.Extensions return builder; } + public static IUmbracoBuilder AddUnattedInstallCreateUser(this IUmbracoBuilder builder) + { + builder.AddNotificationHandler(); + //builder.AddNotificationHandler(); + return builder; + } + // TODO: Does this need to exist and/or be public? public static IUmbracoBuilder AddWebServer(this IUmbracoBuilder builder) { diff --git a/src/Umbraco.Web.Common/Install/CreateUnattendedUserNotificationHandler.cs b/src/Umbraco.Web.Common/Install/CreateUnattendedUserNotificationHandler.cs index 6886368662..82ffef158a 100644 --- a/src/Umbraco.Web.Common/Install/CreateUnattendedUserNotificationHandler.cs +++ b/src/Umbraco.Web.Common/Install/CreateUnattendedUserNotificationHandler.cs @@ -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 + public class CreateUnattendedUserNotificationHandler : INotificationHandler { private readonly GlobalSettings _globalSettings; private readonly IUserService _userService; - private readonly IBackOfficeUserManager _userManager; - public CreateUnattendedUserNotificationHandler(IOptions globalSettings, IUserService userService, IBackOfficeUserManager userManager) + public CreateUnattendedUserNotificationHandler(IOptions globalSettings, IUserService userService) { _globalSettings = globalSettings.Value; _userService = userService; - _userManager = userManager; } /// Listening for when the UnattendedInstallNotification fired after a sucessfulk /// /// - 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())); } }