From e4561851a42e92838ebf7ea1303e3c4c30810a32 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 21 Dec 2017 17:30:37 +1100 Subject: [PATCH] modifies some code changes to null check and not to use extension methods that require singleton access. --- .../Models/Mapping/ContentModelMapper.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs index 5c4e263a52..1d222ccd1a 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs @@ -52,7 +52,7 @@ namespace Umbraco.Web.Models.Mapping .ForMember(display => display.Alias, expression => expression.Ignore()) .ForMember(display => display.Tabs, expression => expression.ResolveUsing(new TabsAndPropertiesResolver(applicationContext.Services.TextService))) .ForMember(display => display.AllowedActions, expression => expression.ResolveUsing( - new ActionButtonsResolver(new Lazy(() => applicationContext.Services.UserService)))) + new ActionButtonsResolver(new Lazy(() => applicationContext.Services.UserService), new Lazy(() => applicationContext.Services.ContentService)))) .AfterMap((content, display) => AfterMap(content, display, applicationContext.Services.DataTypeService, applicationContext.Services.TextService, applicationContext.Services.ContentTypeService)); @@ -219,10 +219,14 @@ namespace Umbraco.Web.Models.Mapping private class ActionButtonsResolver : ValueResolver> { private readonly Lazy _userService; + private readonly Lazy _contentService; - public ActionButtonsResolver(Lazy userService) + public ActionButtonsResolver(Lazy userService, Lazy contentService) { + if (userService == null) throw new ArgumentNullException("userService"); + if (contentService == null) throw new ArgumentNullException("contentService"); _userService = userService; + _contentService = contentService; } protected override IEnumerable ResolveCore(IContent source) @@ -234,14 +238,21 @@ namespace Umbraco.Web.Models.Mapping } var svc = _userService.Value; + string path; + if (source.HasIdentity) + path = source.Path; + else + { + var parent = _contentService.Value.GetById(source.ParentId); + path = parent == null ? "-1" : parent.Path; + } + var permissions = svc.GetPermissionsForPath( //TODO: This is certainly not ideal usage here - perhaps the best way to deal with this in the future is // with the IUmbracoContextAccessor. In the meantime, if used outside of a web app this will throw a null // refrence exception :( UmbracoContext.Current.Security.CurrentUser, - // Here we need to do a special check since this could be new content, in which case we need to get the permissions - // from the parent, not the existing one otherwise permissions would be coming from the root since Id is 0. - source.HasIdentity ? source.Path : source.Parent().Path) + path) .GetAllPermissions(); return permissions;