feat: 添加值对象

This commit is contained in:
陈淳
2024-04-22 18:06:09 +08:00
parent 58ff8f45cf
commit d389dcbedf
5 changed files with 57 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
using Yi.Framework.Core.Data;
using Yi.Framework.Core.Helper;
using Yi.Framework.Rbac.Domain.Entities.ValueObjects;
using Yi.Framework.Rbac.Domain.Shared.Enums;
namespace Yi.Framework.Rbac.Domain.Entities
@@ -22,7 +23,7 @@ namespace Yi.Framework.Rbac.Domain.Entities
public UserEntity(string userName, string password, long phone, string nick = "萌新")
{
UserName = userName;
Password = password;
EncryPassword.Password = password;
Phone = phone;
Nick = nick;
BuildPassword();
@@ -55,14 +56,20 @@ namespace Yi.Framework.Rbac.Domain.Entities
public string UserName { get; set; } = string.Empty;
/// <summary>
/// 密码
/// 加密密码
/// </summary>
public string Password { get; set; } = string.Empty;
[SugarColumn(IsOwnsOne = true)]
public EncryPasswordValueObject EncryPassword { get; set; }=new EncryPasswordValueObject();
/// <summary>
/// 加密盐值
/// </summary>
public string Salt { get; set; } = string.Empty;
///// <summary>
///// 密码
///// </summary>
//public string Password { get; set; } = string.Empty;
///// <summary>
///// 加密盐值
///// </summary>
//public string Salt { get; set; } = string.Empty;
/// <summary>
/// 头像
@@ -175,14 +182,14 @@ namespace Yi.Framework.Rbac.Domain.Entities
//如果不传值那就把自己的password当作传进来的password
if (password == null)
{
if (Password == null)
if (EncryPassword.Password == null)
{
throw new ArgumentNullException(nameof(Password));
throw new ArgumentNullException(nameof(EncryPassword.Password));
}
password = Password;
password = EncryPassword.Password;
}
Salt = MD5Helper.GenerateSalt();
Password = MD5Helper.SHA2Encode(password, Salt);
EncryPassword.Salt = MD5Helper.GenerateSalt();
EncryPassword.Password = MD5Helper.SHA2Encode(password, EncryPassword.Salt);
return this;
}
@@ -193,12 +200,12 @@ namespace Yi.Framework.Rbac.Domain.Entities
/// <returns></returns>
public bool JudgePassword(string password)
{
if (Salt is null)
if (EncryPassword.Salt is null)
{
throw new ArgumentNullException(Salt);
throw new ArgumentNullException(EncryPassword.Salt);
}
var p = MD5Helper.SHA2Encode(password, Salt);
if (Password == MD5Helper.SHA2Encode(password, Salt))
var p = MD5Helper.SHA2Encode(password, EncryPassword.Salt);
if (EncryPassword.Password == MD5Helper.SHA2Encode(password, EncryPassword.Salt))
{
return true;
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Values;
namespace Yi.Framework.Rbac.Domain.Entities.ValueObjects
{
public class EncryPasswordValueObject : ValueObject
{
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; } = string.Empty;
/// <summary>
/// 加密盐值
/// </summary>
public string Salt { get; set; } = string.Empty;
protected override IEnumerable<object> GetAtomicValues()
{
yield return Password;
yield return Salt;
}
}
}

View File

@@ -154,7 +154,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
userAction.Invoke(user);
}
if (user.Password == MD5Helper.SHA2Encode(password, user.Salt))
if (user.EncryPassword.Password == MD5Helper.SHA2Encode(password, user.EncryPassword.Salt))
{
return;
}
@@ -247,7 +247,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
throw new UserFriendlyException("无效更新!原密码错误!");
}
user.Password = newPassword;
user.EncryPassword.Password = newPassword;
user.BuildPassword();
await _repository.UpdateAsync(user);
}
@@ -262,7 +262,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
var user = await _repository.GetByIdAsync(userId);
// EntityHelper.TrySetId(user, () => GuidGenerator.Create(), true);
user.Password = password;
user.EncryPassword.Password = password;
user.BuildPassword();
return await _repository.UpdateAsync(user);
}

View File

@@ -152,8 +152,8 @@ namespace Yi.Framework.Rbac.Domain.Managers
//{
// throw new UserFriendlyException($"数据错误用户id{nameof(userId)} 不存在,请重新登录");
//}
user.Password = string.Empty;
user.Salt = string.Empty;
user.EncryPassword.Password = string.Empty;
user.EncryPassword.Salt = string.Empty;
//超级管理员特殊处理
if (UserConst.Admin.Equals(user.UserName))