Example #1
0
        public virtual IActionResult SubscribePopupPOST(int productId)
        {
            var product = _productService.GetProductById(productId);

            if (product == null || product.Deleted)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            if (!_customerService.IsRegistered(_workContext.CurrentCustomer))
            {
                return(Content(_localizationService.GetResource("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                _productService.GetTotalStockQuantity(product) <= 0)
            {
                //out of stock
                var subscription = _backInStockSubscriptionService
                                   .FindSubscription(_workContext.CurrentCustomer.Id, product.Id, _storeContext.CurrentStore.Id);
                if (subscription != null)
                {
                    //subscription already exists
                    //unsubscribe
                    _backInStockSubscriptionService.DeleteSubscription(subscription);

                    _notificationService.SuccessNotification(_localizationService.GetResource("BackInStockSubscriptions.Notification.Unsubscribed"));
                    return(new OkResult());
                }

                //subscription does not exist
                //subscribe
                if (_backInStockSubscriptionService
                    .GetAllSubscriptionsByCustomerId(_workContext.CurrentCustomer.Id, _storeContext.CurrentStore.Id, 0, 1)
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Json(new
                    {
                        result = string.Format(_localizationService.GetResource("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)
                    }));
                }
                subscription = new BackInStockSubscription
                {
                    CustomerId   = _workContext.CurrentCustomer.Id,
                    ProductId    = product.Id,
                    StoreId      = _storeContext.CurrentStore.Id,
                    CreatedOnUtc = DateTime.UtcNow
                };
                _backInStockSubscriptionService.InsertSubscription(subscription);

                _notificationService.SuccessNotification(_localizationService.GetResource("BackInStockSubscriptions.Notification.Subscribed"));
                return(new OkResult());
            }

            //subscription not possible
            return(Content(_localizationService.GetResource("BackInStockSubscriptions.NotAllowed")));
        }
        public virtual async Task <IActionResult> SubscribePopupPOST(string productId)
        {
            var product = await _productService.GetProductById(productId);

            if (product == null)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            var customer = _workContext.CurrentCustomer;

            if (!customer.IsRegistered())
            {
                return(Content(_localizationService.GetResource("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.GetTotalStockQuantity(warehouseId: _storeContext.CurrentStore.DefaultWarehouseId) <= 0)
            {
                //out of stock
                var subscription = await _backInStockSubscriptionService
                                   .FindSubscription(customer.Id, product.Id, _storeContext.CurrentStore.Id, product.UseMultipleWarehouses?_storeContext.CurrentStore.DefaultWarehouseId : "");

                if (subscription != null)
                {
                    //subscription already exists
                    //unsubscribe
                    await _backInStockSubscriptionService.DeleteSubscription(subscription);

                    return(Content("Unsubscribed"));
                }

                //subscription does not exist
                //subscribe
                if ((await _backInStockSubscriptionService
                     .GetAllSubscriptionsByCustomerId(customer.Id, _storeContext.CurrentStore.Id, 0, 1))
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Content(string.Format(_localizationService.GetResource("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)));
                }
                subscription = new BackInStockSubscription
                {
                    CustomerId   = customer.Id,
                    ProductId    = product.Id,
                    StoreId      = _storeContext.CurrentStore.Id,
                    WarehouseId  = product.UseMultipleWarehouses ? _storeContext.CurrentStore.DefaultWarehouseId : "",
                    CreatedOnUtc = DateTime.UtcNow
                };
                await _backInStockSubscriptionService.InsertSubscription(subscription);

                return(Content("Subscribed"));
            }

            //subscription not possible
            return(Content(_localizationService.GetResource("BackInStockSubscriptions.NotAllowed")));
        }
        public ActionResult BackInStockSubscribePopup(int id /* productId */, FormCollection form)
        {
            var product = _productService.GetProductById(id);

            if (product == null || product.Deleted || product.IsSystemProduct)
            {
                throw new ArgumentException(T("Products.NotFound", id));
            }

            if (!_services.WorkContext.CurrentCustomer.IsRegistered())
            {
                return(Content(T("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.StockQuantity <= 0)
            {
                var customer = _services.WorkContext.CurrentCustomer;
                var store    = _services.StoreContext.CurrentStore;

                // Out of stock.
                var subscription = _backInStockSubscriptionService.FindSubscription(customer.Id, product.Id, store.Id);
                if (subscription != null)
                {
                    // Unsubscribe.
                    _backInStockSubscriptionService.DeleteSubscription(subscription);
                    return(Content("Unsubscribed"));
                }
                else
                {
                    if (_backInStockSubscriptionService.GetAllSubscriptionsByCustomerId(customer.Id, store.Id, 0, 1).TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                    {
                        return(Content(string.Format(T("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)));
                    }

                    // Subscribe.
                    subscription = new BackInStockSubscription
                    {
                        Customer     = customer,
                        Product      = product,
                        StoreId      = store.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };

                    _backInStockSubscriptionService.InsertSubscription(subscription);
                    return(Content("Subscribed"));
                }
            }
            else
            {
                return(Content(T("BackInStockSubscriptions.NotAllowed")));
            }
        }
        public ActionResult BackInStockSubscribePopupPOST(int id /* productId */)
        {
            var product = _productService.GetProductById(id);

            if (product == null || product.Deleted)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            if (!_services.WorkContext.CurrentCustomer.IsRegistered())
            {
                return(Content(T("BackInStockSubscriptions.OnlyRegistered")));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.StockQuantity <= 0)
            {
                //out of stock
                var subscription = _backInStockSubscriptionService
                                   .FindSubscription(_services.WorkContext.CurrentCustomer.Id, product.Id, _services.StoreContext.CurrentStore.Id);
                if (subscription != null)
                {
                    //unsubscribe
                    _backInStockSubscriptionService.DeleteSubscription(subscription);
                    return(Content("Unsubscribed"));
                }
                else
                {
                    if (_backInStockSubscriptionService
                        .GetAllSubscriptionsByCustomerId(_services.WorkContext.CurrentCustomer.Id, _services.StoreContext.CurrentStore.Id, 0, 1)
                        .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                    {
                        return(Content(string.Format(T("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)));
                    }

                    //subscribe
                    subscription = new BackInStockSubscription()
                    {
                        Customer     = _services.WorkContext.CurrentCustomer,
                        Product      = product,
                        StoreId      = _services.StoreContext.CurrentStore.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    };
                    _backInStockSubscriptionService.InsertSubscription(subscription);
                    return(Content("Subscribed"));
                }
            }
            else
            {
                return(Content(T("BackInStockSubscriptions.NotAllowed")));
            }
        }
        /// <summary>
        /// Permanent delete of customer
        /// </summary>
        /// <param name="customer">Customer</param>
        public virtual void PermanentDeleteCustomer(Customer customer)
        {
            if (customer == null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            //blog comments
            var blogComments = _blogService.GetAllComments(customerId: customer.Id);

            _blogService.DeleteBlogComments(blogComments);

            //news comments
            var newsComments = _newsService.GetAllComments(customerId: customer.Id);

            _newsService.DeleteNewsComments(newsComments);

            //back in stock subscriptions
            var backInStockSubscriptions = _backInStockSubscriptionService.GetAllSubscriptionsByCustomerId(customer.Id);

            foreach (var backInStockSubscription in backInStockSubscriptions)
            {
                _backInStockSubscriptionService.DeleteSubscription(backInStockSubscription);
            }

            //product review
            var productReviews   = _productService.GetAllProductReviews(customerId: customer.Id, approved: null);
            var reviewedProducts = _productService.GetProductsByIds(productReviews.Select(p => p.ProductId).Distinct().ToArray());

            _productService.DeleteProductReviews(productReviews);
            //update product totals
            foreach (var product in reviewedProducts)
            {
                _productService.UpdateProductReviewTotals(product);
            }

            //external authentication record
            foreach (var ear in customer.ExternalAuthenticationRecords)
            {
                _externalAuthenticationService.DeleteExternalAuthenticationRecord(ear);
            }

            //forum subscriptions
            var forumSubscriptions = _forumService.GetAllSubscriptions(customer.Id);

            foreach (var forumSubscription in forumSubscriptions)
            {
                _forumService.DeleteSubscription(forumSubscription);
            }

            //shopping cart items
            foreach (var sci in customer.ShoppingCartItems)
            {
                _shoppingCartService.DeleteShoppingCartItem(sci);
            }

            //private messages (sent)
            foreach (var pm in _forumService.GetAllPrivateMessages(0, customer.Id, 0, null, null, null, null))
            {
                _forumService.DeletePrivateMessage(pm);
            }

            //private messages (received)
            foreach (var pm in _forumService.GetAllPrivateMessages(0, 0, customer.Id, null, null, null, null))
            {
                _forumService.DeletePrivateMessage(pm);
            }

            //newsletter
            var allStores = _storeService.GetAllStores();

            foreach (var store in allStores)
            {
                var newsletter = _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreId(customer.Email, store.Id);
                if (newsletter != null)
                {
                    _newsLetterSubscriptionService.DeleteNewsLetterSubscription(newsletter);
                }
            }

            //addresses
            foreach (var address in customer.Addresses)
            {
                _customerService.RemoveCustomerAddress(customer, address);
                _customerService.UpdateCustomer(customer);
                //now delete the address record
                _addressService.DeleteAddress(address);
            }

            //generic attributes
            var keyGroup          = customer.GetUnproxiedEntityType().Name;
            var genericAttributes = _genericAttributeService.GetAttributesForEntity(customer.Id, keyGroup);

            _genericAttributeService.DeleteAttributes(genericAttributes);

            //ignore ActivityLog
            //ignore ForumPost, ForumTopic, ignore ForumPostVote
            //ignore Log
            //ignore PollVotingRecord
            //ignore ProductReviewHelpfulness
            //ignore RecurringPayment
            //ignore ReturnRequest
            //ignore RewardPointsHistory
            //and we do not delete orders

            //remove from Registered role, add to Guest one
            if (customer.IsRegistered())
            {
                var registeredRole = _customerService.GetCustomerRoleBySystemName(NopCustomerDefaults.RegisteredRoleName);
                customer.RemoveCustomerRoleMapping(
                    customer.CustomerCustomerRoleMappings.FirstOrDefault(mapping => mapping.CustomerRoleId == registeredRole.Id));
            }

            if (!customer.IsGuest())
            {
                var guestRole = _customerService.GetCustomerRoleBySystemName(NopCustomerDefaults.GuestsRoleName);
                customer.AddCustomerRoleMapping(new CustomerCustomerRoleMapping {
                    CustomerRole = guestRole
                });
            }

            var email = customer.Email;

            //clear other information
            customer.Email             = string.Empty;
            customer.EmailToRevalidate = string.Empty;
            customer.Username          = string.Empty;
            customer.Active            = false;
            customer.Deleted           = true;
            _customerService.UpdateCustomer(customer);

            //raise event
            _eventPublisher.Publish(new CustomerPermanentlyDeleted(customer.Id, email));
        }
Example #6
0
        public virtual void AnonymizeCustomer(Customer customer, bool pseudomyzeContent)
        {
            Guard.NotNull(customer, nameof(customer));

            var language     = customer.GetLanguage();
            var customerName = customer.GetFullName() ?? customer.Username ?? customer.FindEmail();

            using (var scope = new DbContextScope(_services.DbContext, autoCommit: false))
            {
                // Set to deleted
                customer.Deleted = true;

                // Unassign roles
                customer.CustomerRoles.Clear();
                customer.CustomerRoles.Add(_services.Resolve <ICustomerService>().GetCustomerRoleBySystemName(SystemCustomerRoleNames.Guests));

                // Delete shopping cart & wishlist (TBD: (mc) Really?!?)
                _shoppingCartService.DeleteExpiredShoppingCartItems(DateTime.UtcNow, customer.Id);

                // Delete forum subscriptions
                var forumSubscriptions = _forumService.GetAllSubscriptions(customer.Id, 0, 0, 0, int.MaxValue);
                foreach (var forumSub in forumSubscriptions)
                {
                    _forumService.DeleteSubscription(forumSub);
                }

                // Delete BackInStock subscriptions
                var backInStockSubscriptions = _backInStockSubscriptionService.GetAllSubscriptionsByCustomerId(customer.Id, 0, 0, int.MaxValue);
                foreach (var stockSub in backInStockSubscriptions)
                {
                    _backInStockSubscriptionService.DeleteSubscription(stockSub);
                }

                // Generic attributes
                var attributes = _genericAttributeService.GetAttributesForEntity(customer.Id, "Customer");
                foreach (var attr in attributes)
                {
                    // we don't need to mask generic attrs, we just delete them.
                    _genericAttributeService.DeleteAttribute(attr);
                }

                // Customer Data
                AnonymizeData(customer, x => x.Username, IdentifierDataType.UserName, language);
                AnonymizeData(customer, x => x.Email, IdentifierDataType.EmailAddress, language);
                AnonymizeData(customer, x => x.LastIpAddress, IdentifierDataType.IpAddress, language);
                if (pseudomyzeContent)
                {
                    AnonymizeData(customer, x => x.AdminComment, IdentifierDataType.LongText, language);
                    AnonymizeData(customer, x => x.LastLoginDateUtc, IdentifierDataType.DateTime, language);
                    AnonymizeData(customer, x => x.LastActivityDateUtc, IdentifierDataType.DateTime, language);
                }

                // Addresses
                foreach (var address in customer.Addresses)
                {
                    AnonymizeAddress(address, language);
                }

                // Private messages
                if (pseudomyzeContent)
                {
                    var privateMessages = _forumService.GetAllPrivateMessages(0, customer.Id, 0, null, null, null, null, 0, int.MaxValue);
                    foreach (var msg in privateMessages)
                    {
                        AnonymizeData(msg, x => x.Subject, IdentifierDataType.Text, language);
                        AnonymizeData(msg, x => x.Text, IdentifierDataType.LongText, language);
                    }
                }

                // Forum topics
                if (pseudomyzeContent)
                {
                    foreach (var topic in customer.ForumTopics)
                    {
                        AnonymizeData(topic, x => x.Subject, IdentifierDataType.Text, language);
                    }
                }

                // Forum posts
                foreach (var post in customer.ForumPosts)
                {
                    AnonymizeData(post, x => x.IPAddress, IdentifierDataType.IpAddress, language);
                    if (pseudomyzeContent)
                    {
                        AnonymizeData(post, x => x.Text, IdentifierDataType.LongText, language);
                    }
                }

                // Customer Content
                foreach (var item in customer.CustomerContent)
                {
                    AnonymizeData(item, x => x.IpAddress, IdentifierDataType.IpAddress, language);

                    if (pseudomyzeContent)
                    {
                        switch (item)
                        {
                        case ProductReview c:
                            AnonymizeData(c, x => x.ReviewText, IdentifierDataType.LongText, language);
                            AnonymizeData(c, x => x.Title, IdentifierDataType.Text, language);
                            break;

                        case NewsComment c:
                            AnonymizeData(c, x => x.CommentText, IdentifierDataType.LongText, language);
                            AnonymizeData(c, x => x.CommentTitle, IdentifierDataType.Text, language);
                            break;

                        case BlogComment c:
                            AnonymizeData(c, x => x.CommentText, IdentifierDataType.LongText, language);
                            break;
                        }
                    }
                }

                //// Anonymize Order IPs
                //// TBD: Don't! Doesn't feel right because of fraud detection etc.
                //foreach (var order in customer.Orders)
                //{
                //	AnonymizeData(order, x => x.CustomerIp, IdentifierDataType.IpAddress, language);
                //}

                // SAVE!!!
                //_services.DbContext.DetachAll(); // TEST
                scope.Commit();

                // Log
                Logger.Info(T("Gdpr.Anonymize.Success", language.Id, customerName));
            }
        }
Example #7
0
        public virtual async Task <IActionResult> SubscribePopup(string productId, IFormCollection form)
        {
            var product = await _productService.GetProductById(productId);

            if (product == null)
            {
                throw new ArgumentException("No product found with the specified id");
            }

            var customer = _workContext.CurrentCustomer;

            string warehouseId = _shoppingCartSettings.AllowToSelectWarehouse ?
                                 form["WarehouseId"].ToString() :
                                 product.UseMultipleWarehouses ? _storeContext.CurrentStore.DefaultWarehouseId :
                                 (string.IsNullOrEmpty(_storeContext.CurrentStore.DefaultWarehouseId) ? product.WarehouseId : _storeContext.CurrentStore.DefaultWarehouseId);

            if (!customer.IsRegistered())
            {
                return(Json(new
                {
                    subscribe = false,
                    buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                    resource = _localizationService.GetResource("BackInStockSubscriptions.OnlyRegistered")
                }));
            }

            if ((product.ManageInventoryMethod == ManageInventoryMethod.ManageStock) &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions &&
                product.GetTotalStockQuantity(warehouseId: warehouseId) <= 0)
            {
                var subscription = await _backInStockSubscriptionService
                                   .FindSubscription(customer.Id, product.Id, null, _storeContext.CurrentStore.Id, warehouseId);

                if (subscription != null)
                {
                    //subscription already exists
                    //unsubscribe
                    await _backInStockSubscriptionService.DeleteSubscription(subscription);

                    return(Json(new
                    {
                        subscribe = false,
                        buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                        resource = _localizationService.GetResource("BackInStockSubscriptions.Unsubscribed")
                    }));
                }

                //subscription does not exist
                //subscribe
                if ((await _backInStockSubscriptionService
                     .GetAllSubscriptionsByCustomerId(customer.Id, _storeContext.CurrentStore.Id, 0, 1))
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Json(new
                    {
                        subscribe = false,
                        buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                        resource = string.Format(_localizationService.GetResource("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)
                    }));
                }
                subscription = new BackInStockSubscription {
                    CustomerId   = customer.Id,
                    ProductId    = product.Id,
                    StoreId      = _storeContext.CurrentStore.Id,
                    WarehouseId  = warehouseId,
                    CreatedOnUtc = DateTime.UtcNow
                };
                await _backInStockSubscriptionService.InsertSubscription(subscription);

                return(Json(new
                {
                    subscribe = true,
                    buttontext = _localizationService.GetResource("BackInStockSubscriptions.DeleteNotifyWhenAvailable"),
                    resource = _localizationService.GetResource("BackInStockSubscriptions.Subscribed")
                }));
            }

            if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStockByAttributes &&
                product.BackorderMode == BackorderMode.NoBackorders &&
                product.AllowBackInStockSubscriptions)
            {
                var attributes = await _mediator.Send(new GetParseProductAttributes()
                {
                    Product = product, Form = form
                });

                var subscription = await _backInStockSubscriptionService
                                   .FindSubscription(customer.Id, product.Id, attributes, _storeContext.CurrentStore.Id, warehouseId);

                if (subscription != null)
                {
                    //subscription already exists
                    //unsubscribe
                    await _backInStockSubscriptionService.DeleteSubscription(subscription);

                    return(Json(new
                    {
                        subscribe = false,
                        buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                        resource = _localizationService.GetResource("BackInStockSubscriptions.Unsubscribed")
                    }));
                }

                //subscription does not exist
                //subscribe
                if ((await _backInStockSubscriptionService
                     .GetAllSubscriptionsByCustomerId(customer.Id, _storeContext.CurrentStore.Id, 0, 1))
                    .TotalCount >= _catalogSettings.MaximumBackInStockSubscriptions)
                {
                    return(Json(new
                    {
                        subscribe = false,
                        buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                        resource = string.Format(_localizationService.GetResource("BackInStockSubscriptions.MaxSubscriptions"), _catalogSettings.MaximumBackInStockSubscriptions)
                    }));
                }

                subscription = new BackInStockSubscription {
                    CustomerId   = customer.Id,
                    ProductId    = product.Id,
                    Attributes   = attributes,
                    StoreId      = _storeContext.CurrentStore.Id,
                    WarehouseId  = warehouseId,
                    CreatedOnUtc = DateTime.UtcNow
                };

                await _backInStockSubscriptionService.InsertSubscription(subscription);

                return(Json(new
                {
                    subscribe = true,
                    buttontext = _localizationService.GetResource("BackInStockSubscriptions.DeleteNotifyWhenAvailable"),
                    resource = _localizationService.GetResource("BackInStockSubscriptions.Subscribed")
                }));
            }

            return(Json(new
            {
                subscribe = false,
                buttontext = _localizationService.GetResource("BackInStockSubscriptions.NotifyMeWhenAvailable"),
                resource = _localizationService.GetResource("BackInStockSubscriptions.NotAllowed")
            }));
        }