modifies some code changes to null check and not to use extension methods that require singleton access.

This commit is contained in:
Shannon
2017-12-21 17:30:37 +11:00
parent c6daf2e111
commit e4561851a4

View File

@@ -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<IUserService>(() => applicationContext.Services.UserService))))
new ActionButtonsResolver(new Lazy<IUserService>(() => applicationContext.Services.UserService), new Lazy<IContentService>(() => 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<IContent, IEnumerable<string>>
{
private readonly Lazy<IUserService> _userService;
private readonly Lazy<IContentService> _contentService;
public ActionButtonsResolver(Lazy<IUserService> userService)
public ActionButtonsResolver(Lazy<IUserService> userService, Lazy<IContentService> contentService)
{
if (userService == null) throw new ArgumentNullException("userService");
if (contentService == null) throw new ArgumentNullException("contentService");
_userService = userService;
_contentService = contentService;
}
protected override IEnumerable<string> 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;