Exemplo n.º 1
0
        public ActionResult ChangeEmail(ChangeEmailModel changeEmail)
        {
            try
            {
                // Validate.

                changeEmail.EmailAddress          = changeEmail.EmailAddress == null ? null : changeEmail.EmailAddress.Trim();
                changeEmail.SecondaryEmailAddress = changeEmail.SecondaryEmailAddress == null ? null : changeEmail.SecondaryEmailAddress.Trim();
                changeEmail.Validate();

                // If changing the email address make sure it is not already being used.

                var member = CurrentMember;
                if (changeEmail.EmailAddress != member.EmailAddresses[0].Address)
                {
                    if (_loginCredentialsQuery.DoCredentialsExist(new LoginCredentials {
                        LoginId = changeEmail.EmailAddress
                    }))
                    {
                        throw new DuplicateUserException();
                    }
                }

                // Change the email addresses and login id for the user.

                member.EmailAddresses = new List <EmailAddress> {
                    new EmailAddress {
                        Address = changeEmail.EmailAddress, IsVerified = false
                    }
                };
                if (!string.IsNullOrEmpty(changeEmail.SecondaryEmailAddress))
                {
                    member.EmailAddresses.Add(new EmailAddress {
                        Address = changeEmail.SecondaryEmailAddress, IsVerified = false
                    });
                }
                _memberAccountsCommand.UpdateMember(member);

                // Send activation.

                _accountVerificationsCommand.SendReactivation(member);
                _accountVerificationsCommand.StartActivationWorkflow(member);

                if (member.EmailAddresses.Count > 1 && !member.EmailAddresses[1].IsVerified)
                {
                    _accountVerificationsCommand.SendVerification(member, member.EmailAddresses[1].Address);
                }

                // Redirect.

                return(RedirectToRoute(AccountsRoutes.ActivationSent));
            }
            catch (UserException ex)
            {
                ModelState.AddModelError(ex, new ActivationErrorHandler());
            }

            return(View(changeEmail));
        }