feat: 添加密码值对象。

main
NoahLan 6 months ago
parent f4686f5d73
commit 6ccf2df930

@ -1,5 +1,6 @@
using NPin.Framework.Core.Crypt.BCrypt; using NPin.Framework.Core.Crypt.BCrypt;
using NPin.Framework.SqlSugarCore.Abstractions.Data; using NPin.Framework.SqlSugarCore.Abstractions.Data;
using NPin.Framework.Upms.Domain.Entities.ValueObjects;
using NPin.Framework.Upms.Domain.Shared.Enums; using NPin.Framework.Upms.Domain.Shared.Enums;
using SqlSugar; using SqlSugar;
using Volo.Abp.Auditing; using Volo.Abp.Auditing;
@ -26,11 +27,8 @@ public class UserEntity : Entity<Guid>, ISoftDelete, IAuditedObject, IEnabled, I
[SugarColumn(ColumnDescription = "昵称")] [SugarColumn(ColumnDescription = "昵称")]
public string? Nickname { get; set; } public string? Nickname { get; set; }
[SugarColumn(ColumnDescription = "密码")] [SugarColumn(ColumnDescription = "密码", IsOwnsOne = true)]
public string Password { get; set; } = string.Empty; public EncryptPasswordValueObject? EncryptPassword { get; set; } = new EncryptPasswordValueObject();
[SugarColumn(ColumnDescription = "密码加盐值")]
public string Salt { get; set; } = string.Empty;
[SugarColumn(ColumnDescription = "简介")] [SugarColumn(ColumnDescription = "简介")]
public string? Introduction { get; set; } public string? Introduction { get; set; }
@ -111,31 +109,23 @@ public class UserEntity : Entity<Guid>, ISoftDelete, IAuditedObject, IEnabled, I
} }
Nickname = nickname.IsNullOrWhiteSpace() ? username : nickname; Nickname = nickname.IsNullOrWhiteSpace() ? username : nickname;
EncryptPassword(password); EncryptPassword.Password = password;
BuildPassword();
} }
/// <summary> /// <summary>
/// 通过随机盐值给密码加密使用BCrypt算法 /// 通过随机盐值给密码加密使用BCrypt算法
/// </summary> /// </summary>
/// <param name="password"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentNullException"></exception>
public UserEntity EncryptPassword(string? password) public UserEntity BuildPassword(string? password = null)
{ {
// 若传入密码无值则使用原本Password // 若传入密码无值则使用原本Password
// 若原本Password依然无值则抛出参数异常 // 若原本Password依然无值则抛出参数异常
if (password == null) password ??= EncryptPassword?.Password ?? throw new ArgumentNullException(nameof(EncryptPassword.Password));
{
if (Password.IsNullOrEmpty())
{
throw new ArgumentNullException(nameof(Password));
}
password = Password;
}
Salt = BCrypt.GenerateSalt(); EncryptPassword.Salt = BCrypt.GenerateSalt();
Password = BCrypt.Generate(password, Salt, 0).ToString()!; EncryptPassword.Password = BCrypt.Generate(password, EncryptPassword.Salt, 0).ToString()!;
return this; return this;
} }
@ -148,11 +138,11 @@ public class UserEntity : Entity<Guid>, ISoftDelete, IAuditedObject, IEnabled, I
/// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentNullException"></exception>
public bool CheckPassword(string password) public bool CheckPassword(string password)
{ {
if (Salt is null) if (EncryptPassword?.Salt is null)
{ {
throw new ArgumentNullException(nameof(Salt)); throw new ArgumentNullException(nameof(EncryptPassword.Salt));
} }
return BCrypt.Check(Password, password, Salt, 0); return BCrypt.Check(EncryptPassword.Password, password, EncryptPassword.Salt, 0);
} }
} }

@ -0,0 +1,32 @@
using Volo.Abp.Domain.Values;
namespace NPin.Framework.Upms.Domain.Entities.ValueObjects;
public class EncryptPasswordValueObject : ValueObject
{
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; } = string.Empty;
/// <summary>
/// 加密盐值
/// </summary>
public string Salt { get; set; } = string.Empty;
public EncryptPasswordValueObject()
{
}
public EncryptPasswordValueObject(string password)
{
this.Password = password;
}
protected override IEnumerable<object> GetAtomicValues()
{
yield return Password;
yield return Salt;
}
}
Loading…
Cancel
Save