Backoffice api: Refactor controllers (#12934)

* Refactor install controller

* Removed unused InstallationTypeTarget

* Add ApiVersion to controllers

* Refactor upgrade controller

* Add missing RequireRuntimeLevelAttribute

(cherry picked from commit a356cf4f40)
This commit is contained in:
Mole
2022-09-13 08:04:45 +02:00
committed by Sebastiaan Janssen
parent 352da6c529
commit 22576edd70
9 changed files with 202 additions and 138 deletions

View File

@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using Umbraco.Cms.Core;
using Umbraco.Cms.ManagementApi.Filters;
using Umbraco.New.Cms.Web.Common.Routing;
namespace Umbraco.Cms.ManagementApi.Controllers.Install;
[ApiController]
[BackOfficeRoute("api/v{version:apiVersion}/install")]
[OpenApiTag("Install")]
[RequireRuntimeLevel(RuntimeLevel.Install)]
public abstract class InstallControllerBase : Controller
{
}

View File

@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Infrastructure.Install;
using Umbraco.Cms.ManagementApi.ViewModels.Installer;
using Umbraco.New.Cms.Core.Factories;
using Umbraco.New.Cms.Core.Models.Installer;
namespace Umbraco.Cms.ManagementApi.Controllers.Install;
[ApiVersion("1.0")]
public class SettingsInstallController : InstallControllerBase
{
private readonly InstallHelper _installHelper;
private readonly IInstallSettingsFactory _installSettingsFactory;
private readonly IUmbracoMapper _mapper;
public SettingsInstallController(
InstallHelper installHelper,
IInstallSettingsFactory installSettingsFactory,
IUmbracoMapper mapper)
{
_installHelper = installHelper;
_installSettingsFactory = installSettingsFactory;
_mapper = mapper;
}
[HttpGet("settings")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status428PreconditionRequired)]
[ProducesResponseType(typeof(InstallSettingsViewModel), StatusCodes.Status200OK)]
public async Task<ActionResult<InstallSettingsViewModel>> Settings()
{
// Register that the install has started
await _installHelper.SetInstallStatusAsync(false, string.Empty);
InstallSettingsModel installSettings = _installSettingsFactory.GetInstallSettings();
InstallSettingsViewModel viewModel = _mapper.Map<InstallSettingsViewModel>(installSettings)!;
return viewModel;
}
}

View File

@@ -0,0 +1,47 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.ManagementApi.ViewModels.Installer;
using Umbraco.Extensions;
using Umbraco.New.Cms.Core.Models.Installer;
using Umbraco.New.Cms.Core.Services.Installer;
namespace Umbraco.Cms.ManagementApi.Controllers.Install;
[ApiVersion("1.0")]
public class SetupInstallController : InstallControllerBase
{
private readonly IUmbracoMapper _mapper;
private readonly IInstallService _installService;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly GlobalSettings _globalSettings;
public SetupInstallController(
IUmbracoMapper mapper,
IInstallService installService,
IOptions<GlobalSettings> globalSettings,
IHostingEnvironment hostingEnvironment)
{
_mapper = mapper;
_installService = installService;
_hostingEnvironment = hostingEnvironment;
_globalSettings = globalSettings.Value;
}
[HttpPost("setup")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status428PreconditionRequired)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> Setup(InstallViewModel installData)
{
InstallData data = _mapper.Map<InstallData>(installData)!;
await _installService.Install(data);
var backOfficePath = _globalSettings.GetBackOfficePath(_hostingEnvironment);
return Created(backOfficePath, null);
}
}

View File

