Skip to content

Commit

Permalink
fix: GlobalConfig scope
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed Apr 14, 2024
1 parent 82d6243 commit d1f5c51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/GZCTF/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class AccountController(
IHostEnvironment environment,
ICaptchaExtension captcha,
IOptionsSnapshot<AccountPolicy> accountPolicy,
IOptionsSnapshot<GlobalConfig> globalConfig,
UserManager<UserInfo> userManager,
SignInManager<UserInfo> signInManager,
ILogger<AccountController> logger,
Expand Down Expand Up @@ -110,7 +111,7 @@ public async Task<IActionResult> Register([FromBody] RegisterModel model, Cancel
}
else
{
if (!mailSender.SendConfirmEmailUrl(user.UserName, user.Email, link, localizer))
if (!mailSender.SendConfirmEmailUrl(user.UserName, user.Email, link, localizer, globalConfig))
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Account_EmailSendFailed)]));
}

Expand Down Expand Up @@ -174,7 +175,7 @@ public async Task<IActionResult> Recovery([FromBody] RecoveryModel model, Cancel
}
else
{
if (!mailSender.SendResetPasswordUrl(user.UserName, user.Email, link, localizer))
if (!mailSender.SendResetPasswordUrl(user.UserName, user.Email, link, localizer, globalConfig))
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Account_EmailSendFailed)]));
}

Expand Down Expand Up @@ -434,7 +435,7 @@ public async Task<IActionResult> ChangeEmail([FromBody] MailChangeModel model)
}
else
{
if (!mailSender.SendChangeEmailUrl(user!.UserName, model.NewMail, link, localizer))
if (!mailSender.SendChangeEmailUrl(user!.UserName, model.NewMail, link, localizer, globalConfig))
return BadRequest(new RequestResponse(localizer[nameof(Resources.Program.Account_EmailSendFailed)]));
}

Expand Down
13 changes: 9 additions & 4 deletions src/GZCTF/Services/Interface/IMailSender.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.Localization;
using GZCTF.Models.Internal;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;

namespace GZCTF.Services.Interface;

Expand Down Expand Up @@ -26,8 +28,9 @@ public interface IMailSender
/// <param name="email">用户新注册的Email</param>
/// <param name="confirmLink">确认链接</param>
/// <param name="localizer">本地化</param>
/// <param name="options">全局配置</param>
public bool SendConfirmEmailUrl(string? userName, string? email, string? confirmLink,
IStringLocalizer<Program> localizer);
IStringLocalizer<Program> localizer, IOptionsSnapshot<GlobalConfig> options);

/// <summary>
/// 发送邮箱重置邮件
Expand All @@ -36,8 +39,9 @@ public bool SendConfirmEmailUrl(string? userName, string? email, string? confirm
/// <param name="email">用户的电子邮件</param>
/// <param name="resetLink">重置链接</param>
/// <param name="localizer">本地化</param>
/// <param name="options">全局配置</param>
public bool SendChangeEmailUrl(string? userName, string? email, string? resetLink,
IStringLocalizer<Program> localizer);
IStringLocalizer<Program> localizer, IOptionsSnapshot<GlobalConfig> options);

