Exemplo n.º 1
0
        /// <summary>
        /// Resolves valid licenses and adds issues for expiring and expired ones
        /// </summary>
        /// <param name="customerAppliccation">Customer app to get licenses for</param>
        /// <returns>List of valid license guids</returns>
        private IEnumerable<Guid> GetValidLicences(CustomerApp customerAppliccation)
        {
            var licenses = (from x in customerAppliccation.LicenseCustomerApps select x.License);

            foreach (var license in licenses)
            {
                if (!license.LicenseExpires.HasValue)
                    continue;

                if (license.LicenseExpires.Value < DateTime.Now)
                {
                    applicationIssueUnitOfWork.CustomerAppId = customerApp.CustomerAppId;
                    applicationIssueUnitOfWork.DateTime = DateTime.Now;
                    applicationIssueUnitOfWork.Severity = ApplicationIssueSeverity.High;
                    applicationIssueUnitOfWork.Message = "License expired";
                    applicationIssueUnitOfWork.Details = String.Format("Your license has expired at: {0}",
                                                                       license.LicenseExpires);
                    applicationIssueUnitOfWork.Commit();
                }
                else if (license.LicenseExpires.Value.AddDays(Constants.LicenseExpireWarningDays*-1) < DateTime.Now)
                {
                    applicationIssueUnitOfWork.CustomerAppId = customerApp.CustomerAppId;
                    applicationIssueUnitOfWork.DateTime = DateTime.Now;
                    applicationIssueUnitOfWork.Severity = ApplicationIssueSeverity.Medium;
                    applicationIssueUnitOfWork.Message = "License is about to expire";
                    applicationIssueUnitOfWork.Details = String.Format("Your license is about to expire at: {0}",
                                                                       license.LicenseExpires);
                    applicationIssueUnitOfWork.Commit();
                }
                else
                {
                    yield return license.ObjectId;
                }
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        public void ExecuteCheckout(Customer purchasingCustomer, Customer owningCustomer)
        {
            var currentUser = context.GetUser(HttpContext.Current.User.Identity);

            //Add PurchasingCustomer if none existing
            if (purchasingCustomer.ObjectId == new Guid())
            {
                context.Customers.Add(purchasingCustomer);
                context.SaveChanges();
                if (!currentUser.IsVendorAdmin)
                {
                    context.UserCustomerRights.Add(new UserCustomerRight
                    {
                        RightObject = purchasingCustomer,
                        RightId = EditEntityMembers.Id,
                        UserId = currentUser.UserId
                    });
                    context.SaveChanges();
                }
            }

            //Add OwningCustomer if none existing
            if (owningCustomer != purchasingCustomer)
            {
                if (owningCustomer.ObjectId == new Guid())
                {
                    context.Customers.Add(owningCustomer);
                    context.SaveChanges();
                    if (!currentUser.IsVendorAdmin)
                    {
                        context.UserCustomerRights.Add(new UserCustomerRight
                        {
                            RightObject = owningCustomer,
                            RightId = EditEntityInfo.Id,
                            UserId = currentUser.UserId
                        });
                        context.SaveChanges();
                    }
                }
            }

            //Create licenses for every transactionitem
            foreach (var item in Transaction.TransactionItems)
            {
                if (item.License == null)
                {
                    var newLicense = new Model.License(item.Sku, owningCustomer, purchasingCustomer);
                    context.Licenses.Add(newLicense);

                    item.License = newLicense;
                }
            }
            context.SaveChanges();

            var existingCustomerApps = (from x in context.Licenses
                join y in context.LicenseCustomerApps on x.ObjectId equals y.LicenseId
                where x.OwningCustomerId == owningCustomer.ObjectId
                select y.CustomerApp).ToList();

            //Add to any existing apps
            if (existingCustomerApps.Any())
            {
                foreach (var customerApp in existingCustomerApps)
                {
                    customerApp.AddLicenses((from x in Transaction.TransactionItems select x.License.ObjectId));
                }
                context.SaveChanges();
            }
            else
            {
                //Create default application containing all licenses
                var newCustomerApp = new CustomerApp()
                {
                    ApplicationName = owningCustomer.Name + "_Application"
                };
                context.CustomerApps.Add(newCustomerApp);
                newCustomerApp.AddLicenses((from x in Transaction.TransactionItems select x.License.ObjectId));
                newCustomerApp.CustomerAppKeys.Add(new CustomerAppKey());
            }
            Transaction.Status = TransactionStatus.Complete;
            context.SaveChanges();
        }