Injecting IWebSecurity directly instead of getting it through UmbracoContext.Security

This commit is contained in:
Elitsa Marinovska
2020-06-04 13:40:33 +02:00
parent b2b0291386
commit e32d5bb9b9
10 changed files with 49 additions and 42 deletions

View File

@@ -7,6 +7,7 @@ using Umbraco.Core.Configuration;
using Umbraco.Core.Models.Packaging;
using Umbraco.Net;
using Umbraco.Web.Install.Models;
using Umbraco.Web.Security;
namespace Umbraco.Web.Install.InstallSteps
{
@@ -16,16 +17,16 @@ namespace Umbraco.Web.Install.InstallSteps
internal class StarterKitDownloadStep : InstallSetupStep<Guid?>
{
private readonly InstallHelper _installHelper;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebSecurity _webSecurity;
private readonly IUmbracoVersion _umbracoVersion;
private readonly IUmbracoApplicationLifetime _umbracoApplicationLifetime;
private readonly IContentService _contentService;
private readonly IPackagingService _packageService;
public StarterKitDownloadStep(IContentService contentService, IPackagingService packageService, InstallHelper installHelper, IUmbracoContextAccessor umbracoContextAccessor, IUmbracoVersion umbracoVersion, IUmbracoApplicationLifetime umbracoApplicationLifetime)
public StarterKitDownloadStep(IContentService contentService, IPackagingService packageService, InstallHelper installHelper, IWebSecurity webSecurity, IUmbracoVersion umbracoVersion, IUmbracoApplicationLifetime umbracoApplicationLifetime)
{
_installHelper = installHelper;
_umbracoContextAccessor = umbracoContextAccessor;
_webSecurity = webSecurity;
_umbracoVersion = umbracoVersion;
_umbracoApplicationLifetime = umbracoApplicationLifetime;
_contentService = contentService;
@@ -66,7 +67,7 @@ namespace Umbraco.Web.Install.InstallSteps
private async Task<(string packageFile, int packageId)> DownloadPackageFilesAsync(Guid kitGuid)
{
//Go get the package file from the package repo
var packageFile = await _packageService.FetchPackageFileAsync(kitGuid, _umbracoVersion.Current, _umbracoContextAccessor.UmbracoContext.Security.GetUserId().ResultOr(0));
var packageFile = await _packageService.FetchPackageFileAsync(kitGuid, _umbracoVersion.Current, _webSecurity.GetUserId().ResultOr(0));
if (packageFile == null) throw new InvalidOperationException("Could not fetch package file " + kitGuid);
//add an entry to the installedPackages.config
@@ -76,7 +77,7 @@ namespace Umbraco.Web.Install.InstallSteps
_packageService.SaveInstalledPackage(packageDefinition);
_packageService.InstallCompiledPackageFiles(packageDefinition, packageFile, _umbracoContextAccessor.UmbracoContext.Security.GetUserId().ResultOr(-1));
_packageService.InstallCompiledPackageFiles(packageDefinition, packageFile, _webSecurity.GetUserId().ResultOr(-1));
return (compiledPackage.PackageFile.Name, packageDefinition.Id);
}

View File

@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Umbraco.Net;
using Umbraco.Core.Services;
using Umbraco.Web.Install.Models;
using Umbraco.Web.Security;
namespace Umbraco.Web.Install.InstallSteps
{
@@ -14,13 +15,13 @@ namespace Umbraco.Web.Install.InstallSteps
internal class StarterKitInstallStep : InstallSetupStep<object>
{
private readonly IUmbracoApplicationLifetime _umbracoApplicationLifetime;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebSecurity _webSecurity;
private readonly IPackagingService _packagingService;
public StarterKitInstallStep(IUmbracoApplicationLifetime umbracoApplicationLifetime, IUmbracoContextAccessor umbracoContextAccessor, IPackagingService packagingService)
public StarterKitInstallStep(IUmbracoApplicationLifetime umbracoApplicationLifetime, IWebSecurity webSecurity, IPackagingService packagingService)
{
_umbracoApplicationLifetime = umbracoApplicationLifetime;
_umbracoContextAccessor = umbracoContextAccessor;
_webSecurity = webSecurity;
_packagingService = packagingService;
}
@@ -47,7 +48,7 @@ namespace Umbraco.Web.Install.InstallSteps
var packageFile = new FileInfo(definition.PackagePath);
_packagingService.InstallCompiledPackageData(definition, packageFile, _umbracoContextAccessor.UmbracoContext.Security.GetUserId().ResultOr(-1));
_packagingService.InstallCompiledPackageData(definition, packageFile, _webSecurity.GetUserId().ResultOr(-1));
}
public override bool RequiresExecution(object model)

View File

@@ -15,7 +15,7 @@ using Umbraco.Web.Common.Filters;
using Umbraco.Web.Editors;
using Umbraco.Web.Features;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Trees;
using Umbraco.Web.Security;
using Umbraco.Web.WebAssets;
using Constants = Umbraco.Core.Constants;
@@ -28,7 +28,7 @@ namespace Umbraco.Web.BackOffice.Controllers
private readonly UmbracoFeatures _features;
private readonly IGlobalSettings _globalSettings;
private readonly IPublishedSnapshotService _publishedSnapshotService;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebSecurity _webSecurity;
private readonly ILocalizationService _localizationService;
private readonly IUmbracoVersion _umbracoVersion;
private readonly IContentSettings _contentSettings;
@@ -44,7 +44,7 @@ namespace Umbraco.Web.BackOffice.Controllers
UmbracoFeatures features,
IGlobalSettings globalSettings,
IPublishedSnapshotService publishedSnapshotService,
IUmbracoContextAccessor umbracoContextAccessor,
IWebSecurity webSecurity,
ILocalizationService localizationService,
IUmbracoVersion umbracoVersion,
IContentSettings contentSettings,
@@ -59,7 +59,7 @@ namespace Umbraco.Web.BackOffice.Controllers
_features = features;
_globalSettings = globalSettings;
_publishedSnapshotService = publishedSnapshotService;
_umbracoContextAccessor = umbracoContextAccessor;
_webSecurity = webSecurity;
_localizationService = localizationService;
_umbracoVersion = umbracoVersion;
_contentSettings = contentSettings ?? throw new ArgumentNullException(nameof(contentSettings));
@@ -112,7 +112,7 @@ namespace Umbraco.Web.BackOffice.Controllers
[UmbracoAuthorize]
public ActionResult Frame(int id, string culture)
{
var user = _umbracoContextAccessor.UmbracoContext.Security.CurrentUser;
var user = _webSecurity.CurrentUser;
var previewToken = _publishedSnapshotService.EnterPreview(user, id);

View File

@@ -8,6 +8,7 @@ using Umbraco.Core.Hosting;
using Umbraco.Core.Services;
using Umbraco.Web.Common.Attributes;
using Umbraco.Web.Models;
using Umbraco.Web.Security;
using Umbraco.Web.Tour;
namespace Umbraco.Web.BackOffice.Controllers
@@ -18,21 +19,21 @@ namespace Umbraco.Web.BackOffice.Controllers
private readonly TourFilterCollection _filters;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ITourSettings _tourSettings;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebSecurity _webSecurity;
private readonly IContentTypeService _contentTypeService;
public TourController(
TourFilterCollection filters,
IHostingEnvironment hostingEnvironment,
ITourSettings tourSettings,
IUmbracoContextAccessor umbracoContextAccessor,
IWebSecurity webSecurity,
IContentTypeService contentTypeService)
{
_filters = filters;
_hostingEnvironment = hostingEnvironment;
_tourSettings = tourSettings;
_umbracoContextAccessor = umbracoContextAccessor;
_webSecurity = webSecurity;
_contentTypeService = contentTypeService;
}
@@ -43,7 +44,7 @@ namespace Umbraco.Web.BackOffice.Controllers
if (_tourSettings.EnableTours == false)
return result;
var user = _umbracoContextAccessor.UmbracoContext.Security.CurrentUser;
var user = _webSecurity.CurrentUser;
if (user == null)
return result;
@@ -185,7 +186,7 @@ namespace Umbraco.Web.BackOffice.Controllers
var backOfficeTours = tours.Where(x =>
aliasFilters.Count == 0 || aliasFilters.All(filter => filter.IsMatch(x.Alias)) == false);
var user = _umbracoContextAccessor.UmbracoContext.Security.CurrentUser;
var user = _webSecurity.CurrentUser;
var localizedTours = backOfficeTours.Where(x =>
string.IsNullOrWhiteSpace(x.Culture) || x.Culture.Equals(user.Language,

View File

@@ -22,7 +22,7 @@ namespace Umbraco.Web.Common.Install
[Area(Umbraco.Core.Constants.Web.Mvc.InstallArea)]
public class InstallController : Controller
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebSecurity _webSecurity;
private readonly InstallHelper _installHelper;
private readonly IRuntimeState _runtime;
private readonly IGlobalSettings _globalSettings;
@@ -33,7 +33,7 @@ namespace Umbraco.Web.Common.Install
private readonly IRuntimeMinifier _runtimeMinifier;
public InstallController(
IUmbracoContextAccessor umbracoContextAccessor,
IWebSecurity webSecurity,
InstallHelper installHelper,
IRuntimeState runtime,
IGlobalSettings globalSettings,
@@ -43,7 +43,7 @@ namespace Umbraco.Web.Common.Install
ILogger logger,
LinkGenerator linkGenerator)
{
_umbracoContextAccessor = umbracoContextAccessor;
_webSecurity = webSecurity;
_installHelper = installHelper;
_runtime = runtime;
_globalSettings = globalSettings;
@@ -69,7 +69,7 @@ namespace Umbraco.Web.Common.Install
// Update ClientDependency version and delete its temp directories to make sure we get fresh caches
_runtimeMinifier.Reset();
var result = _umbracoContextAccessor.UmbracoContext.Security.ValidateCurrentUser(false);
var result = _webSecurity.ValidateCurrentUser(false);
switch (result)
{

View File

@@ -2,6 +2,7 @@
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Security;
namespace Umbraco.Web.Editors.Filters
{
@@ -10,7 +11,7 @@ namespace Umbraco.Web.Editors.Filters
/// </summary>
internal class ContentSaveModelValidator : ContentModelValidator<IContent, ContentItemSave, ContentVariantSave>
{
public ContentSaveModelValidator(ILogger logger, IUmbracoContextAccessor umbracoContextAccessor, ILocalizedTextService textService) : base(logger, umbracoContextAccessor, textService)
public ContentSaveModelValidator(ILogger logger, IWebSecurity webSecurity, ILocalizedTextService textService) : base(logger, webSecurity, textService)
{
}
}

View File

@@ -25,19 +25,19 @@ namespace Umbraco.Web.Editors.Filters
internal sealed class ContentSaveValidationAttribute : ActionFilterAttribute
{
private readonly ILogger _logger;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebSecurity _webSecurity;
private readonly ILocalizedTextService _textService;
private readonly IContentService _contentService;
private readonly IUserService _userService;
private readonly IEntityService _entityService;
public ContentSaveValidationAttribute(): this(Current.Logger, Current.UmbracoContextAccessor, Current.Services.TextService, Current.Services.ContentService, Current.Services.UserService, Current.Services.EntityService)
public ContentSaveValidationAttribute(): this(Current.Logger, Current.UmbracoContextAccessor.UmbracoContext.Security, Current.Services.TextService, Current.Services.ContentService, Current.Services.UserService, Current.Services.EntityService)
{ }
public ContentSaveValidationAttribute(ILogger logger, IUmbracoContextAccessor umbracoContextAccessor, ILocalizedTextService textService, IContentService contentService, IUserService userService, IEntityService entityService)
public ContentSaveValidationAttribute(ILogger logger, IWebSecurity webSecurity, ILocalizedTextService textService, IContentService contentService, IUserService userService, IEntityService entityService)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor));
_webSecurity = webSecurity ?? throw new ArgumentNullException(nameof(webSecurity));
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
_contentService = contentService ?? throw new ArgumentNullException(nameof(contentService));
_userService = userService ?? throw new ArgumentNullException(nameof(userService));
@@ -47,11 +47,11 @@ namespace Umbraco.Web.Editors.Filters
public override void OnActionExecuting(HttpActionContext actionContext)
{
var model = (ContentItemSave)actionContext.ActionArguments["contentItem"];
var contentItemValidator = new ContentSaveModelValidator(_logger, _umbracoContextAccessor, _textService);
var contentItemValidator = new ContentSaveModelValidator(_logger, _webSecurity, _textService);
if (!ValidateAtLeastOneVariantIsBeingSaved(model, actionContext)) return;
if (!contentItemValidator.ValidateExistingContent(model, actionContext)) return;
if (!ValidateUserAccess(model, actionContext, _umbracoContextAccessor.UmbracoContext.Security)) return;
if (!ValidateUserAccess(model, actionContext, _webSecurity)) return;
//validate for each variant that is being updated
foreach (var variant in model.Variants.Where(x => x.Save))

View File

@@ -9,6 +9,7 @@ using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Composing;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Security;
using Umbraco.Web.WebApi;
namespace Umbraco.Web.Editors.Filters
@@ -19,19 +20,19 @@ namespace Umbraco.Web.Editors.Filters
internal class MediaItemSaveValidationAttribute : ActionFilterAttribute
{
private readonly ILogger _logger;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebSecurity _webSecurity;
private readonly ILocalizedTextService _textService;
private readonly IMediaService _mediaService;
private readonly IEntityService _entityService;
public MediaItemSaveValidationAttribute() : this(Current.Logger, Current.UmbracoContextAccessor, Current.Services.TextService, Current.Services.MediaService, Current.Services.EntityService)
public MediaItemSaveValidationAttribute() : this(Current.Logger, Current.UmbracoContextAccessor.UmbracoContext.Security, Current.Services.TextService, Current.Services.MediaService, Current.Services.EntityService)
{
}
public MediaItemSaveValidationAttribute(ILogger logger, IUmbracoContextAccessor umbracoContextAccessor, ILocalizedTextService textService, IMediaService mediaService, IEntityService entityService)
public MediaItemSaveValidationAttribute(ILogger logger, IWebSecurity webSecurity, ILocalizedTextService textService, IMediaService mediaService, IEntityService entityService)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor));
_webSecurity = webSecurity ?? throw new ArgumentNullException(nameof(webSecurity));
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
_mediaService = mediaService ?? throw new ArgumentNullException(nameof(mediaService));
_entityService = entityService ?? throw new ArgumentNullException(nameof(entityService));
@@ -40,7 +41,7 @@ namespace Umbraco.Web.Editors.Filters
public override void OnActionExecuting(HttpActionContext actionContext)
{
var model = (MediaItemSave)actionContext.ActionArguments["contentItem"];
var contentItemValidator = new MediaSaveModelValidator(_logger, _umbracoContextAccessor, _textService);
var contentItemValidator = new MediaSaveModelValidator(_logger, _webSecurity, _textService);
if (ValidateUserAccess(model, actionContext))
{
@@ -90,7 +91,7 @@ namespace Umbraco.Web.Editors.Filters
if (MediaController.CheckPermissions(
actionContext.Request.Properties,
_umbracoContextAccessor.UmbracoContext.Security.CurrentUser,
_webSecurity.CurrentUser,
_mediaService, _entityService,
contentIdToCheck, contentToCheck) == false)
{

View File

@@ -2,6 +2,7 @@
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Security;
namespace Umbraco.Web.Editors.Filters
{
@@ -10,7 +11,7 @@ namespace Umbraco.Web.Editors.Filters
/// </summary>
internal class MediaSaveModelValidator : ContentModelValidator<IMedia, MediaItemSave, IContentProperties<ContentPropertyBasic>>
{
public MediaSaveModelValidator(ILogger logger, IUmbracoContextAccessor umbracoContextAccessor, ILocalizedTextService textService) : base(logger, umbracoContextAccessor, textService)
public MediaSaveModelValidator(ILogger logger, IWebSecurity webSecurity, ILocalizedTextService textService) : base(logger, webSecurity, textService)
{
}
}

View File

@@ -6,6 +6,7 @@ using Umbraco.Core.Services;
using Umbraco.Core.Strings;
using Umbraco.Web.Composing;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Security;
namespace Umbraco.Web.Editors.Filters
{
@@ -15,20 +16,20 @@ namespace Umbraco.Web.Editors.Filters
internal class MemberSaveValidationAttribute : ActionFilterAttribute
{
private readonly ILogger _logger;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IWebSecurity _webSecurity;
private readonly ILocalizedTextService _textService;
private readonly IMemberTypeService _memberTypeService;
private readonly IMemberService _memberService;
private readonly IShortStringHelper _shortStringHelper;
public MemberSaveValidationAttribute()
: this(Current.Logger, Current.UmbracoContextAccessor, Current.Services.TextService, Current.Services.MemberTypeService, Current.Services.MemberService, Current.ShortStringHelper)
: this(Current.Logger, Current.UmbracoContextAccessor.UmbracoContext.Security, Current.Services.TextService, Current.Services.MemberTypeService, Current.Services.MemberService, Current.ShortStringHelper)
{ }
public MemberSaveValidationAttribute(ILogger logger, IUmbracoContextAccessor umbracoContextAccessor, ILocalizedTextService textService, IMemberTypeService memberTypeService, IMemberService memberService, IShortStringHelper shortStringHelper)
public MemberSaveValidationAttribute(ILogger logger, IWebSecurity webSecurity, ILocalizedTextService textService, IMemberTypeService memberTypeService, IMemberService memberService, IShortStringHelper shortStringHelper)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor));
_webSecurity = webSecurity ?? throw new ArgumentNullException(nameof(webSecurity));
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
_memberTypeService = memberTypeService ?? throw new ArgumentNullException(nameof(memberTypeService));
_memberService = memberService ?? throw new ArgumentNullException(nameof(memberService));
@@ -38,7 +39,7 @@ namespace Umbraco.Web.Editors.Filters
public override void OnActionExecuting(HttpActionContext actionContext)
{
var model = (MemberSave)actionContext.ActionArguments["contentItem"];
var contentItemValidator = new MemberSaveModelValidator(_logger, _umbracoContextAccessor,_textService, _memberTypeService, _memberService, _shortStringHelper);
var contentItemValidator = new MemberSaveModelValidator(_logger, _webSecurity, _textService, _memberTypeService, _memberService, _shortStringHelper);
//now do each validation step
if (contentItemValidator.ValidateExistingContent(model, actionContext))
if (contentItemValidator.ValidateProperties(model, model, actionContext))