/// <summary>
/// 发送密码重置邮件
Expand All @@ -46,6 +50,7 @@ public bool SendChangeEmailUrl(string? userName, string? email, string? resetLin
/// <param name="email">用户的电子邮件</param>
/// <param name="resetLink">重置链接</param>
/// <param name="localizer">本地化</param>
/// <param name="options">全局配置</param>
public bool SendResetPasswordUrl(string? userName, string? email, string? resetLink,
IStringLocalizer<Program> localizer);
IStringLocalizer<Program> localizer, IOptionsSnapshot<GlobalConfig> options);
}
30 changes: 15 additions & 15 deletions src/GZCTF/Services/MailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public sealed class MailSender : IMailSender, IDisposable
{
private readonly ConcurrentQueue<MailContent> _mailQueue = new();
private readonly EmailConfig? _options;
private readonly IOptions<GlobalConfig> _globalConfig;
private readonly ILogger<MailSender> _logger;
private readonly SmtpClient? _smtpClient;
private readonly CancellationTokenSource _cancellationTokenSource = new();
Expand All @@ -24,10 +23,8 @@ public sealed class MailSender : IMailSender, IDisposable

public MailSender(
IOptions<EmailConfig> options,
IOptions<GlobalConfig> globalConfig,
ILogger<MailSender> logger)
{
_globalConfig = globalConfig;
_logger = logger;
_options = options.Value;
_cancellationToken = _cancellationTokenSource.Token;
Expand Down Expand Up @@ -66,10 +63,10 @@ public async Task<bool> SendEmailAsync(string subject, string content, string to

public async Task SendUrlAsync(MailContent content)
{
var template = _globalConfig.Value.EmailTemplate switch
var template = content.GlobalConfig.EmailTemplate switch
{
GlobalConfig.DefaultEmailTemplate => content.Localizer[nameof(Resources.Program.MailSender_Template)],
_ => _globalConfig.Value.EmailTemplate
_ => content.GlobalConfig.EmailTemplate
};

// TODO: use a string formatter library
Expand All @@ -82,7 +79,7 @@ public async Task SendUrlAsync(MailContent content)
.Replace("{userName}", content.UserName)
.Replace("{url}", content.Url)
.Replace("{nowtime}", content.Time)
.Replace("{platform}", $"{_globalConfig.Value.Title}::CTF")
.Replace("{platform}", $"{content.GlobalConfig.Title}::CTF")
.ToString();

if (!await SendEmailAsync(content.Title, emailContent, content.Email))
Expand Down Expand Up @@ -134,19 +131,19 @@ await _smtpClient.AuthenticateAsync(_options!.UserName, _options.Password,
}

public bool SendConfirmEmailUrl(string? userName, string? email, string? confirmLink,
IStringLocalizer<Program> localizer) =>
SendUrlIfPossible(userName, email, confirmLink, MailType.ConfirmEmail, localizer);
IStringLocalizer<Program> localizer, IOptionsSnapshot<GlobalConfig> options) =>
SendUrlIfPossible(userName, email, confirmLink, MailType.ConfirmEmail, localizer, options);

public bool SendChangeEmailUrl(string? userName, string? email, string? resetLink,
IStringLocalizer<Program> localizer) =>
SendUrlIfPossible(userName, email, resetLink, MailType.ChangeEmail, localizer);
IStringLocalizer<Program> localizer, IOptionsSnapshot<GlobalConfig> options) =>
SendUrlIfPossible(userName, email, resetLink, MailType.ChangeEmail, localizer, options);

public bool SendResetPasswordUrl(string? userName, string? email, string? resetLink,
IStringLocalizer<Program> localizer) =>
SendUrlIfPossible(userName, email, resetLink, MailType.ResetPassword, localizer);
IStringLocalizer<Program> localizer, IOptionsSnapshot<GlobalConfig> options) =>
SendUrlIfPossible(userName, email, resetLink, MailType.ResetPassword, localizer, options);

bool SendUrlIfPossible(string? userName, string? email, string? resetLink, MailType type,
IStringLocalizer<Program> localizer)
IStringLocalizer<Program> localizer, IOptionsSnapshot<GlobalConfig> options)
{
if (_smtpClient is null)
return false;
Expand All @@ -158,7 +155,7 @@ bool SendUrlIfPossible(string? userName, string? email, string? resetLink, MailT
return false;
}

var content = new MailContent(userName, email, resetLink, type, localizer);
var content = new MailContent(userName, email, resetLink, type, localizer, options);

_mailQueue.Enqueue(content);
_resetEvent.Set();
Expand Down Expand Up @@ -198,7 +195,8 @@ public class MailContent(
string email,
string resetLink,
MailType type,
IStringLocalizer<Program> localizer)
IStringLocalizer<Program> localizer,
IOptionsSnapshot<GlobalConfig> globalConfig)
{
/// <summary>
/// 邮件标题
Expand Down Expand Up @@ -254,4 +252,6 @@ public class MailContent(
public string Time { get; set; } = DateTimeOffset.UtcNow.ToString("u");

public IStringLocalizer<Program> Localizer => localizer;

public GlobalConfig GlobalConfig => globalConfig.Value;
}

0 comments on commit d1f5c51

Please sign in to comment.