Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

60 lines
1.8 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Threading.Tasks;
using Squidex.Domain.Apps.Entities;
using Squidex.Domain.Apps.Entities.Apps;
using Squidex.Domain.Apps.Entities.Apps.Commands;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Commands;
namespace Squidex.Web.CommandMiddlewares
{
public sealed class EnrichWithAppIdCommandMiddleware : ICommandMiddleware
{
private readonly IContextProvider contextProvider;
public EnrichWithAppIdCommandMiddleware(IContextProvider contextProvider)
{
Guard.NotNull(contextProvider, nameof(contextProvider));
this.contextProvider = contextProvider;
}
public Task HandleAsync(CommandContext context, NextDelegate next)
{
if (context.Command is IAppCommand appCommand && appCommand.AppId == null)
{
var appId = GetAppId();
appCommand.AppId = appId;
}
if (context.Command is AppCommand appSelfCommand && appSelfCommand.AppId == Guid.Empty)
{
var appId = GetAppId();
appSelfCommand.AppId = appId.Id;
}
return next(context);
}
private NamedId<Guid> GetAppId()
{
var context = contextProvider.Context;
if (context.App == null)
{
throw new InvalidOperationException("Cannot resolve app.");
}
return context.App.NamedId();
}
}
}