Files
BeWoPlaner/Service/ServiceBehavior/AdminMultitenancyContextInitializer.cs

73 lines
2.1 KiB
C#
Raw Normal View History

2025-02-20 18:39:11 +01:00
using BeWo.Data;
2025-06-03 15:46:41 +02:00
using BeWo.Service.Attributes;
2025-02-20 18:39:11 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using System.ServiceModel.Dispatcher;
using BeWo.Service.Core;
2025-02-21 16:09:11 +01:00
using BS.Shared.Extensions;
using BeWo.Service.ServiceUtils;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
2025-05-27 10:32:21 +02:00
using DevExpress.CodeParser;
2025-02-20 18:39:11 +01:00
namespace BeWo.Service.ServiceBehavior
{
public class AdminMultitenancyContextInitializer : ICallContextInitializer
{
public void AfterInvoke(object pCorrelationState)
{
if (pCorrelationState is MultitenancyOperationContextExt)
{
OperationContext.Current.Extensions.Remove((MultitenancyOperationContextExt)pCorrelationState);
}
}
public object BeforeInvoke(InstanceContext pInstanceContext, IClientChannel pChannel, Message pMessage)
{
string lTenant = null;
2025-05-27 10:32:21 +02:00
var (lInterface, lMethod) = ServiceHelper.GetInterfaceAndMethodASP(pMessage, pChannel);
2025-02-25 12:55:41 +01:00
var lTemp = ServiceHelper.GetCustomAttribute<RequireNoTenantAttribute>(lInterface, lMethod);
if (lTemp is object)
2025-02-20 18:39:11 +01:00
{
return null;
}
2025-02-21 16:09:11 +01:00
2025-02-25 12:55:41 +01:00
// Hole tenant aus Query
// Wird erstmal nicht benötigt. Per Header besser
// Ungefähr auf einem Level wie Username
//if (pMessage.Properties?.Via is Uri uri)
//{
// var query = HttpUtility.ParseQueryString(uri.Query);
// var tenant = query["tenant"];
2025-02-20 18:39:11 +01:00
2025-02-25 12:55:41 +01:00
// lTenant = tenant;
//}
2025-02-20 18:39:11 +01:00
// Hole tenant aus Header
if (string.IsNullOrWhiteSpace(lTenant))
{
2025-02-25 12:55:41 +01:00
var prop = (HttpRequestMessageProperty)pMessage.Properties[HttpRequestMessageProperty.Name];
lTenant = prop?.Headers["tenant"];
2025-02-20 18:39:11 +01:00
}
// Wenn kein Tenant gefunden wird,
if (string.IsNullOrWhiteSpace(lTenant))
2025-02-20 18:39:11 +01:00
{
throw new ArgumentException($"Zugriff verweigert. Tenant (\'{lTenant}\') darf nicht leer sein.", nameof(lTenant));
2025-02-20 18:39:11 +01:00
}
var lExt = new MultitenancyOperationContextExt { Tenant = lTenant };
OperationContext.Current.Extensions.Add(lExt);
return lExt;
2025-02-20 18:39:11 +01:00
}
}
}