From ad6ef2688dc2759f67a4f44e0d89e6a18d9a36ab Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Tue, 24 May 2022 13:36:31 +0200 Subject: [PATCH] Changed back Attempt to Attempt (#12468) --- src/Umbraco.Core/Security/IBackofficeSecurity.cs | 2 +- .../Controllers/AuthenticationController.cs | 2 +- .../Controllers/CurrentUserController.cs | 2 +- .../Controllers/UsersController.cs | 4 ++-- .../Security/BackofficeSecurity.cs | 12 +++++++----- .../Umbraco.Core/CoreThings/ObjectExtensionsTests.cs | 2 +- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Core/Security/IBackofficeSecurity.cs b/src/Umbraco.Core/Security/IBackofficeSecurity.cs index 3b3c956cd6..12b29a0288 100644 --- a/src/Umbraco.Core/Security/IBackofficeSecurity.cs +++ b/src/Umbraco.Core/Security/IBackofficeSecurity.cs @@ -21,7 +21,7 @@ namespace Umbraco.Cms.Core.Security /// The current user's Id that has been authenticated for the request. /// If authentication hasn't taken place this will be unsuccessful. // TODO: This should just be an extension method on ClaimsIdentity - Attempt GetUserId(); + Attempt GetUserId(); /// /// Checks if the specified user as access to the app diff --git a/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs b/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs index fcf76f4e9c..f15adfd28b 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs @@ -130,7 +130,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers [Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)] // Needed to enforce the principle set on the request, if one exists. public IDictionary GetPasswordConfig(int userId) { - Attempt currentUserId = _backofficeSecurityAccessor.BackOfficeSecurity?.GetUserId() ?? Attempt.Fail(); + Attempt currentUserId = _backofficeSecurityAccessor.BackOfficeSecurity?.GetUserId() ?? Attempt.Fail(); return _passwordConfiguration.GetConfiguration( currentUserId.Success ? currentUserId.Result != userId diff --git a/src/Umbraco.Web.BackOffice/Controllers/CurrentUserController.cs b/src/Umbraco.Web.BackOffice/Controllers/CurrentUserController.cs index 0084c0680d..5c75534a8b 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/CurrentUserController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/CurrentUserController.cs @@ -311,7 +311,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers [ValidateAngularAntiForgeryToken] public async Task> GetCurrentUserLinkedLogins() { - var identityUser = await _backOfficeUserManager.FindByIdAsync(_backofficeSecurityAccessor.BackOfficeSecurity?.GetUserId().ResultOr(0)?.ToString(CultureInfo.InvariantCulture)); + var identityUser = await _backOfficeUserManager.FindByIdAsync(_backofficeSecurityAccessor.BackOfficeSecurity?.GetUserId().ResultOr(0).ToString(CultureInfo.InvariantCulture)); // deduplicate in case there are duplicates (there shouldn't be now since we have a unique constraint on the external logins // but there didn't used to be) diff --git a/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs b/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs index 22a0a47fc5..734f1d6062 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs @@ -742,8 +742,8 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers [Authorize(Policy = AuthorizationPolicies.AdminUserEditsRequireAdmin)] public IActionResult PostDisableUsers([FromQuery]int[] userIds) { - var tryGetCurrentUserId = _backofficeSecurityAccessor.BackOfficeSecurity?.GetUserId() ?? Attempt.Fail(); - if (tryGetCurrentUserId.Success && userIds.Contains(tryGetCurrentUserId.Result!.Value)) + var tryGetCurrentUserId = _backofficeSecurityAccessor.BackOfficeSecurity?.GetUserId() ?? Attempt.Fail(); + if (tryGetCurrentUserId.Success && userIds.Contains(tryGetCurrentUserId.Result)) { return ValidationProblem("The current user cannot disable itself"); } diff --git a/src/Umbraco.Web.Common/Security/BackofficeSecurity.cs b/src/Umbraco.Web.Common/Security/BackofficeSecurity.cs index e1fa57557b..f772ad583c 100644 --- a/src/Umbraco.Web.Common/Security/BackofficeSecurity.cs +++ b/src/Umbraco.Web.Common/Security/BackofficeSecurity.cs @@ -36,10 +36,10 @@ public class BackOfficeSecurity : IBackOfficeSecurity // Check again if (_currentUser == null) { - Attempt id = GetUserId(); - if (id.Success && id.Result is not null) + Attempt id = GetUserId(); + if (id.Success) { - _currentUser = id.Success ? _userService.GetUserById(id.Result.Value) : null; + _currentUser = id.Success ? _userService.GetUserById(id.Result) : null; } } } @@ -50,10 +50,12 @@ public class BackOfficeSecurity : IBackOfficeSecurity } /// - public Attempt GetUserId() + public Attempt GetUserId() { ClaimsIdentity? identity = _httpContextAccessor.HttpContext?.GetCurrentIdentity(); - return identity == null ? Attempt.Fail() : Attempt.Succeed(identity.GetId()); + + var id = identity?.GetId(); + return id.HasValue is false ? Attempt.Fail() : Attempt.Succeed(id.Value); } /// diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/CoreThings/ObjectExtensionsTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/CoreThings/ObjectExtensionsTests.cs index f8a07fdf21..00362a0abe 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/CoreThings/ObjectExtensionsTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/CoreThings/ObjectExtensionsTests.cs @@ -60,7 +60,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.CoreThings public void CanConvertIntToNullableInt() { int i = 1; - Attempt result = i.TryConvertTo(); + Attempt result = i.TryConvertTo(); Assert.That(result.Success, Is.True); }