feat: 添加密码值对象。

main
NoahLan 6 months ago
parent f4686f5d73
commit 6ccf2df930

@ -1,158 +1,148 @@
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.Shared.Enums; using NPin.Framework.Upms.Domain.Entities.ValueObjects;
using SqlSugar; using NPin.Framework.Upms.Domain.Shared.Enums;
using Volo.Abp.Auditing; using SqlSugar;
using Volo.Abp.Domain.Entities; using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
namespace NPin.Framework.Upms.Domain.Entities;
namespace NPin.Framework.Upms.Domain.Entities;
[SugarTable("User", "用户表")]
public class UserEntity : Entity<Guid>, ISoftDelete, IAuditedObject, IEnabled, IOrderNum [SugarTable("User", "用户表")]
{ public class UserEntity : Entity<Guid>, ISoftDelete, IAuditedObject, IEnabled, IOrderNum
#region User {
#region User
[SugarColumn(IsPrimaryKey = true)] public override Guid Id { get; protected set; }
[SugarColumn(IsPrimaryKey = true)] public override Guid Id { get; protected set; }
[SugarColumn(ColumnDescription = "用户名")]
public string Username { get; set; } = string.Empty; [SugarColumn(ColumnDescription = "用户名")]
public string Username { get; set; } = string.Empty;
[SugarColumn(ColumnDescription = "手机号码")]
public string PhoneNumber { get; set; } [SugarColumn(ColumnDescription = "手机号码")]
public string PhoneNumber { get; set; }
[SugarColumn(ColumnDescription = "邮箱")]
public string? Email { get; set; } [SugarColumn(ColumnDescription = "邮箱")]
public string? Email { get; set; }
[SugarColumn(ColumnDescription = "昵称")]
public string? Nickname { get; set; } [SugarColumn(ColumnDescription = "昵称")]
public string? Nickname { get; set; }
[SugarColumn(ColumnDescription = "密码")]
public string Password { get; set; } = string.Empty; [SugarColumn(ColumnDescription = "密码", IsOwnsOne = true)]
public EncryptPasswordValueObject? EncryptPassword { get; set; } = new EncryptPasswordValueObject();
[SugarColumn(ColumnDescription = "密码加盐值")]
public string Salt { get; set; } = string.Empty; [SugarColumn(ColumnDescription = "简介")]
public string? Introduction { get; set; }
[SugarColumn(ColumnDescription = "简介")]
public string? Introduction { get; set; } [SugarColumn(ColumnDescription = "性别")]
public GenderEnum Gender { get; set; } = GenderEnum.Secrecy;
[SugarColumn(ColumnDescription = "性别")]
public GenderEnum Gender { get; set; } = GenderEnum.Secrecy; [SugarColumn(ColumnDescription = "注册IP地址")]
public string? IpAddr { get; set; }
[SugarColumn(ColumnDescription = "注册IP地址")]
public string? IpAddr { get; set; } [SugarColumn(ColumnDescription = "头像")]
public string? Avatar { get; set; }
[SugarColumn(ColumnDescription = "头像")]
public string? Avatar { get; set; } #endregion
#endregion #region Implements
#region Implements /// <summary>
/// 逻辑删除
/// <summary> /// </summary>
/// 逻辑删除 public bool IsDeleted { get; }
/// </summary>
public bool IsDeleted { get; } public DateTime CreationTime { get; } = DateTime.Now;
public Guid? CreatorId { get; }
public DateTime CreationTime { get; } = DateTime.Now; public DateTime? LastModificationTime { get; }
public Guid? CreatorId { get; } public Guid? LastModifierId { get; }
public DateTime? LastModificationTime { get; }
public Guid? LastModifierId { get; } /// <summary>
/// 是否启用
/// <summary> /// </summary>
/// 是否启用 public bool IsEnabled { get; set; }
/// </summary>
public bool IsEnabled { get; set; } public int OrderNum { get; set; }
public int OrderNum { get; set; } #endregion
#endregion #region 关联关系(导航)
#region 关联关系(导航) /// <summary>
/// 用户元数据,保存可扩展数据
/// <summary> /// 这里不使用Json的原因是有些库不是很好支持
/// 用户元数据,保存可扩展数据 /// </summary>
/// 这里不使用Json的原因是有些库不是很好支持 [Navigate(NavigateType.OneToMany, nameof(UserMetaEntity.UserId))]
/// </summary> public List<UserMetaEntity> Metadata { get; set; }
[Navigate(NavigateType.OneToMany, nameof(UserMetaEntity.UserId))]
public List<UserMetaEntity> Metadata { get; set; } /// <summary>
/// 角色列表,多对多
/// <summary> /// </summary>
/// 角色列表,多对多 [Navigate(typeof(UserRoleEntity), nameof(UserRoleEntity.UserId), nameof(UserRoleEntity.RoleId))]
/// </summary> public List<RoleEntity> Roles { get; set; }
[Navigate(typeof(UserRoleEntity), nameof(UserRoleEntity.UserId), nameof(UserRoleEntity.RoleId))]
public List<RoleEntity> Roles { get; set; } /// <summary>
/// 岗位列表,多对多
/// <summary> /// </summary>
/// 岗位列表,多对多 [Navigate(typeof(UserPostEntity), nameof(UserPostEntity.UserId), nameof(UserPostEntity.PostId))]
/// </summary> public List<PostEntity> Posts { get; set; }
[Navigate(typeof(UserPostEntity), nameof(UserPostEntity.UserId), nameof(UserPostEntity.PostId))]
public List<PostEntity> Posts { get; set; } /// <summary>
/// 所在的组织机构列表,多对多
/// <summary> /// </summary>
/// 所在的组织机构列表,多对多 [Navigate(typeof(UserOrganizationEntity), nameof(UserOrganizationEntity.UserId),
/// </summary> nameof(UserOrganizationEntity.OrganizationId))]
[Navigate(typeof(UserOrganizationEntity), nameof(UserOrganizationEntity.UserId), public List<OrganizationEntity> Organizations { get; set; }
nameof(UserOrganizationEntity.OrganizationId))]
public List<OrganizationEntity> Organizations { get; set; } #endregion
#endregion public UserEntity()
{
public UserEntity() }
{
} public UserEntity(string username, string phoneNumber, string password, string? nickname)
{
public UserEntity(string username, string phoneNumber, string password, string? nickname) Username = username;
{ PhoneNumber = phoneNumber;
Username = username; if (username.IsNullOrEmpty())
PhoneNumber = phoneNumber; {
if (username.IsNullOrEmpty()) username = $"用户{phoneNumber}";
{ }
username = $"用户{phoneNumber}";
} Nickname = nickname.IsNullOrWhiteSpace() ? username : nickname;
EncryptPassword.Password = password;
Nickname = nickname.IsNullOrWhiteSpace() ? username : nickname;
EncryptPassword(password); BuildPassword();
} }
/// <summary> /// <summary>
/// 通过随机盐值给密码加密使用BCrypt算法 /// 通过随机盐值给密码加密使用BCrypt算法
/// </summary> /// </summary>
/// <param name="password"></param> /// <exception cref="ArgumentNullException"></exception>
/// <returns></returns> public UserEntity BuildPassword(string? password = null)
/// <exception cref="ArgumentNullException"></exception> {
public UserEntity EncryptPassword(string? password) // 若传入密码无值则使用原本Password
{ // 若原本Password依然无值则抛出参数异常
// 若传入密码无值则使用原本Password password ??= EncryptPassword?.Password ?? throw new ArgumentNullException(nameof(EncryptPassword.Password));
// 若原本Password依然无值则抛出参数异常
if (password == null) EncryptPassword.Salt = BCrypt.GenerateSalt();
{ EncryptPassword.Password = BCrypt.Generate(password, EncryptPassword.Salt, 0).ToString()!;
if (Password.IsNullOrEmpty())
{ return this;
throw new ArgumentNullException(nameof(Password)); }
}
/// <summary>
password = Password; /// 检查密码是否一致
} /// </summary>
/// <param name="password"></param>
Salt = BCrypt.GenerateSalt(); /// <returns></returns>
Password = BCrypt.Generate(password, Salt, 0).ToString()!; /// <exception cref="ArgumentNullException"></exception>
public bool CheckPassword(string password)
return this; {
} if (EncryptPassword?.Salt is null)
{
/// <summary> throw new ArgumentNullException(nameof(EncryptPassword.Salt));
/// 检查密码是否一致 }
/// </summary>
/// <param name="password"></param> return BCrypt.Check(EncryptPassword.Password, password, EncryptPassword.Salt, 0);
/// <returns></returns> }
/// <exception cref="ArgumentNullException"></exception>
public bool CheckPassword(string password)
{
if (Salt is null)
{
throw new ArgumentNullException(nameof(Salt));
}
return BCrypt.Check(Password, password, 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