/// <summary>
        /// The caches the customer.
        /// </summary>
        /// <param name="customer">
        /// The customer.
        /// </param>
        private void CacheCustomer(ICustomerBase customer)
        {
            // set/reset the cookie
            var cookie = new HttpCookie(CustomerCookieName)
            {
                Value = this.ContextData.ToJson()
            };

            // Ensure a session cookie for Anonymous customers
            if (customer.IsAnonymous)
            {
                if (_anonCookieExpireDays <= 0)
                {
                    cookie.Expires = DateTime.MinValue;
                }
                else
                {
                    var expires = DateTime.Now.AddDays(_anonCookieExpireDays);
                    cookie.Expires = expires;
                }
            }

            this._umbracoContext.HttpContext.Response.Cookies.Add(cookie);

            this._cache.RequestCache.GetCacheItem(CustomerCookieName, () => this.ContextData);
            this._cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(customer.Key), () => customer, TimeSpan.FromMinutes(20), true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to either retrieve an anonymous customer or an existing customer
        /// </summary>
        /// <param name="key">The key of the customer to retrieve</param>
        private void TryGetCustomer(Guid key)
        {
            var customer = (ICustomerBase)_cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(key));

            // check the cache for a previously retrieved customer
            if (customer != null)
            {
                CurrentCustomer = customer;

                if (customer.IsAnonymous)
                {
                    if (_membershipHelper.IsLoggedIn())
                    {
                        var memberId = _membershipHelper.GetCurrentMemberId();
                        var member   = _memberService.GetById(memberId);

                        if (MerchelloConfiguration.Current.CustomerMemberTypes.Any(x => x == member.ContentTypeAlias))
                        {
                            var anonymousBasket = Basket.GetBasket(_merchelloContext, customer);

                            customer = _customerService.GetByLoginName(member.Username) ??
                                       _customerService.CreateCustomerWithKey(member.Username);

                            var customerBasket = Basket.GetBasket(_merchelloContext, customer);

                            //// convert the customer basket
                            ConvertBasket(anonymousBasket, customerBasket);

                            CacheCustomer(customer);
                            CurrentCustomer = customer;

                            return;
                        }
                    }
                }

                ContextData.Key = customer.Key;

                return;
            }

            // try to get the customer
            customer = _customerService.GetAnyByKey(key);

            if (customer != null)
            {
                CurrentCustomer = customer;
                ContextData.Key = customer.Key;
                CacheCustomer(customer);
            }
            else
            {
                // create a new anonymous customer
                CreateAnonymousCustomer();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The caches the customer.
        /// </summary>
        /// <param name="customer">
        /// The customer.
        /// </param>
        private void CacheCustomer(ICustomerBase customer)
        {
            // set/reset the cookie
            // TODO decide how we want to deal with cookie persistence options
            var cookie = new HttpCookie(CustomerCookieName)
            {
                Value = ContextData.ToJson()
            };

            _umbracoContext.HttpContext.Response.Cookies.Add(cookie);

            _cache.RequestCache.GetCacheItem(CustomerCookieName, () => ContextData);
            _cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(customer.Key), () => customer, TimeSpan.FromMinutes(5), true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reinitializes the customer context
        /// </summary>
        /// <param name="customer">
        /// The <see cref="CustomerBase"/>
        /// </param>
        /// <remarks>
        /// Sometimes useful to clear the various caches used internally in the customer context
        /// </remarks>
        public virtual void Reinitialize(ICustomerBase customer)
        {
            // customer has logged out, so we need to go back to an anonymous customer
            var cookie = this._umbracoContext.HttpContext.Request.Cookies[CustomerCookieName];

            if (cookie == null)
            {
                this.Initialize();
                return;
            }

            cookie.Expires = DateTime.Now.AddDays(-1);

            this._cache.RequestCache.ClearCacheItem(CustomerCookieName);
            this._cache.RuntimeCache.ClearCacheItem(CacheKeys.CustomerCacheKey(customer.Key));

            this.Initialize();
        }
Exemplo n.º 5
0
        /// <summary>
        /// The caches the customer.
        /// </summary>
        /// <param name="customer">
        /// The customer.
        /// </param>
        private void CacheCustomer(ICustomerBase customer)
        {
            // set/reset the cookie
            // TODO decide how we want to deal with cookie persistence options
            var cookie = new HttpCookie(CustomerCookieName)
            {
                Value = this.ContextData.ToJson()
            };

            // Ensure a session cookie for Anonymous customers
            // TODO - on persisted authenticcation, we need to synch the cookie expiration
            if (customer.IsAnonymous)
            {
                cookie.Expires = DateTime.MinValue;
            }

            this._umbracoContext.HttpContext.Response.Cookies.Add(cookie);

            this._cache.RequestCache.GetCacheItem(CustomerCookieName, () => this.ContextData);
            this._cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(customer.Key), () => customer, TimeSpan.FromMinutes(5), true);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Attempts to either retrieve an anonymous customer or an existing customer
        /// </summary>
        /// <param name="key">The key of the customer to retrieve</param>
        protected virtual void TryGetCustomer(Guid key)
        {
            var customer = (ICustomerBase)Cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(key));

            // use the IsLoggedIn method to check which gets/sets the value in the Request Cache
            var isLoggedIn = this.IsLoggedIn(key);

            // Check the cache for a previously retrieved customer.
            // There can be many requests for the current customer during a single request.
            if (customer != null)
            {
                CurrentCustomer = customer;

                // No we need to assert whether or not the authentication status has changed
                // during this request - the user either logged in or has logged out.
                if (customer.IsAnonymous)
                {
                    // We have an anonymous customer but the user is now authenticated so we may want to create an
                    // customer and convert the basket
                    if (isLoggedIn)
                    {
                        this.EnsureCustomerCreationAndConvertBasket(customer);
                    }
                }
                else if (customer.IsAnonymous == false && isLoggedIn == false)
                {
                    // The customer that was found was not anonymous and yet the member is
                    // not logged in.
                    CreateAnonymousCustomer();
                    return;
                }
                else if (customer.IsAnonymous == false && isLoggedIn)
                {
                    // User may have logged out and logged in with a different customer
                    // Addresses issue http://issues.merchello.com/youtrack/issue/M-454
                    this.EnsureIsLoggedInCustomer(customer, this.GetMembershipProviderKey());

                    return;
                }

                // The customer key MUST be set in the ContextData
                ContextData.Key = customer.Key;
                return;
            }

            // Customer has not been cached so we have to start from scratch.
            customer = CustomerService.GetAnyByKey(key);

            if (customer != null)
            {
                //// There is either a Customer or Anonymous Customer record

                CurrentCustomer = customer;
                ContextData.Key = customer.Key;

                // The current Membership Providers "ID or Key" is stored in the ContextData so that we can "ensure" that the current logged
                // in member is the same as the reference we have to a previously logged in member in the same browser.
                if (isLoggedIn)
                {
                    ContextData.Values.Add(new KeyValuePair <string, string>(UmbracoMemberIdDataKey, this.GetMembershipProviderKey()));
                }

                // Cache the customer so that for each request we don't have to do a bunch of
                // DB lookups.
                CacheCustomer(customer);
            }
            else
            {
                //// No records were found - create a new Anonymous Customer
                CreateAnonymousCustomer();
            }
        }
        /// <summary>
        /// Attempts to either retrieve an anonymous customer or an existing customer
        /// </summary>
        /// <param name="key">The key of the customer to retrieve</param>
        protected virtual void TryGetCustomer(Guid key)
        {
            // REFACTOR-v3 - this should come directly from the service as this is redundant and creates
            // a second (context specific) cache item.  However, since we're not cloning the cached item
            // out of cache this does create somewhat of a protection against accidently changing values.
            // Also, ideally, we should use a proxy of ICustomerBase so that the customer values are immutable.
            var customer = (ICustomerBase)Cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(key));

            // use the IsLoggedIn method to check which gets/sets the value in the Request Cache
            var isLoggedIn = this.IsLoggedIn(key);

            // Check the cache for a previously retrieved customer.
            // There can be many requests for the current customer during a single request.
            if (customer != null)
            {
                CurrentCustomer = customer;

                // No we need to assert whether or not the authentication status has changed
                // during this request - the user either logged in or has logged out.
                if (customer.IsAnonymous)
                {
                    // We have an anonymous customer but the user is now authenticated so we may want to create an
                    // customer and convert the basket
                    if (isLoggedIn)
                    {
                        this.EnsureCustomerCreationAndConvertBasket(customer);
                    }
                }
                else if (customer.IsAnonymous == false && isLoggedIn == false)
                {
                    // The customer that was found was not anonymous and yet the member is
                    // not logged in.
                    var values = customer.ExtendedData.GetItemsByKeyPrefix(Core.Constants.ExtendedDataKeys.CustomerContextDataPrefix);
                    CreateAnonymousCustomer();
                    UpdateContextData(CurrentCustomer, values);
                    return;
                }
                else if (customer.IsAnonymous == false && isLoggedIn)
                {
                    // User may have logged out and logged in with a different customer
                    // Addresses issue http://issues.merchello.com/youtrack/issue/M-454
                    this.EnsureIsLoggedInCustomer(customer, this.GetMembershipProviderKey());
                    return;
                }

                // The customer key MUST be set in the ContextData
                ContextData.Key = customer.Key;
                return;
            }

            // Customer has not been cached so we have to start from scratch.
            customer = CustomerService.GetAnyByKey(key);

            if (customer != null)
            {
                //// There is either a Customer or Anonymous Customer record

                CurrentCustomer = customer;
                ContextData.Key = customer.Key;

                // The current Membership Providers "ID or Key" is stored in the ContextData so that we can "ensure" that the current logged
                // in member is the same as the reference we have to a previously logged in member in the same browser.
                if (isLoggedIn)
                {
                    ContextData.Pid = this.GetMembershipProviderKey();
                }

                ////ContextData.Values.Add(new KeyValuePair<string, string>(UmbracoMemberIdDataKey, this.GetMembershipProviderKey()));

                // FYI this is really only to set the customer cookie so this entire block
                // should be merged into the section of code directly above.
                CacheCustomer(customer);
            }
            else
            {
                //// No records were found - create a new Anonymous Customer
                CreateAnonymousCustomer();
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Attempts to either retrieve an anonymous customer or an existing customer
        /// </summary>
        /// <param name="key">The key of the customer to retrieve</param>
        private void TryGetCustomer(Guid key)
        {
            var customer = (ICustomerBase)_cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(key));

            var isLoggedIn = (bool)_cache.RequestCache.GetCacheItem(CacheKeys.CustomerIsLoggedIn(key), () => _membershipHelper.IsLoggedIn());

            // check the cache for a previously retrieved customer
            if (customer != null)
            {
                CurrentCustomer = customer;

                if (customer.IsAnonymous)
                {
                    if (isLoggedIn)
                    {
                        var memberId = _membershipHelper.GetCurrentMemberId();
                        var member   = _memberService.GetById(memberId);

                        if (MerchelloConfiguration.Current.CustomerMemberTypes.Any(x => x == member.ContentTypeAlias))
                        {
                            var anonymousBasket = Basket.GetBasket(_merchelloContext, customer);

                            customer = _customerService.GetByLoginName(member.Username) ??
                                       _customerService.CreateCustomerWithKey(member.Username);


                            ContextData.Key = customer.Key;
                            ContextData.Values.Add(new KeyValuePair <string, string>(UmbracoMemberIdDataKey, memberId.ToString(CultureInfo.InvariantCulture)));
                            var customerBasket = Basket.GetBasket(_merchelloContext, customer);

                            //// convert the customer basket
                            ConvertBasket(anonymousBasket, customerBasket);

                            CacheCustomer(customer);
                            CurrentCustomer = customer;

                            return;
                        }
                    }
                }
                else if (customer.IsAnonymous == false && isLoggedIn == false)
                {
                    CreateAnonymousCustomer();
                    return;
                }
                else if (customer.IsAnonymous == false && isLoggedIn)
                {
                    this.EnsureIsLoggedInCustomer(customer);
                }

                ContextData.Key = customer.Key;

                return;
            }

            // try to get the customer
            customer = _customerService.GetAnyByKey(key);

            if (customer != null)
            {
                CurrentCustomer = customer;
                ContextData.Key = customer.Key;
                if (isLoggedIn)
                {
                    ContextData.Values.Add(new KeyValuePair <string, string>(UmbracoMemberIdDataKey, _membershipHelper.GetCurrentMemberId().ToString(CultureInfo.InvariantCulture)));
                }
                CacheCustomer(customer);
            }
            else
            {
                // create a new anonymous customer
                CreateAnonymousCustomer();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Attempts to either retrieve an anonymous customer or an existing customer
        /// </summary>
        /// <param name="key">The key of the customer to retrieve</param>
        private void TryGetCustomer(Guid key)
        {
            var customer = (ICustomerBase)_cache.RuntimeCache.GetCacheItem(CacheKeys.CustomerCacheKey(key));

            var isLoggedIn = (bool)_cache.RequestCache.GetCacheItem(CacheKeys.CustomerIsLoggedIn(key), () => _membershipHelper.IsLoggedIn());


            // check the cache for a previously retrieved customer
            if (customer != null)
            {
                CurrentCustomer = customer;

                if (customer.IsAnonymous)
                {
                    if (isLoggedIn)
                    {
                        var memberId = _membershipHelper.GetCurrentMemberId();
                        var member   = _memberService.GetById(memberId);

                        if (MerchelloConfiguration.Current.CustomerMemberTypes.Any(x => x == member.ContentTypeAlias))
                        {
                            var anonymousBasket = Basket.GetBasket(_merchelloContext, customer);

                            customer = _customerService.GetByLoginName(member.Username) ??
                                       _customerService.CreateCustomerWithKey(member.Username);


                            ContextData.Key = customer.Key;

                            var customerBasket = Basket.GetBasket(_merchelloContext, customer);

                            //// convert the customer basket
                            ConvertBasket(anonymousBasket, customerBasket);

                            CacheCustomer(customer);
                            CurrentCustomer = customer;

                            return;
                        }
                    }
                }
                else if (customer.IsAnonymous == false && isLoggedIn == false)
                {
                    // customer has logged out, so we need to go back to an anonymous customer
                    var cookie = _umbracoContext.HttpContext.Request.Cookies[CustomerCookieName];

                    cookie.Expires = DateTime.Now.AddDays(-1);

                    _cache.RequestCache.ClearCacheItem(CustomerCookieName);
                    _cache.RuntimeCache.ClearCacheItem(CacheKeys.CustomerCacheKey(customer.Key));

                    Initialize();

                    return;
                }

                ContextData.Key = customer.Key;

                return;
            }

            // try to get the customer
            customer = _customerService.GetAnyByKey(key);

            if (customer != null)
            {
                CurrentCustomer = customer;
                ContextData.Key = customer.Key;
                CacheCustomer(customer);
            }
            else
            {
                // create a new anonymous customer
                CreateAnonymousCustomer();
            }
        }