예제 #1
0
        private static Customer GetCurrentCustomer()
        {
            Customer customer = null;

            if (HttpContext.Current != null)
            {
                var cachedCustomer = HttpContext.Current.Items[CustomerContextKey] as Customer;
                if (cachedCustomer != null)
                {
                    return(cachedCustomer);
                }

                if (IsDebug)
                {
                    customer = new Customer
                    {
                        CustomerRole = Role.Administrator,
                        IsVirtual    = true
                    };
                }

                //registered user
                if (customer == null)
                {
                    customer = AuthorizeService.GetAuthenticatedCustomer();
                }

                //load guest customer
                if (customer == null)
                {
                    var customerCookie = CommonHelper.GetCookie(CustomerCookieName);
                    if (customerCookie != null && !String.IsNullOrEmpty(customerCookie.Value))
                    {
                        Guid customerGuid;
                        if (Guid.TryParse(customerCookie.Value, out customerGuid) &&
                            !CustomerService.ExistsCustomer(customerGuid))
                        {
                            customer = new Customer {
                                Id = customerGuid, CustomerRole = Role.Guest
                            };
                        }
                    }
                }

                //create guest if not exists
                if (customer == null)
                {
                    var customerId = Guid.NewGuid();
                    while (CustomerService.ExistsCustomer(customerId))
                    {
                        customerId = Guid.NewGuid();
                    }

                    customer = new Customer {
                        Id = customerId, CustomerRole = Role.Guest
                    };
                    SetCustomerCookie(customer.Id);
                }

                HttpContext.Current.Items[CustomerContextKey] = customer;
            }
            return(customer);
        }