Exemple #1
0
        public string GetTenantDomain(CoreSettings coreSettings, bool allowMappedDomain = true)
        {
            var baseHost = coreSettings.GetBaseDomain(HostedRegion);

            if (string.IsNullOrEmpty(baseHost) && !string.IsNullOrEmpty(HostedRegion))
            {
                baseHost = HostedRegion;
            }

            string result;

            if (baseHost == "localhost" || TenantAlias == "localhost")
            {
                //single tenant on local host
                TenantAlias = "localhost";
                result      = HostName;
            }
            else
            {
                result = string.Format("{0}.{1}", TenantAlias, baseHost).TrimEnd('.').ToLowerInvariant();
            }
            if (!string.IsNullOrEmpty(MappedDomain) && allowMappedDomain)
            {
                if (MappedDomain.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))
                {
                    MappedDomain = MappedDomain.Substring(7);
                }
                if (MappedDomain.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
                {
                    MappedDomain = MappedDomain.Substring(8);
                }
                result = MappedDomain.ToLowerInvariant();
            }

            return(result);
        }
Exemple #2
0
        public Tenant SaveTenant(CoreSettings coreSettings, Tenant t)
        {
            if (t == null)
            {
                throw new ArgumentNullException("tenant");
            }

            using var tx = TenantDbContext.Database.BeginTransaction();

            if (!string.IsNullOrEmpty(t.MappedDomain))
            {
                var baseUrl = coreSettings.GetBaseDomain(t.HostedRegion);

                if (baseUrl != null && t.MappedDomain.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase))
                {
                    ValidateDomain(t.MappedDomain.Substring(0, t.MappedDomain.Length - baseUrl.Length - 1), t.TenantId, false);
                }
                else
                {
                    ValidateDomain(t.MappedDomain, t.TenantId, false);
                }
            }

            if (t.TenantId == Tenant.DEFAULT_TENANT)
            {
                t.Version = TenantDbContext.TenantVersion
                            .Where(r => r.DefaultVersion == 1 || r.Id == 0)
                            .OrderByDescending(r => r.Id)
                            .Select(r => r.Id)
                            .FirstOrDefault();

                var tenant = new DbTenant
                {
                    Id                    = t.TenantId,
                    Alias                 = t.TenantAlias.ToLowerInvariant(),
                    MappedDomain          = !string.IsNullOrEmpty(t.MappedDomain) ? t.MappedDomain.ToLowerInvariant() : null,
                    Version               = t.Version,
                    VersionChanged        = t.VersionChanged,
                    Name                  = t.Name ?? t.TenantAlias,
                    Language              = t.Language,
                    TimeZone              = t.TimeZone,
                    OwnerId               = t.OwnerId,
                    TrustedDomains        = t.GetTrustedDomains(),
                    TrustedDomainsEnabled = t.TrustedDomainsType,
                    CreationDateTime      = t.CreatedDateTime,
                    Status                = t.Status,
                    StatusChanged         = t.StatusChangeDate,
                    PaymentId             = t.PaymentId,
                    LastModified          = t.LastModified = DateTime.UtcNow,
                    Industry              = t.Industry,
                    Spam                  = t.Spam,
                    Calls                 = t.Calls
                };

                tenant = TenantDbContext.Tenants.Add(tenant).Entity;
                TenantDbContext.SaveChanges();
                t.TenantId = tenant.Id;
            }
            else
            {
                var tenant = TenantDbContext.Tenants
                             .Where(r => r.Id == t.TenantId)
                             .FirstOrDefault();

                if (tenant != null)
                {
                    tenant.Alias                 = t.TenantAlias.ToLowerInvariant();
                    tenant.MappedDomain          = !string.IsNullOrEmpty(t.MappedDomain) ? t.MappedDomain.ToLowerInvariant() : null;
                    tenant.Version               = t.Version;
                    tenant.VersionChanged        = t.VersionChanged;
                    tenant.Name                  = t.Name ?? t.TenantAlias;
                    tenant.Language              = t.Language;
                    tenant.TimeZone              = t.TimeZone;
                    tenant.TrustedDomains        = t.GetTrustedDomains();
                    tenant.TrustedDomainsEnabled = t.TrustedDomainsType;
                    tenant.CreationDateTime      = t.CreatedDateTime;
                    tenant.Status                = t.Status;
                    tenant.StatusChanged         = t.StatusChangeDate;
                    tenant.PaymentId             = t.PaymentId;
                    tenant.LastModified          = t.LastModified = DateTime.UtcNow;
                    tenant.Industry              = t.Industry;
                    tenant.Spam                  = t.Spam;
                    tenant.Calls                 = t.Calls;
                }
                TenantDbContext.SaveChanges();
            }

            if (string.IsNullOrEmpty(t.PartnerId) && string.IsNullOrEmpty(t.AffiliateId) && string.IsNullOrEmpty(t.Campaign))
            {
                var p = TenantDbContext.TenantPartner
                        .Where(r => r.TenantId == t.TenantId)
                        .FirstOrDefault();

                if (p != null)
                {
                    TenantDbContext.TenantPartner.Remove(p);
                }
            }
            else
            {
                var tenantPartner = new DbTenantPartner
                {
                    TenantId    = t.TenantId,
                    PartnerId   = t.PartnerId,
                    AffiliateId = t.AffiliateId,
                    Campaign    = t.Campaign
                };

                TenantDbContext.TenantPartner.Add(tenantPartner);
            }

            tx.Commit();

            //CalculateTenantDomain(t);
            return(t);
        }
