public virtual void SignIn(Customer customer, bool createPersistentCookie)
        {
            var now = DateTime.Now;

            var ticket = new FormsAuthenticationTicket(
                1 /*version*/,
                customer.Username,
                now,
                now.Add(_expirationTimeSpan),
                createPersistentCookie,
               customer.Username,
                FormsAuthentication.FormsCookiePath);

            var encryptedTicket = FormsAuthentication.Encrypt(ticket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            cookie.Secure = FormsAuthentication.RequireSSL;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            if (FormsAuthentication.CookieDomain != null)
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }

            _httpContext.Response.Cookies.Add(cookie);
            _cachedCustomer = customer;
        }
        private static void FilteredLog(ILogger logger, LogLevel level, string message, Exception exception = null, Customer customer = null)
        {
            //don't log thread abort exception
            if (exception is System.Threading.ThreadAbortException)
                return;

            if (logger.IsEnabled(level))
            {
                string fullMessage = exception == null ? string.Empty : exception.ToString();
                logger.InsertLog(level, message, fullMessage, customer);
            }
        }
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="customer">Customer</param>
 /// <param name="email">Email</param>
 /// <param name="username">Username</param>
 /// <param name="password">Password</param>
 /// <param name="passwordFormat">Password fprmat</param>
 /// <param name="isApproved">Is approved</param>
 public CustomerRegistrationRequest(Customer customer, string email, string username,
     string password,
     PasswordFormat passwordFormat,
     bool isApproved = true)
 {
     this.Customer = customer;
     this.Email = email;
     this.Username = username;
     this.Password = password;
     this.PasswordFormat = passwordFormat;
     this.IsApproved = isApproved;
 }
        /// <summary>
        /// Delete a customer
        /// </summary>
        /// <param name="customer">Customer</param>
        public virtual void DeleteCustomer(Customer customer)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            if (customer.IsSystemAccount)
                throw new AFException(string.Format("System customer account ({0}) could not be deleted",
                    customer.SystemName));

            customer.Deleted = true;

            if (!String.IsNullOrEmpty(customer.Email))
                customer.Email += "-DELETED";
            if (!String.IsNullOrEmpty(customer.Username))
                customer.Username += "-DELETED";

            UpdateCustomer(customer);
        }
        public virtual Customer GetAuthenticatedCustomer()
        {
            if (_cachedCustomer != null)
                return _cachedCustomer;

            if (_httpContext == null ||
                _httpContext.Request == null ||
                !_httpContext.Request.IsAuthenticated ||
                !(_httpContext.User.Identity is FormsIdentity))
            {
                return null;
            }

            var formsIdentity = (FormsIdentity)_httpContext.User.Identity;
            var customer = GetAuthenticatedCustomerFromTicket(formsIdentity.Ticket);
            if (customer != null && customer.Active && !customer.Deleted)
                _cachedCustomer = customer;
            return _cachedCustomer;
        }
 public virtual void SignOut()
 {
     _cachedCustomer = null;
     FormsAuthentication.SignOut();
 }
        /// <summary>
        /// Inserts a log item
        /// </summary>
        /// <param name="logLevel">Log level</param>
        /// <param name="shortMessage">The short message</param>
        /// <param name="fullMessage">The full message</param>
        /// <param name="customer">The customer to associate log record with</param>
        /// <returns>A log item</returns>
        public virtual Log InsertLog(LogLevel logLevel, string shortMessage, string fullMessage = "",
            Customer customer = null)
        {
            //check ignore word/phrase list?
            if (IgnoreLog(shortMessage) && IgnoreLog(fullMessage))
                return null;

            var log = new Log
            {
                LogLevel = logLevel,
                ShortMessage = shortMessage,
                FullMessage = fullMessage,
                IpAddress = _webHelper.GetCurrentIpAddress(),
                PageUrl = _webHelper.GetThisPageUrl(true),
                ReferrerUrl = _webHelper.GetUrlReferrer(),
                CreatedOn = DateTime.Now
            };

            if (customer != null)
            {
                log.CustomerId = customer.Id;
            }

            _logRepository.Insert(log);

            return log;
        }
        /// <summary>
        /// Gets the order list.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <param name="page">The page.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public virtual IPagedList<Order> GetOrderList(Customer customer, int page, int size)
        {
            if (customer == null)
                throw new ArgumentNullException(nameof(customer));

            var query = _orderRepository.Table.Where(t => t.CustomerId == customer.Id && !t.Deleted)
                .OrderByDescending(t => t.CreatedTime);
            return new PagedList<Order>(query, page - 1, size);
        }
 public static void Warning(this ILogger logger, string message, Exception exception = null, Customer customer = null)
 {
     FilteredLog(logger, LogLevel.Warning, message, exception, customer);
 }
        /// <summary>
        /// Updates the customer
        /// </summary>
        /// <param name="customer">Customer</param>
        public virtual void UpdateCustomer(Customer customer)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            _customerRepository.Update(customer);

            //event notification
            _eventPublisher.EntityUpdated(customer);
        }
        /// <summary>
        /// Sets a customer username
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="newUsername">New Username</param>
        public virtual void SetUsername(Customer customer, string newUsername)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            newUsername = newUsername.Trim();

            if (newUsername.Length > 100)
                throw new AFException("Account.UsernameErrors.UsernameTooLong");

            var user2 = _customerService.GetCustomerByUsername(newUsername);
            if (user2 != null && customer.Id != user2.Id)
                throw new AFException("Account.EmailUsernameErrors.UsernameAlreadyExists");

            customer.Username = newUsername;
            _customerService.UpdateCustomer(customer);
        }
        /// <summary>
        /// Sets a user email
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="newEmail">New email</param>
        public virtual void SetEmail(Customer customer, string newEmail)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            if (newEmail == null)
                throw new AFException("Email cannot be null");

            newEmail = newEmail.Trim();

            if (!CommonHelper.IsValidEmail(newEmail))
                throw new AFException("Account.EmailErrors.NewEmailIsNotValid");

            if (newEmail.Length > 100)
                throw new AFException("Account.EmailErrors.EmailTooLong");

            var customer2 = _customerService.GetCustomerByEmail(newEmail);
            if (customer2 != null && customer.Id != customer2.Id)
                throw new AFException("Account.EmailErrors.EmailAlreadyExists");

            customer.Email = newEmail;
            _customerService.UpdateCustomer(customer);
        }
 /// <summary>
 /// Inserts a log item
 /// </summary>
 /// <param name="logLevel">Log level</param>
 /// <param name="shortMessage">The short message</param>
 /// <param name="fullMessage">The full message</param>
 /// <param name="customer">The customer to associate log record with</param>
 /// <returns>A log item</returns>
 public virtual Log InsertLog(LogLevel logLevel, string shortMessage, string fullMessage = "", Customer customer = null)
 {
     return null;
 }