diff --git a/src/Umbraco.Cms.Api.Management/Services/DictionaryItemImportService.cs b/src/Umbraco.Cms.Api.Management/Services/DictionaryItemImportService.cs index dda6acd511..53d9c4fb5a 100644 --- a/src/Umbraco.Cms.Api.Management/Services/DictionaryItemImportService.cs +++ b/src/Umbraco.Cms.Api.Management/Services/DictionaryItemImportService.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using Umbraco.Cms.Api.Management.Services.OperationStatus; using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Extensions; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.TemporaryFile; using Umbraco.Cms.Core.Scoping; @@ -70,7 +71,7 @@ internal sealed class DictionaryItemImportService : IDictionaryItemImportService } // import the UDT file - (IDictionaryItem? DictionaryItem, DictionaryImportOperationStatus Status) importResult = ImportUdtFile(loadResult.Document, userKey, parentKey, temporaryFile); + (IDictionaryItem? DictionaryItem, DictionaryImportOperationStatus Status) importResult = await ImportUdtFile(loadResult.Document, userKey, parentKey, temporaryFile); scope.Complete(); @@ -97,7 +98,7 @@ internal sealed class DictionaryItemImportService : IDictionaryItemImportService } } - private (IDictionaryItem? DictionaryItem, DictionaryImportOperationStatus Status) ImportUdtFile(XDocument udtFileContent, Guid userKey, Guid? parentKey, TemporaryFileModel temporaryFileModel) + private async Task<(IDictionaryItem? DictionaryItem, DictionaryImportOperationStatus Status)> ImportUdtFile(XDocument udtFileContent, Guid userKey, Guid? parentKey, TemporaryFileModel temporaryFileModel) { if (udtFileContent.Root == null) { @@ -106,7 +107,7 @@ internal sealed class DictionaryItemImportService : IDictionaryItemImportService try { - var currentUserId = _userService.GetAsync(userKey).Result?.Id ?? Constants.Security.SuperUserId; + var currentUserId = (await _userService.GetRequiredUserAsync(userKey)).Id; IEnumerable dictionaryItems = _packageDataInstallation.ImportDictionaryItem(udtFileContent.Root, currentUserId, parentKey); IDictionaryItem? importedDictionaryItem = dictionaryItems.FirstOrDefault(); return importedDictionaryItem != null diff --git a/src/Umbraco.Core/Extensions/UserServiceExtensions.cs b/src/Umbraco.Core/Extensions/UserServiceExtensions.cs new file mode 100644 index 0000000000..0946891b31 --- /dev/null +++ b/src/Umbraco.Core/Extensions/UserServiceExtensions.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models.Membership; +using Umbraco.Cms.Core.Services; + +namespace Umbraco.Cms.Core.Extensions; + +public static class UserServiceExtensions +{ + public static async Task GetRequiredUserAsync(this IUserService userService, Guid key) + => await userService.GetAsync(key) + ?? throw new InvalidOperationException($"Could not find user with key: {key}"); +} diff --git a/src/Umbraco.Core/Services/AuditService.cs b/src/Umbraco.Core/Services/AuditService.cs index 7da7bf7d9c..32a08808e3 100644 --- a/src/Umbraco.Core/Services/AuditService.cs +++ b/src/Umbraco.Core/Services/AuditService.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Exceptions; +using Umbraco.Cms.Core.Extensions; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.Entities; using Umbraco.Cms.Core.Models.Membership; @@ -235,11 +236,7 @@ public sealed class AuditService : RepositoryService, IAuditService using (ScopeProvider.CreateCoreScope(autoComplete: true)) { - var user = await _userService.GetAsync(entityKey); - if (user is null) - { - throw new ArgumentNullException($"Could not find user with key {entityKey}"); - } + IUser user = await _userService.GetRequiredUserAsync(entityKey); IQuery query = Query().Where(x => x.UserId == user.Id); IQuery? customFilter = sinceDate.HasValue ? Query().Where(x => x.CreateDate >= sinceDate) : null; diff --git a/src/Umbraco.Core/Services/MediaImportService.cs b/src/Umbraco.Core/Services/MediaImportService.cs index 8c73bbc89f..ca5af17a73 100644 --- a/src/Umbraco.Core/Services/MediaImportService.cs +++ b/src/Umbraco.Core/Services/MediaImportService.cs @@ -1,4 +1,5 @@ using Umbraco.Cms.Core.Cache; +using Umbraco.Cms.Core.Extensions; using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.Membership; @@ -46,8 +47,7 @@ internal sealed class MediaImportService : IMediaImportService throw new InvalidOperationException("Could not read from file stream, please ensure it is open and readable"); } - IUser user = await _userService.GetAsync(userKey) - ?? throw new ArgumentException($"Could not find a user with the specified user key ({userKey})", nameof(userKey)); + IUser user = await _userService.GetRequiredUserAsync(userKey); var safeFileName = fileName.ToSafeFileName(_shortStringHelper); var mediaItemName = safeFileName.ToFriendlyName(); diff --git a/src/Umbraco.Core/Services/MemberContentEditingService.cs b/src/Umbraco.Core/Services/MemberContentEditingService.cs index 0d564e71d7..d53121fe6a 100644 --- a/src/Umbraco.Core/Services/MemberContentEditingService.cs +++ b/src/Umbraco.Core/Services/MemberContentEditingService.cs @@ -39,8 +39,7 @@ internal sealed class MemberContentEditingService IMemberType memberType = await ContentTypeService.GetAsync(member.ContentType.Key) ?? throw new InvalidOperationException($"The member type {member.ContentType.Alias} could not be found."); - IUser user = await _userService.GetAsync(userKey) - ?? throw new InvalidOperationException("The supplied user key did not match any existing user"); + IUser user = await _userService.GetRequiredUserAsync(userKey); if (ValidateAccessToSensitiveProperties(member, memberType, updateModel, user) is false) { diff --git a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs index d22efd1529..ad8de35609 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Extensions; using Umbraco.Cms.Core.Manifest; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.Packaging; @@ -126,7 +127,7 @@ public class PackagingService : IPackagingService return Attempt.FailWithStatus(PackageOperationStatus.NotFound, null); } - int currentUserId = _userService.GetAsync(userKey).Result?.Id ?? Constants.Security.SuperUserId; + int currentUserId = (await _userService.GetRequiredUserAsync(userKey)).Id; _auditService.Add(AuditType.Delete, currentUserId, -1, "Package", $"Created package '{package.Name}' deleted. Package key: {key}"); _createdPackages.Delete(package.Id); @@ -189,7 +190,7 @@ public class PackagingService : IPackagingService return Attempt.FailWithStatus(PackageOperationStatus.DuplicateItemName, package); } - int currentUserId = _userService.GetAsync(userKey).Result?.Id ?? Constants.Security.SuperUserId; + int currentUserId = (await _userService.GetRequiredUserAsync(userKey)).Id; _auditService.Add(AuditType.New, currentUserId, -1, "Package", $"Created package '{package.Name}' created. Package key: {package.PackageId}"); scope.Complete(); return await Task.FromResult(Attempt.SucceedWithStatus(PackageOperationStatus.Success, package)); @@ -205,7 +206,7 @@ public class PackagingService : IPackagingService return Attempt.FailWithStatus(PackageOperationStatus.NotFound, package); } - int currentUserId = _userService.GetAsync(userKey).Result?.Id ?? Constants.Security.SuperUserId; + int currentUserId = (await _userService.GetRequiredUserAsync(userKey)).Id; _auditService.Add(AuditType.New, currentUserId, -1, "Package", $"Created package '{package.Name}' updated. Package key: {package.PackageId}"); scope.Complete(); return await Task.FromResult(Attempt.SucceedWithStatus(PackageOperationStatus.Success, package)); diff --git a/tests/Umbraco.Tests.Integration/ManagementApi/ManagementApiTest.cs b/tests/Umbraco.Tests.Integration/ManagementApi/ManagementApiTest.cs index 1eab5885f2..b23588a0c5 100644 --- a/tests/Umbraco.Tests.Integration/ManagementApi/ManagementApiTest.cs +++ b/tests/Umbraco.Tests.Integration/ManagementApi/ManagementApiTest.cs @@ -15,6 +15,7 @@ using Umbraco.Cms.Api.Management.Controllers; using Umbraco.Cms.Api.Management.Controllers.Security; using Umbraco.Cms.Api.Management.Security; using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Extensions; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.Membership; using Umbraco.Cms.Core.Scoping; @@ -60,8 +61,7 @@ public abstract class ManagementApiTest : UmbracoTestServerTestBase IUser user; if (isAdmin) { - user = await userService.GetAsync(userKey) ?? - throw new Exception("Super user not found."); + user = await userService.GetRequiredUserAsync(userKey); user.Username = user.Email = username; userService.Save(user); }