Skip to content

Commit

Permalink
feat:register add email confirm code
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 19, 2024
1 parent 835e6c5 commit 26e6ab5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
22 changes: 11 additions & 11 deletions src/LinCms.Application.Contracts/Cms/Account/RegisterDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ namespace LinCms.Cms.Account;

public class RegisterEmailCodeInput : IValidatableObject
{
/// <summary>
/// 昵称
/// </summary>
[StringLength(10, MinimumLength = 2, ErrorMessage = "昵称长度必须在2~10之间")]
[Required(ErrorMessage = "昵称不可为空")]
public string Nickname { get; set; }

/// <summary>
/// 邮件
/// </summary>
Expand Down Expand Up @@ -46,6 +39,13 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
}
public class RegisterDto : RegisterEmailCodeInput
{
/// <summary>
/// 昵称
/// </summary>
[StringLength(10, MinimumLength = 2, ErrorMessage = "昵称长度必须在2~10之间")]
[Required(ErrorMessage = "昵称不可为空")]
public string Nickname { get; set; }

/// <summary>
/// 密码
/// </summary>
Expand All @@ -56,12 +56,12 @@ public class RegisterDto : RegisterEmailCodeInput
/// <summary>
/// 发送邮件时返回的唯一码,以保证用户请求与验证码是一个请求
/// </summary>
//[Required(ErrorMessage = "非法请求")]
//public string EmailCode { get; set; }
[Required(ErrorMessage = "请获取邮件验证码")]
public string EmailCode { get; set; }

/// <summary>
/// 邮件发送的验证码
/// </summary>
//[Required(ErrorMessage = "邮件发送的验证码不能为空")]
//public string VerificationCode { get; set; }
[Required(ErrorMessage = "邮件发送的验证码不能为空")]
public string VerificationCode { get; set; }
}
7 changes: 6 additions & 1 deletion src/LinCms.Application.Contracts/Cms/Admins/UserSearchDto.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using LinCms.Data;
using JetBrains.Annotations;
using LinCms.Data;

namespace LinCms.Cms.Admins;

public class UserSearchDto : PageDto
{
public int? GroupId { get; set; }

[CanBeNull] public string Email { get; set; }
[CanBeNull] public string Nickname { get; set; }
[CanBeNull] public string Username { get; set; }
}
18 changes: 14 additions & 4 deletions src/LinCms.Application.Contracts/LinCms.Application.Contracts.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/LinCms.Application/Cms/Account/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public async Task<string> SendEmailCodeAsync(RegisterEmailCodeInput registerDto)

var message = new MimeMessage();
message.From.Add(new MailboxAddress(_mailKitOptions.UserName, _mailKitOptions.UserName));
message.To.Add(new MailboxAddress(registerDto.Nickname, registerDto.Email));
message.To.Add(new MailboxAddress(registerDto.Email, registerDto.Email));
message.Subject = $"vvlog-你的验证码是";

string uuid = Guid.NewGuid().ToString();
Expand All @@ -129,7 +129,7 @@ public async Task<string> SendEmailCodeAsync(RegisterEmailCodeInput registerDto)

message.Body = new TextPart("html")
{
Text = $@"{registerDto.Nickname},您好!</br>你此次验证码如下,请在 30 分钟内输入验证码进行下一步操作。</br>如非你本人操作,请忽略此邮件。</br>{verificationCode}"
Text = $@"{registerDto.Email},您好!</br>你此次验证码如下,请在 30 分钟内输入验证码进行下一步操作。</br>如非你本人操作,请忽略此邮件。</br>{verificationCode}"
};

