Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary null check in 2FA authorization filter #17009

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, AuditTrailSetti

public override async Task<IDisplayResult> UpdateAsync(ISite site, AuditTrailSettings settings, UpdateEditorContext context)
{
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, AuditTrailPermissions.ManageAuditTrailSettings))
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, AuditTrailPermissions.ManageAuditTrailSettings))
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private void ApplyContainedItemOptionsFilter(ContainedItemOptions containedItemO
query.With<ContentItemIndex>(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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ IStringLocalizer<LocalizationSettingsDisplayDriver> stringLocalizer
/// <inheritdocs />
public override async Task<IDisplayResult> EditAsync(ISite site, LocalizationSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageCultures))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, MicrosoftAccoun

public override async Task<IDisplayResult> UpdateAsync(ISite site, MicrosoftAccountSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved
if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageMicrosoftAuthentication))
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public OpenIdClientSettingsDisplayDriver(

public override async Task<IDisplayResult> EditAsync(ISite site, OpenIdClientSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved
if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageClientSettings))
{
return null;
Expand Down Expand Up @@ -103,7 +103,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, OpenIdClientSet

public override async Task<IDisplayResult> UpdateAsync(ISite site, OpenIdClientSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved
if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageClientSettings))
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public OpenIdServerSettingsDisplayDriver(

public override async Task<IDisplayResult> EditAsync(OpenIdServerSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageServerSettings))
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public override async Task<IDisplayResult> EditAsync(OpenIdServerSettings settin

public override async Task<IDisplayResult> UpdateAsync(OpenIdServerSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageServerSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public OpenIdValidationSettingsDisplayDriver(

public override async Task<IDisplayResult> EditAsync(OpenIdValidationSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageValidationSettings))
{
Expand All @@ -53,7 +53,7 @@ public override async Task<IDisplayResult> EditAsync(OpenIdValidationSettings se

public override async Task<IDisplayResult> UpdateAsync(OpenIdValidationSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageValidationSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override async Task<IDisplayResult> UpdateAsync(PublishLaterPart part, Up
{
var httpContext = _httpContextAccessor.HttpContext;

if (await _authorizationService.AuthorizeAsync(httpContext?.User, CommonPermissions.PublishContent, part.ContentItem))
if (await _authorizationService.AuthorizeAsync(httpContext.User, CommonPermissions.PublishContent, part.ContentItem))
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
var viewModel = new PublishLaterPartViewModel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override string SettingsGroupId

public override async Task<IDisplayResult> EditAsync(ISite site, ReCaptchaSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageReCaptchaSettings))
{
Expand All @@ -52,7 +52,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, ReCaptchaSettin

public override async Task<IDisplayResult> UpdateAsync(ISite site, ReCaptchaSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageReCaptchaSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override string SettingsGroupId

public override async Task<IDisplayResult> EditAsync(ISite site, ReverseProxySettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageReverseProxySettings))
{
Expand All @@ -54,7 +54,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, ReverseProxySet

public override async Task<IDisplayResult> UpdateAsync(ISite site, ReverseProxySettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageReverseProxySettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override async Task<IDisplayResult> UpdateAsync(ISite site, AzureAISearch
return null;
}

if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, AzureAISearchIndexPermissionHelper.ManageAzureAISearchIndexes))
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, AzureAISearchIndexPermissionHelper.ManageAzureAISearchIndexes))
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override IDisplayResult Edit(ISite site, AzureAISearchSettings settings,

public override async Task<IDisplayResult> UpdateAsync(ISite site, AzureAISearchSettings settings, UpdateEditorContext context)
{
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, AzureAISearchIndexPermissionHelper.ManageAzureAISearchIndexes))
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, AzureAISearchIndexPermissionHelper.ManageAzureAISearchIndexes))
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override async Task<IDisplayResult> UpdateAsync(ISite site, ElasticSettin
return null;
}

if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, Permissions.ManageElasticIndexes))
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, Permissions.ManageElasticIndexes))
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override string SettingsGroupId

public override async Task<IDisplayResult> EditAsync(ISite site, LuceneSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageLuceneIndexes))
{
Expand All @@ -51,7 +51,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, LuceneSettings

public override async Task<IDisplayResult> UpdateAsync(ISite site, LuceneSettings section, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageLuceneIndexes))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override string SettingsGroupId

