public TenantResolver(IMultiTenantStore multiTenantStore, IMultiTenantStrategy _multiTenantStrategy)
        {
            _multiTenantStore = multiTenantStore ??
                                throw new MultiTenantException(null, new ArgumentNullException(nameof(multiTenantStore)));

            this._multiTenantStrategy = _multiTenantStrategy ??
                                        throw new ArgumentNullException(nameof(_multiTenantStrategy));
        }
        public TenantResolver(IMultiTenantStore multiTenantStore, IMultiTenantStrategy multiTenantStrategy, ILogger <TenantResolver> logger = null)
        {
            this.multiTenantStore = multiTenantStore ??
                                    throw new MultiTenantException(null, new ArgumentNullException(nameof(multiTenantStore)));

            this.multiTenantStrategy = multiTenantStrategy ??
                                       throw new ArgumentNullException(nameof(TenantResolver.multiTenantStrategy));

            this.logger = logger;
        }
        private static void SetStrategyInfo(MultiTenantContext multiTenantContext, IMultiTenantStrategy strategy)
        {
            var strategyInfo = new StrategyInfo();

            strategyInfo.MultiTenantContext = multiTenantContext;
            strategyInfo.Strategy           = strategy;
            if (strategy.GetType().IsGenericType&&
                strategy.GetType().GetGenericTypeDefinition() == typeof(MultiTenantStrategyWrapper <>))
            {
                strategyInfo.StrategyType = strategy.GetType().GetGenericArguments().First();
            }
            else
            {
                strategyInfo.StrategyType = strategy.GetType();
            }
            multiTenantContext.StrategyInfo = strategyInfo;
        }
        public async Task InvokeAsync(HttpContext context)
        {
            // 多租户上下文设置到项集合中。
            if (!context.Items.ContainsKey(Constants.HttpContextMultiTenantContext))
            {
                var multiTenantContext = new MultiTenantContext();
                context.Items.Add(Constants.HttpContextMultiTenantContext, multiTenantContext);

                IMultiTenantStrategy strategy = null;
                string identifier             = null;

                // 取回IMultiTenantStrategy实例 循环 直到取到租户结束
                foreach (var strat in context.RequestServices.GetServices <IMultiTenantStrategy>())
                {
                    // 取回租户ID
                    identifier = await strat.GetIdentifierAsync(context);

                    if (identifier != null)
                    {
                        strategy = strat;
                        break;
                    }
                }

                var        store      = context.RequestServices.GetRequiredService <IMultiTenantStore>();
                TenantInfo tenantInfo = null;
                if (identifier != null)
                {
                    SetStrategyInfo(multiTenantContext, strategy);
                    tenantInfo = await store.TryGetByCodeAsync(identifier);
                }
                // 解决远程身份验证回调(如果适用)。
                if (tenantInfo == null)
                {
                    strategy = context.RequestServices.GetService <RemoteAuthenticationStrategy>();

                    if (strategy != null)
                    {
                        identifier = await strategy.GetIdentifierAsync(context);

                        if (identifier != null)
                        {
                            SetStrategyInfo(multiTenantContext, strategy);
                            tenantInfo = await store.TryGetByIdAsync(identifier);
                        }
                    }
                }

                // Finally try the fallback identifier, if applicable.
                // 最后,如果适用,尝试fallback。
                if (tenantInfo == null)
                {
                    //strategy = context.RequestServices.GetService<FallbackStrategy>();
                    strategy = context.RequestServices.GetService <StaticStrategy>();

                    if (strategy != null)
                    {
                        identifier = await strategy.GetIdentifierAsync(context);

                        SetStrategyInfo(multiTenantContext, strategy);
                        tenantInfo = await store.TryGetByIdAsync(identifier);
                    }
                }

                if (tenantInfo != null)
                {
                    // 设置租户信息
                    multiTenantContext.TenantInfo = tenantInfo;
                    tenantInfo.MultiTenantContext = multiTenantContext;

                    // 设置租户存储信息
                    var storeInfo = new StoreInfo();
                    storeInfo.MultiTenantContext = multiTenantContext;
                    storeInfo.Store = store;
                    if (store.GetType().IsGenericType&&
                        store.GetType().GetGenericTypeDefinition() == typeof(MultiTenantStoreWrapper <>))
                    {
                        storeInfo.Store     = (IMultiTenantStore)store.GetType().GetProperty("Store").GetValue(store);
                        storeInfo.StoreType = store.GetType().GetGenericArguments().First();
                    }
                    else
                    {
                        storeInfo.Store     = store;
                        storeInfo.StoreType = store.GetType();
                    }
                    multiTenantContext.StoreInfo = storeInfo;
                }
            }

            if (next != null)
            {
                await next(context);
            }
        }
        public async Task Invoke(HttpContext context)
        {
            // Set the initial multitenant context into the Items collections.
            if (!context.Items.ContainsKey(Constants.HttpContextMultiTenantContext))
            {
                var multiTenantContext = new MultiTenantContext();
                context.Items.Add(Constants.HttpContextMultiTenantContext, multiTenantContext);

                IMultiTenantStrategy strategy = null;
                string identifier             = null;

                // Cycle through the strategies
                foreach (var strat in context.RequestServices.GetServices <IMultiTenantStrategy>())
                {
                    // Try the registered strategy.
                    identifier = await strat.GetIdentifierAsync(context);

                    if (identifier != null)
                    {
                        strategy = strat;
                        break;
                    }
                }

                var        store      = context.RequestServices.GetRequiredService <IMultiTenantStore>();
                TenantInfo tenantInfo = null;
                if (identifier != null)
                {
                    SetStrategyInfo(multiTenantContext, strategy);
                    tenantInfo = await store.TryGetByIdentifierAsync(identifier);
                }

                // Resolve for remote authentication callbacks if applicable.
                if (tenantInfo == null)
                {
                    strategy = context.RequestServices.GetService <RemoteAuthenticationStrategy>();

                    if (strategy != null)
                    {
                        identifier = await strategy.GetIdentifierAsync(context);

                        if (identifier != null)
                        {
                            SetStrategyInfo(multiTenantContext, strategy);
                            tenantInfo = await store.TryGetByIdentifierAsync(identifier);
                        }
                    }
                }

                // Finally try the fallback identifier, if applicable.
                if (tenantInfo == null)
                {
                    strategy = context.RequestServices.GetService <FallbackStrategy>();
                    if (strategy != null)
                    {
                        identifier = await strategy.GetIdentifierAsync(context);

                        SetStrategyInfo(multiTenantContext, strategy);
                        tenantInfo = await store.TryGetByIdentifierAsync(identifier);
                    }
                }

                if (tenantInfo != null)
                {
                    // Set the tenant info.
                    multiTenantContext.TenantInfo = tenantInfo;
                    tenantInfo.MultiTenantContext = multiTenantContext;

                    // Set the store info.
                    var storeInfo = new StoreInfo();
                    storeInfo.MultiTenantContext = multiTenantContext;
                    storeInfo.Store = store;
                    if (store.GetType().IsGenericType&&
                        store.GetType().GetGenericTypeDefinition() == typeof(MultiTenantStoreWrapper <>))
                    {
                        storeInfo.Store     = (IMultiTenantStore)store.GetType().GetProperty("Store").GetValue(store);
                        storeInfo.StoreType = store.GetType().GetGenericArguments().First();
                    }
                    else
                    {
                        storeInfo.Store     = store;
                        storeInfo.StoreType = store.GetType();
                    }
                    multiTenantContext.StoreInfo = storeInfo;
                }
            }

            if (next != null)
            {
                await next(context);
            }
        }
 public MultiTenantStrategyWrapper(IMultiTenantStrategy strategy, ILogger logger)
 {
     this.Strategy = strategy ?? throw new ArgumentNullException(nameof(strategy));
     this.logger   = logger ?? throw new ArgumentNullException(nameof(logger));
 }
Esempio n. 7
0
 public MultiTenantStrategyWrapper(IMultiTenantStrategy strategy, ILogger logger)
 {
     this.Strategy = strategy;
     this.logger   = logger;
 }