using MS.Microservice.Core; using MS.Microservice.Core.Domain; using MS.Microservice.Core.Domain.Entity; using MS.Microservice.Core.Extension; using System; using System.Collections.Generic; using System.Linq; using System.Text.Json.Serialization; namespace MS.Microservice.Domain.Aggregates.IdentityModel { public class User : EntityBase, IFullAuditTracker, IAggregateRoot { public const string ModernPasswordSaltMarker = "v2"; private bool _isDisabled; private string? _telephone; private DateTime? _deletedAt; private int _creatorId; private int _updatorId; private string? _email; private string? _name; private string? _password; private string? _account; private string? _salt; private string? _fzAccount; private string? _fzId; private User() { Roles = new List(); } [JsonConstructor] public User(string account, string password, string salt, bool isDisabled, string telephone, int creatorId, int updatorId, string email, string name, string fzAccount, string fzId) : this() { _password = password; _salt = salt; _isDisabled = isDisabled; _telephone = telephone; _creatorId = creatorId; _updatorId = updatorId; _email = email; _name = name; _account = account; _fzAccount = fzAccount; _fzId = fzId; } public void SetPasswordHash(string passwordHash) { ArgumentException.ThrowIfNullOrWhiteSpace(passwordHash); _password = passwordHash; _salt = ModernPasswordSaltMarker; } public bool HasModernPasswordHash() => !string.IsNullOrWhiteSpace(_password) && string.Equals(_salt, ModernPasswordSaltMarker, StringComparison.Ordinal); public string? Account { get => _account; private set => _account = value; } public string? Name { get => _name; private set => _name = value; } public string? Password { get => _password; private set => _password = value; } public string? Salt { get => _salt; private set => _salt = value; } public string? Telephone { get => _telephone; private set => _telephone = value; } public string? Email { get => _email; private set => _email = value; } public bool IsDisabled { get => _isDisabled; private set => _isDisabled = value; } public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } public DateTime? DeletedAt { get => _deletedAt; private set => _deletedAt = value; } public int CreatorId { get => _creatorId; private set => _creatorId = value; } public int UpdaterId => _updatorId; [Obsolete("Use UpdaterId instead. The legacy name remains mapped to the existing database column.")] public int UpdatorId { get => UpdaterId; private set => _updatorId = value; } public ICollection Roles { get; private set; } public string? FzAccount { get => _fzAccount; private set => _fzAccount = value; } public string? FzId { get => _fzId; private set => _fzId = value; } public override bool IsTransient() { return Id == 0; } public void AddRole(Role role) { if (Roles.Any(r => r.Id == role.Id)) { return; } Roles.Add(role); } public void AddRoles(ICollection roles) { foreach (var role in roles) { AddRole(role); } } public void Delete() => _deletedAt = DateTime.Now; /// /// 修改用户信息 /// /// 名称 空 不更新 /// 电话 空 不更新 /// 邮箱 空 不更新 /// 密码 空 不更新 /// 密码盐 空 不更新 internal void Update(string? name, string? telephone, string? email,string? password,string? salt) { if (name.IsNullOrEmpty() == false) _name = name; if (telephone.IsNullOrEmpty() == false) _telephone = telephone; if (email.IsNullOrEmpty() == false) _email = email; if (password.IsNullOrEmpty() == false && salt.IsNullOrEmpty() == false) { _password = password; _salt = salt; } } } }