@@ -0,0 +1,52 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.Install.Models;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Infrastructure.Migrations.Install;
using Umbraco.Cms.ManagementApi.ViewModels.Installer;
namespace Umbraco.Cms.ManagementApi.Controllers.Install;
[ApiVersion("1.0")]
public class ValidateDatabaseInstallController : InstallControllerBase
{
private readonly DatabaseBuilder _databaseBuilder;
private readonly IUmbracoMapper _mapper;
public ValidateDatabaseInstallController(
DatabaseBuilder databaseBuilder,
IUmbracoMapper mapper)
{
_databaseBuilder = databaseBuilder;
_mapper = mapper;
}
[HttpPost("validateDatabase")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> ValidateDatabase(DatabaseInstallViewModel viewModel)
{
// TODO: Async - We need to figure out what we want to do with async endpoints that doesn't do anything async
// We want these to be async for future use (Ideally we'll have more async things),
// But we need to figure out how we want to handle it in the meantime? use Task.FromResult or?
DatabaseModel databaseModel = _mapper.Map<DatabaseModel>(viewModel)!;
var success = _databaseBuilder.ConfigureDatabaseConnection(databaseModel, true);
if (success)
{
return Ok();
}
var invalidModelProblem = new ProblemDetails
{
Title = "Invalid database configuration",
Detail = "The provided database configuration is invalid",
Status = StatusCodes.Status400BadRequest,
Type = "Error",
};
return BadRequest(invalidModelProblem);
}
}

View File

@@ -1,111 +0,0 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Install.Models;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Infrastructure.Install;
using Umbraco.Cms.Infrastructure.Migrations.Install;
using Umbraco.Cms.ManagementApi.Filters;
using Umbraco.Cms.ManagementApi.ViewModels.Installer;
using Umbraco.Extensions;
using Umbraco.New.Cms.Core.Factories;
using Umbraco.New.Cms.Core.Models.Installer;
using Umbraco.New.Cms.Core.Services.Installer;
using Umbraco.New.Cms.Web.Common.Routing;
namespace Umbraco.Cms.ManagementApi.Controllers;
[ApiController]
[ApiVersion("1.0")]
[BackOfficeRoute("api/v{version:apiVersion}/install")]
[RequireRuntimeLevel(RuntimeLevel.Install)]
public class NewInstallController : Controller
{
private readonly IUmbracoMapper _mapper;
private readonly IInstallSettingsFactory _installSettingsFactory;
private readonly IInstallService _installService;
private readonly GlobalSettings _globalSettings;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly InstallHelper _installHelper;
private readonly DatabaseBuilder _databaseBuilder;
public NewInstallController(
IUmbracoMapper mapper,
IInstallSettingsFactory installSettingsFactory,
IInstallService installService,
IOptions<GlobalSettings> globalSettings,
IHostingEnvironment hostingEnvironment,
InstallHelper installHelper,
DatabaseBuilder databaseBuilder)
{
_mapper = mapper;
_installSettingsFactory = installSettingsFactory;
_installService = installService;
_globalSettings = globalSettings.Value;
_hostingEnvironment = hostingEnvironment;
_installHelper = installHelper;
_databaseBuilder = databaseBuilder;
}
[HttpGet("settings")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status428PreconditionRequired)]
[ProducesResponseType(typeof(InstallSettingsViewModel), StatusCodes.Status200OK)]
public async Task<ActionResult<InstallSettingsViewModel>> Settings()
{
// Register that the install has started
await _installHelper.SetInstallStatusAsync(false, string.Empty);
InstallSettingsModel installSettings = _installSettingsFactory.GetInstallSettings();
InstallSettingsViewModel viewModel = _mapper.Map<InstallSettingsViewModel>(installSettings)!;
return viewModel;
}
[HttpPost("setup")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status428PreconditionRequired)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> Setup(InstallViewModel installData)
{
InstallData data = _mapper.Map<InstallData>(installData)!;
await _installService.Install(data);
var backOfficePath = _globalSettings.GetBackOfficePath(_hostingEnvironment);
return Created(backOfficePath, null);
}
[HttpPost("validateDatabase")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> ValidateDatabase(DatabaseInstallViewModel viewModel)
{
// TODO: Async - We need to figure out what we want to do with async endpoints that doesn't do anything async
// We want these to be async for future use (Ideally we'll have more async things),
// But we need to figure out how we want to handle it in the meantime? use Task.FromResult or?
DatabaseModel databaseModel = _mapper.Map<DatabaseModel>(viewModel)!;
var success = _databaseBuilder.ConfigureDatabaseConnection(databaseModel, true);
if (success)
{
return Ok();
}
var invalidModelProblem = new ProblemDetails
{
Title = "Invalid database configuration",
Detail = "The provided database configuration is invalid",
Status = StatusCodes.Status400BadRequest,
Type = "Error",
};
return BadRequest(invalidModelProblem);
}
}

View File

@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.New.Cms.Core.Services.Installer;
namespace Umbraco.Cms.ManagementApi.Controllers.Upgrade;
[ApiVersion("1.0")]
public class AuthorizeUpgradeController : UpgradeControllerBase
{
private readonly IUpgradeService _upgradeService;
public AuthorizeUpgradeController(IUpgradeService upgradeService) => _upgradeService = upgradeService;
[HttpPost("authorize")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status428PreconditionRequired)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Authorize()
{
await _upgradeService.Upgrade();
return Ok();
}
}

View File

@@ -1,48 +1,26 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.ManagementApi.Filters;
using Umbraco.Cms.ManagementApi.ViewModels.Installer;
using Umbraco.New.Cms.Core.Factories;
using Umbraco.New.Cms.Core.Models.Installer;
using Umbraco.New.Cms.Core.Services.Installer;
using Umbraco.New.Cms.Web.Common.Routing;
namespace Umbraco.Cms.ManagementApi.Controllers;
namespace Umbraco.Cms.ManagementApi.Controllers.Upgrade;
// TODO: This needs to be an authorized controller.
[ApiController]
[ApiVersion("1.0")]
[RequireRuntimeLevel(RuntimeLevel.Upgrade)]
[BackOfficeRoute("api/v{version:apiVersion}/upgrade")]
public class UpgradeController : Controller
public class SettingsUpgradeController : UpgradeControllerBase
{
private readonly IUpgradeSettingsFactory _upgradeSettingsFactory;
private readonly IUpgradeService _upgradeService;
private readonly IUmbracoMapper _mapper;
public UpgradeController(
public SettingsUpgradeController(
IUpgradeSettingsFactory upgradeSettingsFactory,
IUpgradeService upgradeService,
IUmbracoMapper mapper)
{
_upgradeSettingsFactory = upgradeSettingsFactory;
_upgradeService = upgradeService;
_mapper = mapper;
}
[HttpPost("authorize")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status428PreconditionRequired)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Authorize()
{
await _upgradeService.Upgrade();
return Ok();
}
[HttpGet("settings")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(UpgradeSettingsViewModel), StatusCodes.Status200OK)]

View File

@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using Umbraco.Cms.Core;
using Umbraco.Cms.ManagementApi.Filters;
using Umbraco.New.Cms.Web.Common.Routing;
namespace Umbraco.Cms.ManagementApi.Controllers.Upgrade;
// TODO: This needs to be an authorized controller.
[ApiController]
[RequireRuntimeLevel(RuntimeLevel.Upgrade)]
[BackOfficeRoute("api/v{version:apiVersion}/upgrade")]
[OpenApiTag("Upgrade")]
public abstract class UpgradeControllerBase : Controller
{
}

View File

@@ -20,8 +20,6 @@ public class SignInUserStep : IInstallStep
_backOfficeUserManager = backOfficeUserManager;
}
public InstallationType InstallationTypeTarget => InstallationType.NewInstall;
public async Task ExecuteAsync(InstallData model)
{
BackOfficeIdentityUser identityUser = await _backOfficeUserManager.FindByIdAsync(Constants.Security.SuperUserIdAsString);