Esempio n. 1
0
 public void RestrictionGroupChanged(RestrictionGroup entity, UpdateOperations operation)
 {
     CheckIsAdminOrSystem();
 }
Esempio n. 2
0
        public Expression <Func <Product, bool> > ProductsFilter()
        {
            if (!HttpContext.Current.Request.IsAuthenticated)
            {
                return(product => false);
            }
            var user = HttpContext.Current.User;

            if (user.IsInRole(Role.ISC_System) || user.IsInRole(Role.ISC_Admin) || user.IsInRole(Role.ISC_User))
            {
                return(product => true);
            }

            var websiteConfigurationRepository = ServiceLocator.Current.GetInstance <IUnitOfWorkFactory>().GetUnitOfWork().GetTypedRepository <IWebSiteConfigurationRepository>();

            var customerProductsInclude = websiteConfigurationRepository.GetOrCreateByName <string>("CustomerProductsInclude").ToLowerInvariant();
            Expression <Func <Product, bool> > filter = null;

            if (customerProductsInclude != "ignore")
            {
                var productIdList = CustomerProduct.GetTable().Where(cp => cp.Customer.Id == ContextProvider.CurrentCustomer.Id).Select(cp => cp.Product.Id).ToList();
                if (customerProductsInclude == "true")
                {
                    filter = product => productIdList.Contains(product.Id);
                }
                else
                {
                    filter = product => !productIdList.Contains(product.Id);
                }
            }

            // Restriction Groups
            var currentCustomer    = ContextProvider.CurrentCustomer;
            var currentShipTo      = ContextProvider.CurrentShipTo;
            var restrictionsByItem = websiteConfigurationRepository.GetOrCreateByName <bool>("RestrictionsByItem");

            if (restrictionsByItem && !currentShipTo.IgnoreProductRestrictions && !currentCustomer.IgnoreProductRestrictions && RestrictionGroup.GetTable().Any())
            {
                var shiptoProductSets   = currentShipTo.CustomerProductSets;
                var customerProductSets = currentCustomer.CustomerProductSets;

                Expression <Func <Product, bool> > restrictionFilter = null;

                foreach (var restrictionGroup in RestrictionGroup.GetTable())
                {
                    var restrictionGroupId   = restrictionGroup.Id;
                    var restrictionGroupName = restrictionGroup.Name.Trim();

                    // shipto product sets take precedence over customer
                    var productSet = shiptoProductSets.FirstOrDefault(ps => string.Equals(ps.Name.Trim(), restrictionGroupName, StringComparison.OrdinalIgnoreCase)) ??
                                     customerProductSets.FirstOrDefault(ps => string.Equals(ps.Name.Trim(), restrictionGroupName, StringComparison.OrdinalIgnoreCase));
                    if (productSet != null)
                    {
                        if (productSet.Products.Count == 0)
                        {
                            // Override the whole Restriction Group for this customer/shipto
                            if (restrictionGroup.Allow)
                            {
                                restrictionFilter = restrictionFilter == null ?
                                                    product => product.RestrictionGroup.Id == restrictionGroupId :
                                                    restrictionFilter.Or(product => product.RestrictionGroup.Id == restrictionGroupId);
                            }
                            // ignore deny groups, its overriden to allow it
                        }
                        else
                        {
                            var productIdList = productSet.Products.Select(cp => cp.Product.Id).ToList();
                            if (restrictionGroup.Allow)
                            {
                                // Override the products from the set for this Restriction Group for this customer/shipto
                                restrictionFilter = restrictionFilter == null ?
                                                    product => productIdList.Contains(product.Id) :
                                                    restrictionFilter.Or(product => productIdList.Contains(product.Id));
                            }
                            else
                            {
                                // all products in the group are denied except certain ones
                                Expression <Func <Product, bool> > subQuery = product => product.RestrictionGroup.Id == restrictionGroupId;
                                subQuery          = subQuery.And(product => !productIdList.Contains(product.Id));
                                restrictionFilter = restrictionFilter == null ? subQuery : restrictionFilter.Or(subQuery);
                            }
                        }
                    }
                    else
                    {
                        // Apply RestrictionGroup if no customer/shipto exceptions
                        // if it's allow, we just don't want to do anything
                        if (!restrictionGroup.Allow)
                        {
                            restrictionFilter = restrictionFilter == null ?
                                                product => product.RestrictionGroup.Id == restrictionGroupId :
                                                restrictionFilter.Or(product => product.RestrictionGroup.Id == restrictionGroupId);
                        }
                    }
                }
                if (restrictionFilter != null)
                {
                    // if anything in this restriction filter is true, the product will be blocked
                    restrictionFilter = restrictionFilter.Not();

                    // SQL has a problem comparing to null uniqueidentifiers so ignore restrictionFilter completely if RestrictionGroupId is NULL
                    Expression <Func <Product, bool> > nullRestrictionGroupFilter = product => product.RestrictionGroup == null;
                    restrictionFilter = nullRestrictionGroupFilter.Or(restrictionFilter);

                    filter = filter == null ? restrictionFilter : filter.And(restrictionFilter);
                }
            }

            return(filter ?? (product => true));
        }