diff --git a/src/OrchardCore.Modules/OrchardCore.Apis.GraphQL/GraphQLMiddleware.cs b/src/OrchardCore.Modules/OrchardCore.Apis.GraphQL/GraphQLMiddleware.cs index a7a2a14dcf7..257f1e2d350 100644 --- a/src/OrchardCore.Modules/OrchardCore.Apis.GraphQL/GraphQLMiddleware.cs +++ b/src/OrchardCore.Modules/OrchardCore.Apis.GraphQL/GraphQLMiddleware.cs @@ -158,6 +158,7 @@ private async Task ExecuteAsync(HttpContext context) options.OperationName = request.OperationName; options.Variables = request.Variables; options.UserContext = _settings.BuildUserContext?.Invoke(context); + options.User = context.User; options.ValidationRules = DocumentValidator.CoreRules .Concat(context.RequestServices.GetServices()) .Append(new ComplexityValidationRule(new ComplexityOptions diff --git a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/ContentItemQuery.cs b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/ContentItemQuery.cs index 671651ba72d..a654e47f018 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/ContentItemQuery.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/ContentItemQuery.cs @@ -2,25 +2,19 @@ using GraphQL.Resolvers; using GraphQL.Types; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using OrchardCore.Apis.GraphQL; using OrchardCore.ContentManagement.GraphQL.Queries.Types; -using ContentsCommonPermissions = OrchardCore.Contents.CommonPermissions; namespace OrchardCore.ContentManagement.GraphQL.Queries; public sealed class ContentItemQuery : ISchemaBuilder { - private readonly IHttpContextAccessor _httpContextAccessor; - internal readonly IStringLocalizer S; - public ContentItemQuery(IHttpContextAccessor httpContextAccessor, - IStringLocalizer localizer) + public ContentItemQuery(IStringLocalizer localizer) { - _httpContextAccessor = httpContextAccessor; S = localizer; } @@ -53,10 +47,9 @@ public Task BuildAsync(ISchema schema) private async ValueTask ResolveAsync(IResolveFieldContext context) { - var httpContext = _httpContextAccessor.HttpContext; var contentItemId = context.GetArgument("contentItemId"); - var contentManager = httpContext.RequestServices.GetRequiredService(); - var authorizationService = httpContext.RequestServices.GetRequiredService(); + var contentManager = context.RequestServices.GetService(); + var authorizationService = context.RequestServices.GetService(); var contentItem = await contentManager.GetAsync(contentItemId); @@ -65,8 +58,9 @@ private async ValueTask ResolveAsync(IResolveFieldContext context) return null; } - if (!await authorizationService.AuthorizeAsync(httpContext.User, ContentsCommonPermissions.ViewContent, contentItem)) + if (!await authorizationService.AuthorizeAsync(context.User, Contents.CommonPermissions.ViewContent, contentItem)) { + // Return null if the user doesn't have permission to view the content item, so that it doesn't appear in the GraphQL response. return null; }