Example #1
0
        /// <summary>
        /// Vaidate DomainLicenses
        /// </summary>
        /// <param name="appKey">Customer App key</param>
        /// <param name="domainValidations">List of domains to validate</param>
        /// <returns>List of DomainValidationResult</returns>
        public IEnumerable<DomainValidationResult> Validate(Guid appKey, IEnumerable<DomainValidation> domainValidations)
        {
            using (var context = dataContextFactory.Create())
            {
                DeleteExpiredDomainLicenses();

                customerApp = context.CustomerAppKeys.Where(x => x.AppKey == appKey)
                                     .Select(x => x.CustomerApp)
                                     .Include(x => x.LicenseCustomerApps)
                                     .FirstOrDefault();

                if (customerApp == null)
                {
                    // need to notify, no customerApp
                    throw new Exception(string.Format("CustomerApp with appKey={0} has not found", appKey));
                }

                IEnumerable<Guid> matchedLicenseIds = GetValidLicences(customerApp).ToList();

                var domainLicenses = new List<DomainLicense>();

                var alreadyFailedDomainLicenses = new List<DomainLicense>();

                IEqualityComparer<DomainLicense> equalityComparer = new DomainLicenseEqualityComparer();

                foreach (DomainValidation domainValidation in domainValidations)
                {
                    string domainName = domainValidation.DomainName;
                    Guid featureCode = domainValidation.FeatureCode;

                    var domainLicense = context.DomainLicenses
                                               .Include(x => x.License.Sku.SkuFeatures.Select(s => s.Feature))
                                               .FirstOrDefault(x => x.DomainName == domainName
                                                                    && matchedLicenseIds.Contains(x.LicenseId)
                                                                    &&
                                                                    x.License.Sku.SkuFeatures.Select(
                                                                        s => s.Feature.FeatureCode)
                                                                     .Contains(featureCode));

                    if (domainLicense == null)
                    {
                        var featureLicense = (from x in context.Licenses where matchedLicenseIds.Contains(x.ObjectId) select x)
                            .Include(x => x.Sku.SkuFeatures.Select(s => s.Feature))
                            .FirstOrDefault(
                                x => x.Sku.SkuFeatures.Select(s => s.Feature.FeatureCode).Any(y => y == featureCode));

                        if (featureLicense == null)
                        {
                            // we do not have license on a feature or we just do not have this one
                            // need to notify
                            continue;
                        }

                        domainLicense = new DomainLicense
                            {
                                DomainName = domainName,
                                AutomaticallyCreated = true,
                                DomainLicenseIssued = featureLicense.Sku.CalculateDomainIssueDate(),
                                DomainLicenseExpires = featureLicense.Sku.CalculateAutoDomainExpiration(),
                                KeyBytes = featureLicense.Sku.PrivateKey.KeyBytes,
                                License = featureLicense,
                                LicenseId = featureLicense.ObjectId
                            };

                        if (!context.DomainLicenses.Any(x => x.DomainLicenseId == domainLicense.DomainLicenseId) &&
                            !alreadyFailedDomainLicenses.Contains(domainLicense, equalityComparer))
                        {
                            context.DomainLicenses.Add(domainLicense);
                            if (!context.SaveChanges(OnValidationFailed))
                            {
                                alreadyFailedDomainLicenses.Add(domainLicense);
                            }
                        }
                    }

                    if (!domainLicenses.Contains(domainLicense, equalityComparer) &&
                        !alreadyFailedDomainLicenses.Contains(domainLicense, equalityComparer))
                    {
                        domainLicenses.Add(domainLicense);
                    }
                }
                return ConvertToDomainValidationResults(domainLicenses);
            }
        }
        private static ActionResult LoadDomainLicenseForRemoval(IDataContextByUser context, Guid key, out DomainLicense domainLicense)
        {
            domainLicense = context.DomainLicenses
                .Include(dl => dl.License)
                .Include(dl => dl.License.Sku)
                .SingleOrDefault(x => x.DomainLicenseId == key);

            if (domainLicense == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            if (!domainLicense.CanBeManuallyDeleted)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }

            return null;
        }