From f74a99b091ac3750a0e68e8aaf9f152e924c9447 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Sun, 1 Dec 2024 01:43:23 +0300 Subject: [PATCH] Remove unnecessary null check in 2FA authorization filter (#17009) Co-authored-by: Georg von Kries --- .../OrchardCore.Lists/Services/ContainerService.cs | 2 +- .../Filters/TwoFactorAuthenticationAuthorizationFilter.cs | 2 +- .../Providers/UserLocalizationRequestCultureProvider.cs | 2 +- .../Handlers/UpdateContentsHandler.cs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Lists/Services/ContainerService.cs b/src/OrchardCore.Modules/OrchardCore.Lists/Services/ContainerService.cs index 45c5f2a9f90..e78305ea817 100644 --- a/src/OrchardCore.Modules/OrchardCore.Lists/Services/ContainerService.cs +++ b/src/OrchardCore.Modules/OrchardCore.Lists/Services/ContainerService.cs @@ -362,7 +362,7 @@ private void ApplyContainedItemOptionsFilter(ContainedItemOptions containedItemO query.With(i => !i.Published && i.Latest); break; case ContentsStatus.Owner: - var currentUserName = _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier); + var currentUserName = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); if (currentUserName != null) { diff --git a/src/OrchardCore.Modules/OrchardCore.Users/Filters/TwoFactorAuthenticationAuthorizationFilter.cs b/src/OrchardCore.Modules/OrchardCore.Users/Filters/TwoFactorAuthenticationAuthorizationFilter.cs index c0543341d0a..52a4785483e 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/Filters/TwoFactorAuthenticationAuthorizationFilter.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/Filters/TwoFactorAuthenticationAuthorizationFilter.cs @@ -36,7 +36,7 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { ArgumentNullException.ThrowIfNull(context); - if (context.HttpContext?.User?.Identity?.IsAuthenticated == false || + if ((context.HttpContext?.User.Identity?.IsAuthenticated ?? false) == false || context.HttpContext.Request.Path.Equals("/" + _userOptions.LogoffPath, StringComparison.OrdinalIgnoreCase) || context.HttpContext.Request.Path.Equals("/" + _userOptions.TwoFactorAuthenticationPath, StringComparison.OrdinalIgnoreCase) || context.HttpContext.Request.Path.Equals("/TwoFactor-Authenticator/", StringComparison.OrdinalIgnoreCase)) diff --git a/src/OrchardCore.Modules/OrchardCore.Users/Localization/Providers/UserLocalizationRequestCultureProvider.cs b/src/OrchardCore.Modules/OrchardCore.Users/Localization/Providers/UserLocalizationRequestCultureProvider.cs index d4985f0dc3e..1e191401adf 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/Localization/Providers/UserLocalizationRequestCultureProvider.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/Localization/Providers/UserLocalizationRequestCultureProvider.cs @@ -10,7 +10,7 @@ public override Task DetermineProviderCultureResult(HttpC { ArgumentNullException.ThrowIfNull(httpContext); - if (httpContext?.User?.Identity?.IsAuthenticated == false) + if ((httpContext?.User.Identity?.IsAuthenticated ?? false) == false) { return NullProviderCultureResult; } diff --git a/src/OrchardCore/OrchardCore.ContentManagement/Handlers/UpdateContentsHandler.cs b/src/OrchardCore/OrchardCore.ContentManagement/Handlers/UpdateContentsHandler.cs index c22e6502259..09d1045d878 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement/Handlers/UpdateContentsHandler.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement/Handlers/UpdateContentsHandler.cs @@ -26,7 +26,7 @@ public override Task CreatingAsync(CreateContentContext context) context.ContentItem.ModifiedUtc = utcNow; var httpContext = _httpContextAccessor.HttpContext; - if (context.ContentItem.Owner == null && (httpContext?.User?.Identity?.IsAuthenticated ?? false)) + if (context.ContentItem.Owner == null && (httpContext?.User.Identity?.IsAuthenticated ?? false)) { context.ContentItem.Owner = httpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); context.ContentItem.Author = httpContext.User.Identity.Name; @@ -40,7 +40,7 @@ public override Task UpdatingAsync(UpdateContentContext context) var utcNow = _clock.UtcNow; context.ContentItem.ModifiedUtc = utcNow; var httpContext = _httpContextAccessor.HttpContext; - if (httpContext?.User?.Identity?.IsAuthenticated ?? false) + if (httpContext?.User.Identity?.IsAuthenticated ?? false) { // The value is only modified during update so that another event like // publishing in a Workflow doesn't change it.