Hello,
I am working on a package migration which runs when the package is installed.
With a lot of the newer bits and pieces of Umbraco supporting Async methods in the services. I was wondering if PackageMigrations support async at all?
As writing .Result
in my code is giving me the heebie jeebies
// Booo :( the Migrate from PackageMigrationBase does not support Async it seems ?!
var adminGroup = _userGroupService.GetAsync(Umbraco.Cms.Core.Constants.Security.AdminGroupKey).Result;
Example Code
This is what I have and was hoping to not use .Result in the codebase.
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Cms.Infrastructure.Packaging;
namespace ContentLock.Migrations.v1
{
public class AddUserPermissionToAdmins : PackageMigrationBase
{
private readonly IUserGroupService _userGroupService;
private readonly ILogger<AddUserPermissionToAdmins> _logger;
public AddUserPermissionToAdmins(IPackagingService packagingService,
IMediaService mediaService,
MediaFileManager mediaFileManager,
MediaUrlGeneratorCollection mediaUrlGenerators,
IShortStringHelper shortStringHelper,
IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
IMigrationContext context,
IOptions<PackageMigrationSettings> packageMigrationsSettings,
IUserGroupService userGroupService,
IUserService userService,
ILogger<AddUserPermissionToAdmins> logger)
: base(
packagingService,
mediaService,
mediaFileManager,
mediaUrlGenerators,
shortStringHelper,
contentTypeBaseServiceProvider,
context,
packageMigrationsSettings)
{
_userGroupService = userGroupService;
_logger = logger;
}
protected override void Migrate()
{
// Booo :( the Migrate from PackageMigrationBase does not support Async
var adminGroup = _userGroupService.GetAsync(Umbraco.Cms.Core.Constants.Security.AdminGroupKey).Result;
if (adminGroup == null)
{
_logger.LogWarning("ContentLock is unable to find the default Umbraco Admin User Group. Exiting");
return;
}
// Permissions ?!
var permissions = adminGroup.Permissions;
// Add new permission (Same as the permission verb in clientside code)
permissions.Add("ContentLock.Enabled");
// Update the user group
var attempt = _userGroupService.UpdateAsync(adminGroup, Umbraco.Cms.Core.Constants.Security.SuperUserKey).Result;
_logger.LogTrace("Updated default Umbraco Admin User Group with the 'ContentLock.Enabled' permission with this attempt status {status}", attempt.Status);
if (!attempt.Success)
{
_logger.LogWarning("ContentLock was unable to update the default Umbraco Admin User Group with permission 'ContentLock.Enabled'");
_logger.LogError(attempt.Exception, "Error updating the User Group permission");
}
}
}
}