using Abp; using Abp.Domain.Entities; using Abp.Domain.Entities.Auditing; using Abp.Extensions; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace VberZero.BaseSystem.Users; [Table("Sys_Users")] public class User : FullAuditedEntity, IFullAudited, IMayHaveTenant, IPassivable { public static string DefaultPassword = "123qwe"; public static string AdminUserName = "admin"; public static string AdminPassword = "123iwb"; public static string SystemUserName = "system"; public static string SystemPassword = "system"; #region CONST public const int MaxUserNameLength = 256; public const int MaxEmailAddressLength = 256; public const int MaxNameLength = 64; public const int MaxSurnameLength = 64; public const int MaxAuthenticationSourceLength = 64; public const int MaxPasswordLength = 128; public const int MaxPlainPasswordLength = 32; public const int MaxAccountNoLength = 32; public const int MaxEmailConfirmationCodeLength = 328; public const int MaxPasswordResetCodeLength = 328; public const int MaxPhoneNumberLength = 32; public const int MaxSecurityStampLength = 128; public const int MaxConcurrencyStampLength = 128; public const int MaxAvatarPathLength = 500; #endregion CONST public virtual int? TenantId { get; set; } /// /// 用户名。 /// 用户名必须是唯一的。 /// [Required] [StringLength(MaxUserNameLength)] public virtual string UserName { get; set; } /// /// 名字 /// [Required] [StringLength(MaxNameLength)] public virtual string Name { get; set; } /// /// 姓氏 /// [Required] [StringLength(MaxSurnameLength)] public virtual string Surname { get; set; } /// /// 返回全名 ( Surname Name) /// [NotMapped] //public virtual string FullName => Surname + " " + Name; public virtual string FullName => Surname; [Required] [StringLength(MaxPasswordLength)] public virtual string Password { get; set; } /// /// 性别 /// public virtual VzDefinition.GenderType Gender { get; set; } /// /// /// 账户类型 /// public virtual VzDefinition.AccountType AccountType { get; set; } /// /// 用户级别 /// public virtual VzDefinition.UserType UserType { get; set; } /// /// 电话号码 /// [StringLength(MaxPhoneNumberLength)] public virtual string PhoneNumber { get; set; } /// /// 电子邮件地址。 /// 电子邮件地址必须是唯一的(可空)。 /// [Required] [StringLength(MaxEmailAddressLength)] public virtual string EmailAddress { get; set; } /// /// 头像 /// [StringLength(MaxAvatarPathLength)] public virtual string AvatarPath { get; set; } /// /// 关联账号 /// [StringLength(MaxAccountNoLength)] public virtual string AccountNo { get; set; } /// /// 电子邮件的确认码 /// [StringLength(MaxEmailConfirmationCodeLength)] public virtual string EmailConfirmationCode { get; set; } /// /// 是否已确认。 /// public virtual bool IsEmailConfirmed { get; set; } /// /// 是否已确认。 /// public virtual bool IsPhoneNumberConfirmed { get; set; } /// /// 重置密码的代码。 /// 如果为空则无效。 /// 一次使用,复位后必须设置为null。 /// [StringLength(MaxPasswordResetCodeLength)] public virtual string PasswordResetCode { get; set; } /// /// 锁定结束日期 /// public virtual DateTime? LockoutEndDateUtc { get; set; } /// /// 访问失败计数 /// public virtual int AccessFailedCount { get; set; } /// /// 是否启用锁定 /// public virtual bool IsLockoutEnabled { get; set; } /// /// 授权源名称。 /// 如果由外部源创建,则设置为外部身份验证源名称。 /// Default: null. /// [StringLength(MaxAuthenticationSourceLength)] public virtual string AuthenticationSource { get; set; } /// /// 防伪印章 /// [StringLength(MaxSecurityStampLength)] public virtual string SecurityStamp { get; set; } /// /// 是否启用双因素身份验证 /// public virtual bool IsTwoFactorEnabled { get; set; } /// /// 用户登陆 /// [ForeignKey("UserId")] public virtual ICollection Logins { get; set; } /// /// 用户角色 /// [ForeignKey("UserId")] public virtual ICollection Roles { get; set; } /// /// 用户声明 /// [ForeignKey("UserId")] public virtual ICollection Claims { get; set; } /// /// 用户权限 /// [ForeignKey("UserId")] public virtual ICollection Permissions { get; set; } /// /// 用户配置 /// [ForeignKey("UserId")] public virtual ICollection Settings { get; set; } /// /// 该用户是否活跃? /// 如果用户不活跃,他/她不能使用该应用程序。 /// public virtual bool IsActive { get; set; } [Required] [StringLength(MaxUserNameLength)] public virtual string NormalizedUserName { get; set; } [Required] [StringLength(MaxEmailAddressLength)] public virtual string NormalizedEmailAddress { get; set; } /// /// 用户持久化到库时必须更改的随机值 /// [StringLength(MaxConcurrencyStampLength)] public virtual string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString(); public virtual ICollection Tokens { get; set; } public virtual User DeleterUser { get; set; } public virtual User CreatorUser { get; set; } public virtual User LastModifierUser { get; set; } public virtual void SetNormalizedNames() { NormalizedUserName = UserName.ToUpperInvariant(); NormalizedEmailAddress = EmailAddress.ToUpperInvariant(); } public User() { IsActive = true; SecurityStamp = SequentialGuidGenerator.Instance.Create().ToString(); AccountType = VzDefinition.AccountType.Client; UserType = VzDefinition.UserType.Ordinary; Gender = VzDefinition.GenderType.Man; } public User(int? tenantId, string userName, string emailAddress = "", string phoneNumber = "") : this() { TenantId = tenantId; UserName = userName; EmailAddress = emailAddress; PhoneNumber = phoneNumber; SetNormalizedNames(); } public virtual void SetNewPasswordResetCode() { PasswordResetCode = Guid.NewGuid().ToString("N").Truncate(MaxPasswordResetCodeLength); } public virtual void SetNewEmailConfirmationCode() { EmailConfirmationCode = Guid.NewGuid().ToString("N").Truncate(MaxEmailConfirmationCodeLength); } /// /// 用户创建 。 /// /// public virtual UserIdentifier ToUserIdentifier() { return new UserIdentifier(TenantId, Id); } public void Unlock() { AccessFailedCount = 0; LockoutEndDateUtc = null; } public static User CreateTenantAdminUser(int tenantId, string emailAddress) { var user = new User { TenantId = tenantId, UserName = AdminUserName, Name = AdminUserName, Surname = AdminUserName, EmailAddress = emailAddress, AccountType = VzDefinition.AccountType.Client, UserType = VzDefinition.UserType.Ordinary, Roles = new List() }; user.SetNormalizedNames(); return user; } public static string CreateRandomPassword() { return Guid.NewGuid().ToString("N").Truncate(MaxPasswordLength); } public override string ToString() { return $"[User {Id}] {UserName}"; } }