public TenantManagementJobsRunner(
     IJobInfoFinder jobInfoFinder,
     TraffkGlobalDbContext gdb,
     Serilog.ILogger logger,
     TraffkTenantShardsDbContext tdb,
     TraffkTenantShardManagerDbContext smdb,
     IPasswordHasher <ApplicationUser> passwordHasher,
     ServiceClientCredentialFactory credentialFactory,
     IOptions <Config> configOptions,
     DbContextOptions <TraffkTenantModelDbContext> rdbOptions)
     : base(gdb, jobInfoFinder, logger)
 {
     CredentialFactory = credentialFactory;
     ConfigOptions     = configOptions;
     RdbOptions        = rdbOptions;
     Smdb           = smdb;
     Tdb            = tdb;
     PasswordHasher = passwordHasher;
 }
Esempio n. 2
0
        public TenantFinderService(IOptions <Config> configOptions, ICacher cacher, TraffkTenantShardsDbContext db, IHttpContextAccessor acc)
        {
            var         config = configOptions.Value;
            AppHostItem z      = null;

#if DEBUG
            PreferredHostname = ActualHostname = StringHelpers.Coalesce(config?.Hostname, acc.HttpContext?.Request?.Host.Host);
#else
            PreferredHostname = ActualHostname = acc.HttpContext?.Request.Host.Host;
#endif

            var defaultUniversalLoginPattern = config.UniversalLoginHostPath;
            if (StringHelpers.IsSameIgnoreCase(defaultUniversalLoginPattern, acc.HttpContext?.Request?.Path.Value))
            {
                var matchedTenant = db.TenantFindByTenantId(config.UniversalLoginTenantId).ExecuteSynchronously().FirstOrDefault();

                if (matchedTenant == null)
                {
                    throw new TenantFinderServiceException(TenantServiceExceptionCodes.TenantNotFound, $"host=[{ActualHostname}]");
                }

                z = new AppHostItem
                {
                    TenantId          = matchedTenant.TenantId,
                    PreferredHostname = ActualHostname,
                    HostDatabaseName  = matchedTenant.HostDatabaseName
                };
            }

            var defaultTenantFinderHostPattern = config.DefaultTenantFinderHostPattern;
            var defaultHostNamePattern         = defaultTenantFinderHostPattern.Replace(@"{0}", @"([^\.]*)");
            var defaultHostNameRegexPattern    = new Regex(defaultHostNamePattern, RegexOptions.Compiled);

            if (defaultHostNameRegexPattern.IsMatch(ActualHostname))
            {
                var m           = defaultHostNameRegexPattern.Match(ActualHostname);
                var loginDomain = m.Success ? m.Groups[1].Value : null;

                z = cacher.FindOrCreateValWithSimpleKey(
                    Cache.CreateKey("LoginDomain", loginDomain),
                    () =>
                {
                    var matchedTenant = db.AppFindByHostname(null, AppTypes.Portal, loginDomain).ExecuteSynchronously().FirstOrDefault();
                    if (matchedTenant == null)
                    {
                        return(null);
                    }
                    return(new AppHostItem
                    {
                        TenantId = matchedTenant.TenantId,
                        PreferredHostname = ActualHostname,
                        HostDatabaseName = matchedTenant.HostDatabaseName
                    });
                });
            }

            if (z == null)
            {
                z = cacher.FindOrCreateValWithSimpleKey(
                    Cache.CreateKey("ActualHostname", ActualHostname),
                    () => db.AppFindByHostname(ActualHostname, AppTypes.Portal).ExecuteSynchronously().FirstOrDefault()
                    );
            }

            if (z != null)
            {
                TenantId_p        = z.TenantId;
                PreferredHostname = z.PreferredHostname;
                DatabaseName      = z.HostDatabaseName;
            }

            acc.HttpContext.Items[HttpContextTenantIdKey] = TenantId_p;
        }
 public UniversalLoginController(TraffkTenantShardsDbContext db)
 {
     DB = db;
 }
 public MyTraffkTenantFinder(TraffkTenantShardsDbContext db)
 {
     DB = db;
 }