Exemple #3
0
        public Tenant SaveTenant(CoreSettings coreSettings, Tenant t)
        {
            if (t == null)
            {
                throw new ArgumentNullException("tenant");
            }

            var db = GetDb();

            using (var tx = db.BeginTransaction())
            {
                if (!string.IsNullOrEmpty(t.MappedDomain))
                {
                    var baseUrl = coreSettings.GetBaseDomain(t.HostedRegion);

                    if (baseUrl != null && t.MappedDomain.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase))
                    {
                        ValidateDomain(db, t.MappedDomain.Substring(0, t.MappedDomain.Length - baseUrl.Length - 1), t.TenantId, false);
                    }
                    else
                    {
                        ValidateDomain(db, t.MappedDomain, t.TenantId, false);
                    }
                }

                if (t.TenantId == Tenant.DEFAULT_TENANT)
                {
                    var q = new SqlQuery("tenants_version")
                            .Select("id")
                            .Where(Exp.Eq("default_version", 1) | Exp.Eq("id", 0))
                            .OrderBy(1, false)
                            .SetMaxResults(1);
                    t.Version = db.ExecuteScalar <int>(q);

                    var i = new SqlInsert("tenants_tenants", true)
                            .InColumnValue("id", t.TenantId)
                            .InColumnValue("alias", t.TenantAlias.ToLowerInvariant())
                            .InColumnValue("mappeddomain", !string.IsNullOrEmpty(t.MappedDomain) ? t.MappedDomain.ToLowerInvariant() : null)
                            .InColumnValue("version", t.Version)
                            .InColumnValue("version_changed", t.VersionChanged)
                            .InColumnValue("name", t.Name ?? t.TenantAlias)
                            .InColumnValue("language", t.Language)
                            .InColumnValue("timezone", t.TimeZone.Id)
                            .InColumnValue("owner_id", t.OwnerId.ToString())
                            .InColumnValue("trusteddomains", t.GetTrustedDomains())
                            .InColumnValue("trusteddomainsenabled", (int)t.TrustedDomainsType)
                            .InColumnValue("creationdatetime", t.CreatedDateTime)
                            .InColumnValue("status", (int)t.Status)
                            .InColumnValue("statuschanged", t.StatusChangeDate)
                            .InColumnValue("payment_id", t.PaymentId)
                            .InColumnValue("last_modified", t.LastModified = DateTime.UtcNow)
                            .InColumnValue("industry", (int)t.Industry)
                            .InColumnValue("spam", t.Spam)
                            .InColumnValue("calls", t.Calls)
                            .Identity <int>(0, t.TenantId, true);


                    t.TenantId = db.ExecuteScalar <int>(i);
                }
                else
                {
                    var u = new SqlUpdate("tenants_tenants")
                            .Set("alias", t.TenantAlias.ToLowerInvariant())
                            .Set("mappeddomain", !string.IsNullOrEmpty(t.MappedDomain) ? t.MappedDomain.ToLowerInvariant() : null)
                            .Set("version", t.Version)
                            .Set("version_changed", t.VersionChanged)
                            .Set("name", t.Name ?? t.TenantAlias)
                            .Set("language", t.Language)
                            .Set("timezone", t.TimeZone.Id)
                            .Set("owner_id", t.OwnerId.ToString())
                            .Set("trusteddomains", t.GetTrustedDomains())
                            .Set("trusteddomainsenabled", (int)t.TrustedDomainsType)
                            .Set("creationdatetime", t.CreatedDateTime)
                            .Set("status", (int)t.Status)
                            .Set("statuschanged", t.StatusChangeDate)
                            .Set("payment_id", t.PaymentId)
                            .Set("last_modified", t.LastModified = DateTime.UtcNow)
                            .Set("industry", (int)t.Industry)
                            .Set("spam", t.Spam)
                            .Set("calls", t.Calls)
                            .Where("id", t.TenantId);

                    db.ExecuteNonQuery(u);
                }

                if (string.IsNullOrEmpty(t.PartnerId) && string.IsNullOrEmpty(t.AffiliateId))
                {
                    var d = new SqlDelete("tenants_partners").Where("tenant_id", t.TenantId);
                    db.ExecuteNonQuery(d);
                }
                else
                {
                    var i = new SqlInsert("tenants_partners", true)
                            .InColumnValue("tenant_id", t.TenantId)
                            .InColumnValue("partner_id", t.PartnerId)
                            .InColumnValue("affiliate_id", t.AffiliateId);
                    db.ExecuteNonQuery(i);
                }

                tx.Commit();
            }
            //CalculateTenantDomain(t);
            return(t);
        }