|
1 |
| -using Umbraco.Cms.Core; |
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Umbraco.Cms.Core; |
2 | 3 | using Umbraco.Cms.Core.Models;
|
3 | 4 | using Umbraco.Cms.Core.Models.Entities;
|
4 | 5 | using Umbraco.Cms.Core.Services;
|
5 | 6 | using Umbraco.Cms.Api.Management.Models.Entities;
|
| 7 | +using Umbraco.Cms.Core.DependencyInjection; |
| 8 | +using Umbraco.Cms.Core.Persistence.Querying; |
| 9 | +using Umbraco.Cms.Core.Scoping; |
6 | 10 | using Umbraco.Extensions;
|
7 | 11 |
|
8 | 12 | namespace Umbraco.Cms.Api.Management.Services.Entities;
|
9 | 13 |
|
10 | 14 | public class UserStartNodeEntitiesService : IUserStartNodeEntitiesService
|
11 | 15 | {
|
12 | 16 | private readonly IEntityService _entityService;
|
| 17 | + private readonly ICoreScopeProvider _scopeProvider; |
| 18 | + private readonly IIdKeyMap _idKeyMap; |
13 | 19 |
|
14 |
| - public UserStartNodeEntitiesService(IEntityService entityService) => _entityService = entityService; |
| 20 | + [Obsolete("Please use the non-obsolete constructor. Scheduled for removal in V17.")] |
| 21 | + public UserStartNodeEntitiesService(IEntityService entityService) |
| 22 | + : this( |
| 23 | + entityService, |
| 24 | + StaticServiceProvider.Instance.GetRequiredService<ICoreScopeProvider>(), |
| 25 | + StaticServiceProvider.Instance.GetRequiredService<IIdKeyMap>()) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + public UserStartNodeEntitiesService(IEntityService entityService, ICoreScopeProvider scopeProvider, IIdKeyMap idKeyMap) |
| 30 | + { |
| 31 | + _entityService = entityService; |
| 32 | + _scopeProvider = scopeProvider; |
| 33 | + _idKeyMap = idKeyMap; |
| 34 | + } |
15 | 35 |
|
16 | 36 | /// <inheritdoc />
|
17 | 37 | public IEnumerable<UserAccessEntity> RootUserAccessEntities(UmbracoObjectTypes umbracoObjectType, int[] userStartNodeIds)
|
@@ -43,6 +63,54 @@ public IEnumerable<UserAccessEntity> RootUserAccessEntities(UmbracoObjectTypes u
|
43 | 63 | .ToArray();
|
44 | 64 | }
|
45 | 65 |
|
| 66 | + public IEnumerable<UserAccessEntity> ChildUserAccessEntities(UmbracoObjectTypes umbracoObjectType, string[] userStartNodePaths, Guid parentKey, int skip, int take, Ordering ordering, out long totalItems) |
| 67 | + { |
| 68 | + Attempt<int> parentIdAttempt = _idKeyMap.GetIdForKey(parentKey, umbracoObjectType); |
| 69 | + if (parentIdAttempt.Success is false) |
| 70 | + { |
| 71 | + totalItems = 0; |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + var parentId = parentIdAttempt.Result; |
| 76 | + IEntitySlim? parent = _entityService.Get(parentId); |
| 77 | + if (parent is null) |
| 78 | + { |
| 79 | + totalItems = 0; |
| 80 | + return []; |
| 81 | + } |
| 82 | + |
| 83 | + IEntitySlim[] children; |
| 84 | + if (userStartNodePaths.Any(path => $"{parent.Path},".StartsWith($"{path},"))) |
| 85 | + { |
| 86 | + // the requested parent is one of the user start nodes (or a descendant of one), all children are by definition allowed |
| 87 | + children = _entityService.GetPagedChildren(parentKey, umbracoObjectType, skip, take, out totalItems, ordering: ordering).ToArray(); |
| 88 | + return ChildUserAccessEntities(children, userStartNodePaths); |
| 89 | + } |
| 90 | + |
| 91 | + // if one or more of the user start nodes are descendants of the requested parent, find the "next child IDs" in those user start node paths |
| 92 | + // - e.g. given the user start node path "-1,2,3,4,5", if the requested parent ID is 3, the "next child ID" is 4. |
| 93 | + var userStartNodePathIds = userStartNodePaths.Select(path => path.Split(Constants.CharArrays.Comma).Select(int.Parse).ToArray()).ToArray(); |
| 94 | + var allowedChildIds = userStartNodePathIds |
| 95 | + .Where(ids => ids.Contains(parentId)) |
| 96 | + // given the previous checks, the parent ID can never be the last in the user start node path, so this is safe |
| 97 | + .Select(ids => ids[ids.IndexOf(parentId) + 1]) |
| 98 | + .Distinct() |
| 99 | + .ToArray(); |
| 100 | + |
| 101 | + totalItems = allowedChildIds.Length; |
| 102 | + if (allowedChildIds.Length == 0) |
| 103 | + { |
| 104 | + // the requested parent is outside the scope of any user start nodes |
| 105 | + return []; |
| 106 | + } |
| 107 | + |
| 108 | + // even though we know the IDs of the allowed child entities to fetch, we still use a Query to yield correctly sorted children |
| 109 | + IQuery<IUmbracoEntity> query = _scopeProvider.CreateQuery<IUmbracoEntity>().Where(x => allowedChildIds.Contains(x.Id)); |
| 110 | + children = _entityService.GetPagedChildren(parentKey, umbracoObjectType, skip, take, out totalItems, query, ordering).ToArray(); |
| 111 | + return ChildUserAccessEntities(children, userStartNodePaths); |
| 112 | + } |
| 113 | + |
46 | 114 | /// <inheritdoc />
|
47 | 115 | public IEnumerable<UserAccessEntity> ChildUserAccessEntities(IEnumerable<IEntitySlim> candidateChildren, string[] userStartNodePaths)
|
48 | 116 | // child entities for users without root access should include:
|
|
0 commit comments