public override async Task<IDisplayResult> EditAsync(ISite site, SearchSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageSearchSettings))
{
Expand All @@ -58,7 +58,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, SearchSettings

public override async Task<IDisplayResult> UpdateAsync(ISite site, SearchSettings section, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageSearchSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public SecuritySettingsDisplayDriver(

public override async Task<IDisplayResult> EditAsync(ISite site, SecuritySettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, SecurityPermissions.ManageSecurityHeadersSettings))
{
Expand Down Expand Up @@ -72,7 +72,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, SecuritySetting

public override async Task<IDisplayResult> UpdateAsync(ISite site, SecuritySettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, SecurityPermissions.ManageSecurityHeadersSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public RobotsSettingsDisplayDriver(

public override async Task<IDisplayResult> EditAsync(ISite site, RobotsSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, SeoConstants.ManageSeoSettings))
{
Expand All @@ -64,7 +64,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, RobotsSettings

public override async Task<IDisplayResult> UpdateAsync(ISite site, RobotsSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!context.GroupId.Equals(SeoConstants.RobotsSettingsGroupId, StringComparison.OrdinalIgnoreCase)
|| !await _authorizationService.AuthorizeAsync(user, SeoConstants.ManageSeoSettings))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected override string SettingsGroupId

public override async Task<IDisplayResult> EditAsync(ISite site, SitemapsRobotsSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, SeoConstants.ManageSeoSettings))
{
Expand All @@ -43,7 +43,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, SitemapsRobotsS

public override async Task<IDisplayResult> UpdateAsync(ISite site, SitemapsRobotsSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, SeoConstants.ManageSeoSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override IDisplayResult Edit(ISite site, AzureSmsSettings settings, Build
model.PhoneNumber = settings.PhoneNumber;
model.HasConnectionString = !string.IsNullOrEmpty(settings.ConnectionString);
}).Location("Content:5#Azure Communication Services")
.RenderWhen(() => _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, SmsPermissions.ManageSmsSettings))
.RenderWhen(() => _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, SmsPermissions.ManageSmsSettings))
hishamco marked this conversation as resolved.
Show resolved Hide resolved
.OnGroup(SettingsGroupId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public override IDisplayResult Edit(ISite site, SmsSettings settings, BuildEdito
.ToArray();

}).Location("Content:1#Providers")
.RenderWhen(() => _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, SmsPermissions.ManageSmsSettings))
.RenderWhen(() => _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, SmsPermissions.ManageSmsSettings))
hishamco marked this conversation as resolved.
Show resolved Hide resolved
.OnGroup(SettingsGroupId);

public override async Task<IDisplayResult> UpdateAsync(ISite site, SmsSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, SmsPermissions.ManageSmsSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public override IDisplayResult Edit(ISite site, TwilioSettings settings, BuildEd
model.AccountSID = settings.AccountSID;
model.HasAuthToken = !string.IsNullOrEmpty(settings.AuthToken);
}).Location("Content:5#Twilio")
.RenderWhen(() => _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, SmsPermissions.ManageSmsSettings))
.RenderWhen(() => _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, SmsPermissions.ManageSmsSettings))
hishamco marked this conversation as resolved.
Show resolved Hide resolved
.OnGroup(SettingsGroupId);
}

public override async Task<IDisplayResult> UpdateAsync(ISite site, TwilioSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

if (!await _authorizationService.AuthorizeAsync(user, SmsPermissions.ManageSmsSettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override string SettingsGroupId

public override async Task<IDisplayResult> EditAsync(ISite site, TaxonomyContentsAdminListSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved
if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageTaxonomies))
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, TwitterSettings

public override async Task<IDisplayResult> UpdateAsync(ISite site, TwitterSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
var user = _httpContextAccessor.HttpContext.User;
hishamco marked this conversation as resolved.
Show resolved Hide resolved
if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageTwitter))
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just strictly follow the nullable annotations of .NET, Identity can be null here as well.

hishamco marked this conversation as resolved.
Show resolved Hide resolved
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpC
{
ArgumentNullException.ThrowIfNull(httpContext);

if (httpContext?.User?.Identity?.IsAuthenticated == false)
if (httpContext?.User.Identity.IsAuthenticated == false)
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
return NullProviderCultureResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down