예제 #1
0
        // GET: Subscription/Unsubscribe
        public ActionResult Unsubscribe(UnsubscriptionModel model)
        {
            var invalidUrlMessage = "You weren't unsubscribed because your unsubscription link is not valid.";

            if (ModelState.IsValid)
            {
                bool emailIsValid = emailHashValidator.ValidateEmailHash(model.Hash, model.Email);

                if (emailIsValid)
                {
                    try
                    {
                        var issue = issueInfoProvider.Get(model.IssueGuid, SiteContext.CurrentSiteID);

                        if (model.UnsubscribeFromAll)
                        {
                            // Unsubscribes if not already unsubscribed
                            if (!unsubscriptionProvider.IsUnsubscribedFromAllNewsletters(model.Email))
                            {
                                subscriptionService.UnsubscribeFromAllNewsletters(model.Email, issue?.IssueID);
                            }

                            model.UnsubscriptionResult = localizer["You've been unsubscribed from all email marketing campaigns."].Value;
                        }
                        else
                        {
                            var newsletter = newsletterInfoProvider.Get(model.NewsletterGuid, SiteContext.CurrentSiteID);
                            if (newsletter != null)
                            {
                                // Unsubscribes if not already unsubscribed
                                if (!unsubscriptionProvider.IsUnsubscribedFromSingleNewsletter(model.Email, newsletter.NewsletterID))
                                {
                                    subscriptionService.UnsubscribeFromSingleNewsletter(model.Email, newsletter.NewsletterID, issue?.IssueID);
                                }

                                model.UnsubscriptionResult = localizer["We would like to offer you a coffee to go, but we can't, because we have just removed your email from the list."].Value;
                            }
                            else
                            {
                                AddError(invalidUrlMessage);
                            }
                        }
                    }
                    catch (ArgumentException)
                    {
                        AddError(invalidUrlMessage);
                    }
                }
                else
                {
                    AddError(invalidUrlMessage);
                }
            }
            else
            {
                AddError(invalidUrlMessage);
            }

            return(View(model));
        }
        public ActionResult Unsubscribe(UnsubscriptionModel model)
        {
            string invalidUrlMessage = ResHelper.GetString("DancingGoatMvc.News.InvalidUnsubscriptionLink");

            if (ModelState.IsValid)
            {
                bool emailIsValid = mEmailHashValidator.ValidateEmailHash(model.Hash, model.Email);

                if (emailIsValid)
                {
                    try
                    {
                        var issue = IssueInfoProvider.GetIssueInfo(model.IssueGuid, SiteContext.CurrentSiteID);

                        if (model.UnsubscribeFromAll)
                        {
                            // Unsubscribes if not already unsubscribed
                            if (!mUnsubscriptionProvider.IsUnsubscribedFromAllNewsletters(model.Email))
                            {
                                mSubscriptionService.UnsubscribeFromAllNewsletters(model.Email, issue?.IssueID);
                            }

                            model.UnsubscriptionResult = ResHelper.GetString("DancingGoatMvc.News.UnsubscribedAll");
                        }
                        else
                        {
                            var newsletter = NewsletterInfoProvider.GetNewsletterInfo(model.NewsletterGuid, SiteContext.CurrentSiteID);
                            if (newsletter != null)
                            {
                                // Unsubscribes if not already unsubscribed
                                if (!mUnsubscriptionProvider.IsUnsubscribedFromSingleNewsletter(model.Email, newsletter.NewsletterID))
                                {
                                    mSubscriptionService.UnsubscribeFromSingleNewsletter(model.Email, newsletter.NewsletterID, issue?.IssueID);
                                }

                                model.UnsubscriptionResult = ResHelper.GetString("DancingGoatMvc.News.Unsubscribed");
                            }
                            else
                            {
                                ModelState.AddModelError(String.Empty, invalidUrlMessage);
                            }
                        }
                    }
                    catch (ArgumentException)
                    {
                        ModelState.AddModelError(String.Empty, invalidUrlMessage);
                    }
                }
                else
                {
                    ModelState.AddModelError(String.Empty, invalidUrlMessage);
                }
            }
            else
            {
                ModelState.AddModelError(String.Empty, invalidUrlMessage);
            }

            return(View(model));
        }
        //EndDocSection:Subscribe


        //DocSection:Unsubscribe
        /// <summary>
        /// Handles marketing email subscription cancellation requests.
        /// </summary>
        public IActionResult Unsubscribe(NewsletterUnsubscriptionViewModel model)
        {
            // Verifies that the subscription cancellation request contains all required parameters
            if (ModelState.IsValid)
            {
                // Confirms whether the hash in the subscription cancellation request is valid for the given email address
                // Provides protection against forged cancellation requests
                if (emailHashValidator.ValidateEmailHash(model.Hash, model.Email))
                {
                    // Gets the marketing email (issue) from which the cancellation request was sent
                    IssueInfo issue = issueInfoProvider.Get(model.IssueGuid, siteService.CurrentSite.SiteID);

                    if (model.UnsubscribeFromAll)
                    {
                        // Checks that the email address is not already unsubscribed from all marketing emails
                        if (!unsubscriptionProvider.IsUnsubscribedFromAllNewsletters(model.Email))
                        {
                            // Unsubscribes the specified email address from all marketing emails (adds it to the opt-out list)
                            subscriptionService.UnsubscribeFromAllNewsletters(model.Email, issue?.IssueID);
                        }
                    }
                    else
                    {
                        // Gets the email feed for which the cancellation was requested
                        NewsletterInfo newsletter = newsletterInfoProvider.Get(model.NewsletterGuid, siteService.CurrentSite.SiteID);

                        if (newsletter != null)
                        {
                            // Checks that the email address is not already unsubscribed from the specified email feed
                            if (!unsubscriptionProvider.IsUnsubscribedFromSingleNewsletter(model.Email, newsletter.NewsletterID))
                            {
                                // Unsubscribes the specified email address from the email feed
                                subscriptionService.UnsubscribeFromSingleNewsletter(model.Email, newsletter.NewsletterID, issue?.IssueID);
                            }
                        }
                    }

                    // Displays a view to inform the user that they were unsubscribed
                    return(RedirectToAction(nameof(UnsubscribeSuccess)));
                }
            }

            // If the subscription cancellation request was not successful, displays a view to inform the user
            // Failure can occur if the request does not provide all required parameters or if the hash is invalid
            return(RedirectToAction(nameof(UnsubscribeFailure)));
        }