await _emailSender.SendAsync(message);
Expand Down
3 changes: 3 additions & 0 deletions src/LinCms.Application/Cms/Users/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public PagedResultDto<UserDto> GetUserListByGroupId(UserSearchDto searchDto)
List<UserDto> linUsers = userRepository.Select
.IncludeMany(r => r.LinGroups)
.WhereIf(searchDto.GroupId != null, r => r.LinUserGroups.AsSelect().Any(u => u.GroupId == searchDto.GroupId))
.WhereIf(searchDto.Email.IsNotNullOrWhiteSpace(), r => r.Email.Contains(searchDto.Email))
.WhereIf(searchDto.Nickname.IsNotNullOrWhiteSpace(), r => r.Nickname.Contains(searchDto.Nickname))
.WhereIf(searchDto.Username.IsNotNullOrWhiteSpace(), r => r.Username.Contains(searchDto.Username))
.OrderByDescending(r => r.Id)
.ToPagerList(searchDto, out long totalCount)
.Select(r =>
Expand Down
32 changes: 18 additions & 14 deletions src/LinCms.Web/Controllers/Cms/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Threading.Tasks;
using FreeRedis;
using LinCms.Aop.Attributes;

namespace LinCms.Controllers.Cms;
Expand All @@ -35,14 +36,16 @@ public class AccountController : ApiControllerBase
private readonly IAuditBaseRepository<BlackRecord> _blackRecordRepository;
private readonly CaptchaOption _loginCaptchaOption;
private readonly ICaptchaManager _captchaManager;
public AccountController(IComponentContext componentContext, IConfiguration configuration, IAccountService accountService, IAuditBaseRepository<BlackRecord> blackRecordRepository, IUserService userService, IOptionsMonitor<CaptchaOption> loginCaptchaOption, ICaptchaManager captchaManager)
private readonly RedisClient _redisClient;
public AccountController(IComponentContext componentContext, IConfiguration configuration, IAccountService accountService, IAuditBaseRepository<BlackRecord> blackRecordRepository, IUserService userService, IOptionsMonitor<CaptchaOption> loginCaptchaOption, ICaptchaManager captchaManager, RedisClient redisClient)
{
bool isIdentityServer4 = configuration.GetSection("Service:IdentityServer4").Value?.ToBoolean() ?? false;
_tokenService = componentContext.ResolveNamed<ITokenService>(isIdentityServer4 ? nameof(IdentityServer4Service) : nameof(JwtTokenService));
_accountService = accountService;
_blackRecordRepository = blackRecordRepository;
_loginCaptchaOption = loginCaptchaOption.CurrentValue;
_captchaManager = captchaManager;
_redisClient = redisClient;
}


Expand Down Expand Up @@ -154,19 +157,20 @@ public async Task<string> SendEmailCodeAsync([FromBody] RegisterEmailCodeInput r
[HttpPost("account/register")]
public async Task<UnifyResponseDto> Register([FromBody] RegisterDto registerDto, [FromServices] IMapper mapper, [FromServices] IUserService userSevice)
{
//string uuid = await RedisHelper.GetAsync("SendEmailCode." + registerDto.Email);

//if (uuid != registerDto.EmailCode)
//{
// return UnifyResponseDto.Error("非法请求");
//}

//string verificationCode = await RedisHelper.GetAsync("SendEmailCode.VerificationCode" + registerDto.Email);
//if (verificationCode != registerDto.VerificationCode)
//{
// return UnifyResponseDto.Error("验证码不正确");
//}
//暂时设置直接激活,因前台未同步改造成功
string uuid = await _redisClient.GetAsync("SendEmailCode." + registerDto.Email);

if (uuid != registerDto.EmailCode)
{
return UnifyResponseDto.Error("非法请求");
}

string verificationCode = await _redisClient.GetAsync("SendEmailCode.VerificationCode" + registerDto.Email);
if (verificationCode != registerDto.VerificationCode)
{
return UnifyResponseDto.Error("验证码不正确");
}
//验证通过后,删除redis中的验证码
await _redisClient.DelAsync("SendEmailCode." + registerDto.Email);
LinUser user = mapper.Map<LinUser>(registerDto);
user.IsEmailConfirmed = true;
await userSevice.CreateAsync(user, new List<long>(), registerDto.Password);
Expand Down

0 comments on commit 26e6ab5

Please sign in to comment.