public void SendConfirmationToNewEmail(string codeValue)
        {
            _confirmationCodeService.ValidateCode(codeValue);

            _confirmationCodeService.DeactivateCode(codeValue);

            var user          = _confirmationCodeService.GetRelatedUser(codeValue);
            var newEmailValue = user.Emails.FindLast(x => x.IsConfirmed == false).Value;

            _senderService.SendConfirmation(newEmailValue, ConfirmationCodeType.EmailConfirmation);
        }
        public bool TrySendConfirmationToNewEmail(string codeValue)
        {
            try
            {
                _confirmationCodeService.ValidateCode(codeValue, ConfirmationCodeType.EmailChange);
            }
            catch (Exception ex)
                when(ex is CodeIsNotActiveException || ex is CodeIsNotExistException || ex is CodeExpirationDateIsUpException)
                {
                    return(false);
                }

            _confirmationCodeService.DeactivateCode(codeValue);

            var user          = _confirmationCodeService.GetRelatedUser(codeValue);
            var newEmailValue = user.Emails.FindLast(x => x.IsConfirmed == false).Value;

            _senderService.SendConfirmation(newEmailValue, ConfirmationCodeType.EmailChangeConfirmation);

            return(true);
        }
        public void RecoverPassword(string newPasswordValue, string codeValue)
        {
            _confirmationCodeService.ValidateCode(codeValue);

            var user = _confirmationCodeService.GetRelatedUser(codeValue);

            if (_passwordPolicy.SatisfiesPolicy(user, newPasswordValue))
            {
                newPasswordValue = _passwordHasher.GetHash(newPasswordValue);
                user.UpdatePassword(newPasswordValue);
                _confirmationCodeService.DeactivateCode(codeValue);
            }
        }
        public void RecoverPassword(string newPasswordValue, string codeValue)
        {
            _confirmationCodeService.ValidateCode(codeValue, Models.ConfirmationCodeType.PasswordRecovery);

            var user = _confirmationCodeService.GetRelatedUser(codeValue);

            if (!_passwordPolicy.SatisfiesPolicy(user, newPasswordValue))
            {
                throw new PasswordDoesNotSatisfyPolicyException("Password does not satisfy policy.");
            }

            newPasswordValue = _passwordHasher.GetHash(newPasswordValue);
            user.UpdatePassword(newPasswordValue);
            _confirmationCodeService.DeactivateCode(codeValue);
        }
        public bool TryAcceptConfirmation(string codeValue)
        {
            try
            {
                _confirmationCodeService.ValidateCode(codeValue);
            }
            catch (Exception ex)
                when(ex is CodeIsNotActiveException || ex is CodeIsNotExistException || ex is CodeIsNotValidException)
                {
                    return(false);
                }
            var user       = _confirmationCodeService.GetRelatedUser(codeValue);
            var emailValue = _confirmationCodeService.GetCodeByValue(codeValue).Email.Value;

            user.Emails.Find(e => e.Value == emailValue).IsConfirmed = true;

            _confirmationCodeService.DeactivateCode(codeValue);

            return(true);
        }