Esempio n. 1
0
        /// <summary>
        /// Sends an email for the given <see cref="EmailPurpose"/> to the <see cref="ApplicationUser"/>
        /// </summary>
        /// <param name="user">The <see cref="ApplicationUser"/> as the recipient of the email.</param>
        /// <param name="purpose">The <see cref="EmailPurpose"/> of the email.</param>
        private async Task SendCodeByEmail(ApplicationUser user, EmailPurpose purpose)
        {
            string code;
            var    deadline = DateTime.UtcNow.Add(_dataProtectionTokenProviderOptions.Value.TokenLifespan);

            // round down to full hours
            deadline = new DateTime(deadline.Year, deadline.Month, deadline.Day, deadline.Hour, 0, 0);

            switch (purpose)
            {
            case EmailPurpose.PleaseConfirmEmail:
                code = _dataProtector.Encrypt(user.Email, DateTimeOffset.UtcNow.Add(_dataProtectionTokenProviderOptions.Value.TokenLifespan)).Base64UrlEncode();
                _sendEmailTask.SetMessageCreator(new ChangeUserAccountCreator
                {
                    Parameters =
                    {
                        Email       = user.Email,
                        Subject     = _localizer["Please confirm your email address"].Value,
                        CallbackUrl = Url.Action(nameof(Register),                          nameof(Account),
                                                 new { Organization= _tenantContext.SiteContext.UrlSegmentValue,           code },
                                                 protocol: HttpContext.Request.Scheme),
                        DeadlineUtc      = deadline,
                        CultureInfo      = CultureInfo.CurrentUICulture,
                        TemplateNameTxt  = TemplateName.PleaseConfirmEmailTxt,
                        TemplateNameHtml = TemplateName.PleaseConfirmEmailHtml
                    }
                });
                break;

            case EmailPurpose.PasswordReset:
                code = (await _signInManager.UserManager.GeneratePasswordResetTokenAsync(user)).Base64UrlEncode();
                _sendEmailTask.SetMessageCreator(new ChangeUserAccountCreator
                {
                    Parameters =
                    {
                        Email       = user.Email,
                        Subject     = _localizer["Please confirm your email address"].Value,
                        CallbackUrl = Url.Action(nameof(ResetPassword),                     nameof(Account),
                                                 new { Organization= _tenantContext.SiteContext.UrlSegmentValue,           id = user.Id,code                            },
                                                 protocol: HttpContext.Request.Scheme),
                        DeadlineUtc      = deadline,
                        CultureInfo      = CultureInfo.CurrentUICulture,
                        TemplateNameTxt  = TemplateName.PasswordResetTxt,
                        TemplateNameHtml = TemplateName.PasswordResetHtml
                    }
                });
                break;

            default:
                _logger.LogError($"Illegal enum type for {nameof(EmailPurpose)}");
                break;
            }

            _queue.QueueTask(_sendEmailTask);
        }
Esempio n. 2
0
        private void SendEmail(ContactViewModel model)
        {
            _sendEmailTask.SetMessageCreator(new Emailing.Creators.ContactFormCreator
            {
                Parameters =
                {
                    ContactForm = model,
                    CultureInfo = CultureInfo.DefaultThreadCurrentUICulture ?? CultureInfo.CurrentCulture
                }
            });

            _queue.QueueTask(_sendEmailTask);
        }
Esempio n. 3
0
        private void SendFixtureNotification(long matchId)
        {
            _sendMailTask.SetMessageCreator(new ChangeFixtureCreator
            {
                Parameters =
                {
                    CultureInfo     = CultureInfo.DefaultThreadCurrentUICulture ?? CultureInfo.CurrentCulture,
                    ChangedByUserId = GetCurrentUserId(),
                    MatchId         = matchId,
                }
            });

            _queue.QueueTask(_sendMailTask);
        }
Esempio n. 4
0
        /// <summary>
        /// Sends mail messages to current and new email address
        /// </summary>
        /// <param name="user">The <see cref="ApplicationUser"/> as the recipient of the notification email.</param>
        /// <param name="newEmail">The new email address for the user that must be confirmed.</param>
        private async Task SendEmail(ApplicationUser user, string newEmail)
        {
            var deadline = DateTime.UtcNow.Add(_dataProtectionTokenProviderOptions.Value.TokenLifespan);

            // round down to full hours
            deadline = new DateTime(deadline.Year, deadline.Month, deadline.Day, deadline.Hour, 0, 0);
            var code = (await _userManager.GenerateChangeEmailTokenAsync(user, newEmail)).Base64UrlEncode();

            _sendEmailTask.SetMessageCreator(new ChangePrimaryUserEmailCreator
            {
                Parameters =
                {
                    Email       = user.Email,
                    NewEmail    = newEmail,
                    CallbackUrl = Url.Action(nameof(ConfirmNewPrimaryEmail),nameof(Manage),  new { Organization = _tenantContext.SiteContext.UrlSegmentValue, id = user.Id, code, e = newEmail.Base64UrlEncode() }, protocol: HttpContext.Request.Scheme),
                    DeadlineUtc = deadline,
                    CultureInfo = CultureInfo.CurrentUICulture,
                }
            });

            _queue.QueueTask(_sendEmailTask);
        }
