public static CustomerInfo ToWebModel(this Register formModel)
        {
            var result = new CustomerInfo
            {
                Email = formModel.Email,
                FullName = string.Join(" ", formModel.FirstName, formModel.LastName),
                FirstName = formModel.FirstName,
                LastName = formModel.LastName
            };

            if (string.IsNullOrEmpty(result.FullName) || string.IsNullOrWhiteSpace(result.FullName))
            {
                result.FullName = formModel.Email;
            }
            return result;
        }
        public void CreateNewCart_AnonymousUser_CheckThatNewCartCreated()
        {
            var cartBuilder = GetCartBuilder();
            var workContext = GetTestWorkContext();
            var anonymousCustomer = new CustomerInfo
            {
                Id = Guid.NewGuid().ToString(),
                IsRegisteredUser = false
            };
            cartBuilder = cartBuilder.GetOrCreateNewTransientCartAsync(workContext.CurrentStore, anonymousCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency).Result;
            Assert.True(cartBuilder.Cart.IsTransient());

            cartBuilder.SaveAsync().Wait();
            var cart = cartBuilder.Cart;
            Assert.False(cart.IsTransient());

            cartBuilder = cartBuilder.GetOrCreateNewTransientCartAsync(workContext.CurrentStore, anonymousCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency).Result;
            Assert.Equal(cart.Id, cartBuilder.Cart.Id);
        }
        public async Task<ActionResult> UpdateAccount(CustomerInfo customer)
        {
            customer.Id = WorkContext.CurrentCustomer.Id;

            var fullName = string.Join(" ", customer.FirstName, customer.LastName).Trim();

            if (string.IsNullOrEmpty(fullName))
            {
                fullName = customer.Email;
            }

            if (!string.IsNullOrWhiteSpace(fullName))
            {
                customer.FullName = fullName;
            }

            await _customerService.UpdateCustomerAsync(customer);

            WorkContext.CurrentCustomer = await _customerService.GetCustomerByIdAsync(customer.Id);
            return View("customers/account", WorkContext);
        }
        public static CustomerInfo ToWebModel(this VirtoCommerceCustomerModuleWebModelContact contact)
        {
            var retVal = new CustomerInfo();
            retVal.InjectFrom(contact);

            retVal.IsRegisteredUser = true;
            if (contact.Addresses != null)
            {
                retVal.Addresses = contact.Addresses.Select(a => a.ToWebModel()).ToList();
            }

            retVal.DefaultBillingAddress = retVal.Addresses.FirstOrDefault(a => (a.Type & AddressType.Billing) == AddressType.Billing);
            retVal.DefaultShippingAddress = retVal.Addresses.FirstOrDefault(a => (a.Type & AddressType.Shipping) == AddressType.Shipping);

            // TODO: Need separate properties for first, middle and last name
            if (!string.IsNullOrEmpty(contact.FullName))
            {
                var nameParts = contact.FullName.Split(_nameSeparator, 2);

                if (nameParts.Length > 0)
                {
                    retVal.FirstName = nameParts[0];
                }

                if (nameParts.Length > 1)
                {
                    retVal.LastName = nameParts[1];
                }
            }

            if (contact.Emails != null)
            {
                retVal.Email = contact.Emails.FirstOrDefault();
            }

            return retVal;
        }
Exemple #5
0
        private async Task<CustomerInfo> GetCustomerAsync(IOwinContext context)
        {
            var retVal = new CustomerInfo();

            var principal = context.Authentication.User;
            var identity = principal.Identity;

            if (identity.IsAuthenticated)
            {
                var userId = identity.GetUserId();
                if (userId == null)
                {
                    //If somehow claim not found in user cookies need load user by name from API
                    var user = await _commerceApi.StorefrontSecurityGetUserByNameAsync(identity.Name);
                    if (user != null)
                    {
                        userId = user.Id;
                    }
                }

                if (userId != null)
                {
                    var customerService = _container.Resolve<ICustomerService>();
                    var customer = await customerService.GetCustomerByIdAsync(userId);
                    retVal = customer ?? retVal;
                    retVal.Id = userId;
                    retVal.UserName = identity.Name;
                    retVal.IsRegisteredUser = true;
                }

                retVal.OperatorUserId = principal.FindFirstValue(StorefrontConstants.OperatorUserIdClaimType);
                retVal.OperatorUserName = principal.FindFirstValue(StorefrontConstants.OperatorUserNameClaimType);

                var allowedStores = principal.FindFirstValue(StorefrontConstants.AllowedStoresClaimType);
                if (!string.IsNullOrEmpty(allowedStores))
                {
                    retVal.AllowedStores = allowedStores.Split(',');
                }
            }

            if (!retVal.IsRegisteredUser)
            {
                retVal.Id = context.Request.Cookies[StorefrontConstants.AnonymousCustomerIdCookie];
                retVal.UserName = StorefrontConstants.AnonymousUsername;
                retVal.FullName = StorefrontConstants.AnonymousUsername;
            }

            return retVal;
        }
