Replace usage of Thread.CurrentPrincipal with IBackofficeSecurityAccessor

This commit is contained in:
Mole
2021-02-23 08:38:27 +01:00
parent 9d0493d38f
commit f1128d7d70
2 changed files with 13 additions and 4 deletions

View File

@@ -26,14 +26,22 @@ namespace Umbraco.Core.Compose
private readonly IUserService _userService;
private readonly IEntityService _entityService;
private readonly IIpResolver _ipResolver;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly GlobalSettings _globalSettings;
public AuditEventsComponent(IAuditService auditService, IUserService userService, IEntityService entityService, IIpResolver ipResolver, IOptions<GlobalSettings> globalSettings)
public AuditEventsComponent(
IAuditService auditService,
IUserService userService,
IEntityService entityService,
IIpResolver ipResolver,
IOptions<GlobalSettings> globalSettings,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
{
_auditService = auditService;
_userService = userService;
_entityService = entityService;
_ipResolver = ipResolver;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_globalSettings = globalSettings.Value;
}
@@ -73,8 +81,8 @@ namespace Umbraco.Core.Compose
{
get
{
var identity = Thread.CurrentPrincipal?.GetUmbracoIdentity();
var user = identity == null ? null : _userService.GetUserById(Convert.ToInt32(identity.GetId()));
var identity = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser;
var user = identity == null ? null : _userService.GetUserById(Convert.ToInt32(identity.Id));
return user ?? UnknownUser(_globalSettings);
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Principal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
@@ -39,7 +40,7 @@ namespace Umbraco.Web.Common.Security
private string GetCurrentUserId(IPrincipal currentUser)
{
UmbracoBackOfficeIdentity umbIdentity = currentUser?.GetUmbracoIdentity();
ClaimsIdentity umbIdentity = currentUser?.GetUmbracoIdentity();
var currentUserId = umbIdentity?.GetUserId<string>() ?? Cms.Core.Constants.Security.SuperUserIdAsString;
return currentUserId;
}