Files
Umbraco-CMS/src/Umbraco.Infrastructure/Installer/Steps/CreateUserStep.cs

177 lines
7.5 KiB
C#
Raw Normal View History

Implement new backoffice installer (#12790) * Add new BackOfficeApi project * Add swagger * Add and route new install controller * Add new install steps * Add Setup endpoint * Add missing RequiresExecution methods * Fix nullability of databasemodel * Move user information to separate model * Remove ping method * Add view models install data * Move mapping folder * Move ViewModels * Add settings endpoint * Remove unused binderprovider * Postfix RequiresExecution with async * Update NewDatabaseUpgradeStep to not depend on install step * Add installstep collection * Move registration into backoffice project * Add InstallService * Use service in controller * Add upgrade to install service and use in controller * Correctly check is database is configured * Reorganize * Reorganize into new core and infrastructure * Rename steps * Rename BackofficeApi to MangementApi * Make install step an interface instead of abstract class * Rename InstallStep to create CreateUserStep * Move restart runtime and sign in user into install steps * Move install service into new core project * Map controllers in composer * Restrict access to installcontroller based on runtime level * Use FireAndForget when logging install * Use actionresult instead of iactionresult * Set new projects as not packable * Link to backoffice in 201 response when installed * Register installations * Add custom backoffice routing template token * Move umbraco path trimming out of application convention * Make it easier to route to backoffice api * Make swagger version aware and move behind backoffice path * Obsolete old install classes * Move maps into single file This is all mappint to/from viewmodels in some manner * Remove usage of InstallSetupResult * Move new projects to the src folder * Remove InstallationType from IInstallStep This upgrade steps should implement their own IUpgradeStep interface * Remove upgrade from service and controller This should be its own service and controller * Add xml docs * Remove internals visible to * Disable package validation for new projects Quite the gotcha here, if the projects are brand new, there is no nuget packages to compare with, this causes the build to fail. * Add ValidateDatabase endpoint * Remove project references to new backoffice We don't actually want to depend on this yet, it's just needed for testing/development * Obsolete installationtype * Add DatabaseSettingsFactory tests * Add InstallServiceTests * Fix InstallServiceTests * Test RequireRuntimeLevelAttribute * Implement new backoffice upgrader (#12818) * Add UpgradeSettingsModel and viewmodel * Add upgrade/settings endpoint * Implement upgrade steps * Add upgrade step collection * Add UpgradeService * Add authorize endpoint to UpgradeController * Fix interface * Add upgrade service tests * Remove runtime check in databaseinstallstep * Move RequireRuntimeLevel to controller * Add a readme to the new backoffice part * BackOffice not Backoffice * Add conditional project references * Fixes based on review * Fix up * Move running of steps into its own method in UpgradeService * Make services transient * More fixup * Log exceptions when running steps
2022-08-29 09:50:48 +02:00
using System.Collections.Specialized;
using System.Data.Common;
using System.Text;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Install.Models;
using Umbraco.Cms.Core.Installer;
using Umbraco.Cms.Core.Models.Installer;
Implement new backoffice installer (#12790) * Add new BackOfficeApi project * Add swagger * Add and route new install controller * Add new install steps * Add Setup endpoint * Add missing RequiresExecution methods * Fix nullability of databasemodel * Move user information to separate model * Remove ping method * Add view models install data * Move mapping folder * Move ViewModels * Add settings endpoint * Remove unused binderprovider * Postfix RequiresExecution with async * Update NewDatabaseUpgradeStep to not depend on install step * Add installstep collection * Move registration into backoffice project * Add InstallService * Use service in controller * Add upgrade to install service and use in controller * Correctly check is database is configured * Reorganize * Reorganize into new core and infrastructure * Rename steps * Rename BackofficeApi to MangementApi * Make install step an interface instead of abstract class * Rename InstallStep to create CreateUserStep * Move restart runtime and sign in user into install steps * Move install service into new core project * Map controllers in composer * Restrict access to installcontroller based on runtime level * Use FireAndForget when logging install * Use actionresult instead of iactionresult * Set new projects as not packable * Link to backoffice in 201 response when installed * Register installations * Add custom backoffice routing template token * Move umbraco path trimming out of application convention * Make it easier to route to backoffice api * Make swagger version aware and move behind backoffice path * Obsolete old install classes * Move maps into single file This is all mappint to/from viewmodels in some manner * Remove usage of InstallSetupResult * Move new projects to the src folder * Remove InstallationType from IInstallStep This upgrade steps should implement their own IUpgradeStep interface * Remove upgrade from service and controller This should be its own service and controller * Add xml docs * Remove internals visible to * Disable package validation for new projects Quite the gotcha here, if the projects are brand new, there is no nuget packages to compare with, this causes the build to fail. * Add ValidateDatabase endpoint * Remove project references to new backoffice We don't actually want to depend on this yet, it's just needed for testing/development * Obsolete installationtype * Add DatabaseSettingsFactory tests * Add InstallServiceTests * Fix InstallServiceTests * Test RequireRuntimeLevelAttribute * Implement new backoffice upgrader (#12818) * Add UpgradeSettingsModel and viewmodel * Add upgrade/settings endpoint * Implement upgrade steps * Add upgrade step collection * Add UpgradeService * Add authorize endpoint to UpgradeController * Fix interface * Add upgrade service tests * Remove runtime check in databaseinstallstep * Move RequireRuntimeLevel to controller * Add a readme to the new backoffice part * BackOffice not Backoffice * Add conditional project references * Fixes based on review * Fix up * Move running of steps into its own method in UpgradeService * Make services transient * More fixup * Log exceptions when running steps
2022-08-29 09:50:48 +02:00
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Migrations.Install;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Extensions;
using Constants = Umbraco.Cms.Core.Constants;
using HttpResponseMessage = System.Net.Http.HttpResponseMessage;
namespace Umbraco.Cms.Infrastructure.Installer.Steps;
Implement new backoffice installer (#12790) * Add new BackOfficeApi project * Add swagger * Add and route new install controller * Add new install steps * Add Setup endpoint * Add missing RequiresExecution methods * Fix nullability of databasemodel * Move user information to separate model * Remove ping method * Add view models install data * Move mapping folder * Move ViewModels * Add settings endpoint * Remove unused binderprovider * Postfix RequiresExecution with async * Update NewDatabaseUpgradeStep to not depend on install step * Add installstep collection * Move registration into backoffice project * Add InstallService * Use service in controller * Add upgrade to install service and use in controller * Correctly check is database is configured * Reorganize * Reorganize into new core and infrastructure * Rename steps * Rename BackofficeApi to MangementApi * Make install step an interface instead of abstract class * Rename InstallStep to create CreateUserStep * Move restart runtime and sign in user into install steps * Move install service into new core project * Map controllers in composer * Restrict access to installcontroller based on runtime level * Use FireAndForget when logging install * Use actionresult instead of iactionresult * Set new projects as not packable * Link to backoffice in 201 response when installed * Register installations * Add custom backoffice routing template token * Move umbraco path trimming out of application convention * Make it easier to route to backoffice api * Make swagger version aware and move behind backoffice path * Obsolete old install classes * Move maps into single file This is all mappint to/from viewmodels in some manner * Remove usage of InstallSetupResult * Move new projects to the src folder * Remove InstallationType from IInstallStep This upgrade steps should implement their own IUpgradeStep interface * Remove upgrade from service and controller This should be its own service and controller * Add xml docs * Remove internals visible to * Disable package validation for new projects Quite the gotcha here, if the projects are brand new, there is no nuget packages to compare with, this causes the build to fail. * Add ValidateDatabase endpoint * Remove project references to new backoffice We don't actually want to depend on this yet, it's just needed for testing/development * Obsolete installationtype * Add DatabaseSettingsFactory tests * Add InstallServiceTests * Fix InstallServiceTests * Test RequireRuntimeLevelAttribute * Implement new backoffice upgrader (#12818) * Add UpgradeSettingsModel and viewmodel * Add upgrade/settings endpoint * Implement upgrade steps * Add upgrade step collection * Add UpgradeService * Add authorize endpoint to UpgradeController * Fix interface * Add upgrade service tests * Remove runtime check in databaseinstallstep * Move RequireRuntimeLevel to controller * Add a readme to the new backoffice part * BackOffice not Backoffice * Add conditional project references * Fixes based on review * Fix up * Move running of steps into its own method in UpgradeService * Make services transient * More fixup * Log exceptions when running steps
2022-08-29 09:50:48 +02:00
public class CreateUserStep : IInstallStep
{
private readonly IUserService _userService;
private readonly DatabaseBuilder _databaseBuilder;
private readonly IHttpClientFactory _httpClientFactory;
private readonly SecuritySettings _securitySettings;
private readonly IOptionsMonitor<ConnectionStrings> _connectionStrings;
private readonly ICookieManager _cookieManager;
private readonly IBackOfficeUserManager _userManager;
private readonly IDbProviderFactoryCreator _dbProviderFactoryCreator;
private readonly IMetricsConsentService _metricsConsentService;
public CreateUserStep(
IUserService userService,
DatabaseBuilder databaseBuilder,
IHttpClientFactory httpClientFactory,
IOptions<SecuritySettings> securitySettings,
IOptionsMonitor<ConnectionStrings> connectionStrings,
ICookieManager cookieManager,
IBackOfficeUserManager userManager,
IDbProviderFactoryCreator dbProviderFactoryCreator,
IMetricsConsentService metricsConsentService)
{
_userService = userService ?? throw new ArgumentNullException(nameof(userService));
_databaseBuilder = databaseBuilder ?? throw new ArgumentNullException(nameof(databaseBuilder));
_httpClientFactory = httpClientFactory;
_securitySettings = securitySettings.Value ?? throw new ArgumentNullException(nameof(securitySettings));
_connectionStrings = connectionStrings;
_cookieManager = cookieManager;
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
_dbProviderFactoryCreator = dbProviderFactoryCreator ?? throw new ArgumentNullException(nameof(dbProviderFactoryCreator));
_metricsConsentService = metricsConsentService;
}
public async Task ExecuteAsync(InstallData model)
{
IUser? admin = _userService.GetUserById(Constants.Security.SuperUserId);
if (admin == null)
{
throw new InvalidOperationException("Could not find the super user!");
}
UserInstallData user = model.User;
admin.Email = user.Email.Trim();
admin.Name = user.Name.Trim();
admin.Username = user.Email.Trim();
_userService.Save(admin);
BackOfficeIdentityUser? membershipUser = await _userManager.FindByIdAsync(Constants.Security.SuperUserIdAsString);
if (membershipUser == null)
{
throw new InvalidOperationException(
$"No user found in membership provider with id of {Constants.Security.SuperUserIdAsString}.");
}
//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");
}
IdentityResult resetResult = await _userManager.ChangePasswordWithResetAsync(membershipUser.Id, resetToken, user.Password.Trim());
if (!resetResult.Succeeded)
{
throw new InvalidOperationException("Could not reset password: " + string.Join(", ", resetResult.Errors.ToErrorMessage()));
}
_metricsConsentService.SetConsentLevel(model.TelemetryLevel);
if (model.User.SubscribeToNewsletter)
{
var values = new NameValueCollection { { "name", admin.Name }, { "email", admin.Email } };
var content = new StringContent(JsonConvert.SerializeObject(values), Encoding.UTF8, "application/json");
HttpClient httpClient = _httpClientFactory.CreateClient();
try
{
HttpResponseMessage response = httpClient.PostAsync("https://shop.umbraco.com/base/Ecom/SubmitEmail/installer.aspx", content).Result;
}
catch { /* fail in silence */ }
}
}
/// <inheritdoc/>
public Task<bool> RequiresExecutionAsync(InstallData model)
{
InstallState installState = GetInstallState();
if (installState.HasFlag(InstallState.Unknown))
{
// In this one case when it's a brand new install and nothing has been configured, make sure the
// back office cookie is cleared so there's no old cookies lying around causing problems
_cookieManager.ExpireCookie(_securitySettings.AuthCookieName);
}
var shouldRun = installState.HasFlag(InstallState.Unknown) || !installState.HasFlag(InstallState.HasNonDefaultUser);
return Task.FromResult(shouldRun);
}
private InstallState GetInstallState()
{
InstallState installState = InstallState.Unknown;
if (_databaseBuilder.IsDatabaseConfigured)
{
installState = (installState | InstallState.HasConnectionString) & ~InstallState.Unknown;
}
ConnectionStrings? umbracoConnectionString = _connectionStrings.CurrentValue;
var isConnectionStringConfigured = umbracoConnectionString.IsConnectionStringConfigured();
if (isConnectionStringConfigured)
{
installState = (installState | InstallState.ConnectionStringConfigured) & ~InstallState.Unknown;
}
DbProviderFactory? factory = _dbProviderFactoryCreator.CreateFactory(umbracoConnectionString.ProviderName);
var isConnectionAvailable = isConnectionStringConfigured && DbConnectionExtensions.IsConnectionAvailable(umbracoConnectionString.ConnectionString, factory);
if (isConnectionAvailable)
{
installState = (installState | InstallState.CanConnect) & ~InstallState.Unknown;
}
var isUmbracoInstalled = isConnectionAvailable && _databaseBuilder.IsUmbracoInstalled();
if (isUmbracoInstalled)
{
installState = (installState | InstallState.UmbracoInstalled) & ~InstallState.Unknown;
}
var hasSomeNonDefaultUser = isUmbracoInstalled && _databaseBuilder.HasSomeNonDefaultUser();
if (hasSomeNonDefaultUser)
{
installState = (installState | InstallState.HasNonDefaultUser) & ~InstallState.Unknown;
}
return installState;
}
[Flags]
private enum InstallState : short
{
// This is an easy way to avoid 0 enum assignment and not worry about
// manual calcs. https://www.codeproject.com/Articles/396851/Ending-the-Great-Debate-on-Enum-Flags
Unknown = 1,
HasVersion = 1 << 1,
HasConnectionString = 1 << 2,
ConnectionStringConfigured = 1 << 3,
CanConnect = 1 << 4,
UmbracoInstalled = 1 << 5,
HasNonDefaultUser = 1 << 6
}
}