Exemple #6
0
        private void ValidateUserStoreLogin(IOwinContext context, CustomerInfo customer, Store currentStore)
        {

            if (customer.IsRegisteredUser && !customer.AllowedStores.IsNullOrEmpty()
                && !customer.AllowedStores.Any(x => string.Equals(x, currentStore.Id, StringComparison.InvariantCultureIgnoreCase)))
            {
                context.Authentication.SignOut();
                context.Authentication.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
            }
        }
 public UserLoginEvent(WorkContext workContext, CustomerInfo prevUser, CustomerInfo newUser)
 {
     WorkContext = workContext;
     PrevUser = prevUser;
     NewUser = newUser;
 }
        private static ClaimsIdentity CreateClaimsIdentity(CustomerInfo customer)
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, customer.UserName),
                new Claim(ClaimTypes.NameIdentifier, customer.Id)
            };

            var identity = new ClaimsIdentity(claims, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

            if (!customer.AllowedStores.IsNullOrEmpty())
            {
                identity.AddClaim(new Claim(StorefrontConstants.AllowedStoresClaimType, string.Join(",", customer.AllowedStores)));
            }

            if (!string.IsNullOrEmpty(customer.OperatorUserName))
            {
                identity.AddClaim(new Claim(StorefrontConstants.OperatorUserNameClaimType, customer.OperatorUserName));
            }

            if (!string.IsNullOrEmpty(customer.OperatorUserId))
            {
                identity.AddClaim(new Claim(StorefrontConstants.OperatorUserIdClaimType, customer.OperatorUserId));
            }

            return identity;
        }
        private async Task<CustomerInfo> GetStorefrontCustomerByUserAsync(VirtoCommerceCoreModuleWebModelStorefrontUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var result = await _customerService.GetCustomerByIdAsync(user.Id);

            // User may not have contact record
            if (result == null)
            {
                result = new CustomerInfo
                {
                    Id = user.Id,
                    IsRegisteredUser = true,
                };
            }

            result.UserId = user.Id;
            result.UserName = user.UserName;
            result.AllowedStores = user.AllowedStores;

            return result;
        }
        private async Task<CustomerInfo> GetCustomerAsync(IOwinContext context)
        {
            CustomerInfo retVal = new CustomerInfo();

            if (context.Authentication.User.Identity.IsAuthenticated)
            {
                var sidClaim = context.Authentication.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Sid);
                var userId = sidClaim != null ? sidClaim.Value : null;
                if (userId == null)
                {
                    //If somehow claim not found in user cookies need load user by name from API
                    var user = await _platformApi.SecurityGetUserByNameAsync(context.Authentication.User.Identity.Name);
                    if (user != null)
                    {
                        userId = user.Id;
                    }
                }

                if (userId != null)
                {
                    retVal = await _customerService.GetCustomerByIdAsync(userId) ?? retVal;
                    retVal.Id = userId;
                    retVal.UserName = context.Authentication.User.Identity.Name;
                    retVal.IsRegisteredUser = true;
                }
            }

            if (!retVal.IsRegisteredUser)
            {
                retVal.Id = context.Request.Cookies[StorefrontConstants.AnonymousCustomerIdCookie];
                retVal.UserName = StorefrontConstants.AnonymousUsername;
                retVal.FullName = StorefrontConstants.AnonymousUsername;
            }

            return retVal;
        }