22using GraphQL . Resolvers ;
33using GraphQL . Types ;
44using Microsoft . AspNetCore . Authorization ;
5- using Microsoft . AspNetCore . Http ;
65using Microsoft . Extensions . DependencyInjection ;
76using Microsoft . Extensions . Localization ;
87using OrchardCore . Apis . GraphQL ;
98using OrchardCore . ContentManagement . GraphQL . Queries . Types ;
10- using ContentsCommonPermissions = OrchardCore . Contents . CommonPermissions ;
119
1210namespace OrchardCore . ContentManagement . GraphQL . Queries ;
1311
1412public sealed class ContentItemQuery : ISchemaBuilder
1513{
16- private readonly IHttpContextAccessor _httpContextAccessor ;
17-
1814 internal readonly IStringLocalizer S ;
1915
20- public ContentItemQuery ( IHttpContextAccessor httpContextAccessor ,
21- IStringLocalizer < ContentItemQuery > localizer )
16+ public ContentItemQuery ( IStringLocalizer < ContentItemQuery > localizer )
2217 {
23- _httpContextAccessor = httpContextAccessor ;
2418 S = localizer ;
2519 }
2620
@@ -53,10 +47,9 @@ public Task BuildAsync(ISchema schema)
5347
5448 private async ValueTask < ContentItem > ResolveAsync ( IResolveFieldContext context )
5549 {
56- var httpContext = _httpContextAccessor . HttpContext ;
5750 var contentItemId = context . GetArgument < string > ( "contentItemId" ) ;
58- var contentManager = httpContext . RequestServices . GetRequiredService < IContentManager > ( ) ;
59- var authorizationService = httpContext . RequestServices . GetRequiredService < IAuthorizationService > ( ) ;
51+ var contentManager = context . RequestServices . GetService < IContentManager > ( ) ;
52+ var authorizationService = context . RequestServices . GetService < IAuthorizationService > ( ) ;
6053
6154 var contentItem = await contentManager . GetAsync ( contentItemId ) ;
6255
@@ -65,8 +58,9 @@ private async ValueTask<ContentItem> ResolveAsync(IResolveFieldContext context)
6558 return null ;
6659 }
6760
68- if ( ! await authorizationService . AuthorizeAsync ( httpContext . User , ContentsCommonPermissions . ViewContent , contentItem ) )
61+ if ( ! await authorizationService . AuthorizeAsync ( context . User , Contents . CommonPermissions . ViewContent , contentItem ) )
6962 {
63+ // Return null if the user doesn't have permission to view the content item, so that it doesn't appear in the GraphQL response.
7064 return null ;
7165 }
7266
0 commit comments