Esempio n. 5
0
        public async Task <IActionResult> Confirm(bool done, CancellationToken cancellationToken)
        {
            var sessionModel = await GetModelFromSession(cancellationToken);

            if (!sessionModel.IsFromSession)
            {
                return(RedirectToAction(nameof(SelectTeam), new { Organization = _tenantContext.SiteContext.UrlSegmentValue }));
            }
            try
            {
                var teamInRoundEntity = new TeamInRoundEntity();
                try
                {
                    // If the team had already been registered for another round, we have get the existing entity
                    if (!sessionModel.TeamInRound.IsNew)
                    {
                        teamInRoundEntity =
                            (await _appDb.TeamInRoundRepository.GetTeamInRoundAsync(
                                 new PredicateExpression(TeamInRoundFields.Id == sessionModel.TeamInRound.Id),
                                 cancellationToken)).First();
                    }
                }
                catch (Exception e)
                {
                    _logger.LogCritical(e, $"{nameof(TeamInRoundEntity)} with ID {sessionModel.TeamInRound.Id} for team ID {sessionModel.TeamInRound.TeamId} not found");
                    throw;
                }

                var isNewApplication = teamInRoundEntity.IsNew;
                sessionModel.TeamInRound.MapFormFieldsToEntity(teamInRoundEntity);
                teamInRoundEntity.Team = new TeamEntity();
                sessionModel.Team.MapFormFieldsToEntity(teamInRoundEntity.Team);
                teamInRoundEntity.Team.Venue = new VenueEntity();
                sessionModel.Venue.MapFormFieldsToEntity(teamInRoundEntity.Team.Venue);



                // Adds the current user as team manager, unless she already is team manager
                await AddManagerToTeamEntity(teamInRoundEntity.Team, cancellationToken);

                if (await _appDb.GenericRepository.SaveEntityAsync(teamInRoundEntity, true, true, cancellationToken))
                {
                    HttpContext.Session.Remove(TeamApplicationSessionName);
                    TempData.Put <TeamApplicationMessageModel.TeamApplicationMessage>(
                        nameof(TeamApplicationMessageModel.TeamApplicationMessage),
                        new TeamApplicationMessageModel.TeamApplicationMessage
                    {
                        AlertType = SiteAlertTagHelper.AlertType.Success,
                        MessageId = TeamApplicationMessageModel.MessageId.ApplicationSuccess
                    });

                    _sendEmailTask.SetMessageCreator(new ConfirmTeamApplicationCreator
                    {
                        Parameters =
                        {
                            CultureInfo          = CultureInfo.DefaultThreadCurrentUICulture,
                            TeamId               = teamInRoundEntity.TeamId,
                            IsNewApplication     = isNewApplication,
                            RoundId              = teamInRoundEntity.RoundId,
                            RegisteredByUserId   = GetCurrentUserId(),
                            UrlToEditApplication = Url.Action(nameof(EditTeam),              nameof(TeamApplication),new { Organization = _tenantContext.SiteContext.UrlSegmentValue, teamId = teamInRoundEntity.TeamId }, Request.Scheme, Request.Host.ToString())
                        }
                    });

                    _queue.QueueTask(_sendEmailTask);

                    return(RedirectToAction(nameof(List), new { Organization = _tenantContext.SiteContext.UrlSegmentValue }));
                }

                throw new Exception($"Saving the {nameof(TeamInRoundEntity)} failed.");
            }
            catch (Exception e)
            {
                _logger.LogCritical(e, "Team application could not be saved.");
                HttpContext.Session.Remove(TeamApplicationSessionName);
                TempData.Put <TeamApplicationMessageModel.TeamApplicationMessage>(
                    nameof(TeamApplicationMessageModel.TeamApplicationMessage),
                    new TeamApplicationMessageModel.TeamApplicationMessage
                {
                    AlertType = SiteAlertTagHelper.AlertType.Danger,
                    MessageId = TeamApplicationMessageModel.MessageId.ApplicationFailure
                });
                return(RedirectToAction(nameof(List), new { Organization = _tenantContext.SiteContext.UrlSegmentValue }));
            }
        }