User.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using Abp;
  2. using Abp.Domain.Entities;
  3. using Abp.Domain.Entities.Auditing;
  4. using Abp.Extensions;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.ComponentModel.DataAnnotations.Schema;
  7. namespace VberZero.BaseSystem.Users;
  8. [Table("Sys_Users")]
  9. public class User : FullAuditedEntity<long>, IFullAudited<User>, IMayHaveTenant, IPassivable
  10. {
  11. public static string DefaultPassword = "123qwe";
  12. public static string AdminUserName = "admin";
  13. public static string AdminPassword = "123iwb";
  14. public static string SystemUserName = "system";
  15. public static string SystemPassword = "system";
  16. #region CONST
  17. public const int MaxUserNameLength = 256;
  18. public const int MaxEmailAddressLength = 256;
  19. public const int MaxNameLength = 64;
  20. public const int MaxSurnameLength = 64;
  21. public const int MaxAuthenticationSourceLength = 64;
  22. public const int MaxPasswordLength = 128;
  23. public const int MaxPlainPasswordLength = 32;
  24. public const int MaxAccountNoLength = 32;
  25. public const int MaxEmailConfirmationCodeLength = 328;
  26. public const int MaxPasswordResetCodeLength = 328;
  27. public const int MaxPhoneNumberLength = 32;
  28. public const int MaxSecurityStampLength = 128;
  29. public const int MaxConcurrencyStampLength = 128;
  30. public const int MaxAvatarPathLength = 500;
  31. #endregion CONST
  32. public virtual int? TenantId { get; set; }
  33. /// <summary>
  34. /// 用户名。
  35. /// 用户名必须是唯一的。
  36. /// </summary>
  37. [Required]
  38. [StringLength(MaxUserNameLength)]
  39. public virtual string UserName { get; set; }
  40. /// <summary>
  41. /// 名字
  42. /// </summary>
  43. [Required]
  44. [StringLength(MaxNameLength)]
  45. public virtual string Name { get; set; }
  46. /// <summary>
  47. /// 姓氏
  48. /// </summary>
  49. [Required]
  50. [StringLength(MaxSurnameLength)]
  51. public virtual string Surname { get; set; }
  52. /// <summary>
  53. /// 返回全名 ( Surname Name)
  54. /// </summary>
  55. [NotMapped]
  56. //public virtual string FullName => Surname + " " + Name;
  57. public virtual string FullName => Surname;
  58. [Required]
  59. [StringLength(MaxPasswordLength)]
  60. public virtual string Password { get; set; }
  61. /// <summary>
  62. /// 性别
  63. /// </summary>
  64. public virtual VzDefinition.GenderType Gender { get; set; }
  65. ///
  66. /// <summary>
  67. /// 账户类型
  68. /// </summary>
  69. public virtual VzDefinition.AccountType AccountType { get; set; }
  70. /// <summary>
  71. /// 用户级别
  72. /// </summary>
  73. public virtual VzDefinition.UserType UserType { get; set; }
  74. /// <summary>
  75. /// 电话号码
  76. /// </summary>
  77. [StringLength(MaxPhoneNumberLength)]
  78. public virtual string PhoneNumber { get; set; }
  79. /// <summary>
  80. /// 电子邮件地址。
  81. /// 电子邮件地址必须是唯一的(可空)。
  82. /// </summary>
  83. [Required]
  84. [StringLength(MaxEmailAddressLength)]
  85. public virtual string EmailAddress { get; set; }
  86. /// <summary>
  87. /// 头像
  88. /// </summary>
  89. [StringLength(MaxAvatarPathLength)]
  90. public virtual string AvatarPath { get; set; }
  91. /// <summary>
  92. /// 关联账号
  93. /// </summary>
  94. [StringLength(MaxAccountNoLength)]
  95. public virtual string AccountNo { get; set; }
  96. /// <summary>
  97. /// 电子邮件的确认码
  98. /// </summary>
  99. [StringLength(MaxEmailConfirmationCodeLength)]
  100. public virtual string EmailConfirmationCode { get; set; }
  101. /// <summary>
  102. /// <see cref="EmailAddress"/> 是否已确认。
  103. /// </summary>
  104. public virtual bool IsEmailConfirmed { get; set; }
  105. /// <summary>
  106. /// <see cref="PhoneNumber"/> 是否已确认。
  107. /// </summary>
  108. public virtual bool IsPhoneNumberConfirmed { get; set; }
  109. /// <summary>
  110. /// 重置密码的代码。
  111. /// 如果为空则无效。
  112. /// 一次使用,复位后必须设置为null。
  113. /// </summary>
  114. [StringLength(MaxPasswordResetCodeLength)]
  115. public virtual string PasswordResetCode { get; set; }
  116. /// <summary>
  117. /// 锁定结束日期
  118. /// </summary>
  119. public virtual DateTime? LockoutEndDateUtc { get; set; }
  120. /// <summary>
  121. /// 访问失败计数
  122. /// </summary>
  123. public virtual int AccessFailedCount { get; set; }
  124. /// <summary>
  125. /// 是否启用锁定
  126. /// </summary>
  127. public virtual bool IsLockoutEnabled { get; set; }
  128. /// <summary>
  129. /// 授权源名称。
  130. /// 如果由外部源创建,则设置为外部身份验证源名称。
  131. /// Default: null.
  132. /// </summary>
  133. [StringLength(MaxAuthenticationSourceLength)]
  134. public virtual string AuthenticationSource { get; set; }
  135. /// <summary>
  136. /// 防伪印章
  137. /// </summary>
  138. [StringLength(MaxSecurityStampLength)]
  139. public virtual string SecurityStamp { get; set; }
  140. /// <summary>
  141. /// 是否启用双因素身份验证
  142. /// </summary>
  143. public virtual bool IsTwoFactorEnabled { get; set; }
  144. /// <summary>
  145. /// 用户登陆
  146. /// </summary>
  147. [ForeignKey("UserId")]
  148. public virtual ICollection<UserLogin> Logins { get; set; }
  149. /// <summary>
  150. /// 用户角色
  151. /// </summary>
  152. [ForeignKey("UserId")]
  153. public virtual ICollection<UserRole> Roles { get; set; }
  154. /// <summary>
  155. /// 用户声明
  156. /// </summary>
  157. [ForeignKey("UserId")]
  158. public virtual ICollection<UserClaim> Claims { get; set; }
  159. /// <summary>
  160. /// 用户权限
  161. /// </summary>
  162. [ForeignKey("UserId")]
  163. public virtual ICollection<UserPermissionSetting> Permissions { get; set; }
  164. /// <summary>
  165. /// 用户配置
  166. /// </summary>
  167. [ForeignKey("UserId")]
  168. public virtual ICollection<SysSetting> Settings { get; set; }
  169. /// <summary>
  170. /// 该用户是否活跃?
  171. /// 如果用户不活跃,他/她不能使用该应用程序。
  172. /// </summary>
  173. public virtual bool IsActive { get; set; }
  174. [Required]
  175. [StringLength(MaxUserNameLength)]
  176. public virtual string NormalizedUserName { get; set; }
  177. [Required]
  178. [StringLength(MaxEmailAddressLength)]
  179. public virtual string NormalizedEmailAddress { get; set; }
  180. /// <summary>
  181. /// 用户持久化到库时必须更改的随机值
  182. /// </summary>
  183. [StringLength(MaxConcurrencyStampLength)]
  184. public virtual string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString();
  185. public virtual ICollection<UserToken> Tokens { get; set; }
  186. public virtual User DeleterUser { get; set; }
  187. public virtual User CreatorUser { get; set; }
  188. public virtual User LastModifierUser { get; set; }
  189. public virtual void SetNormalizedNames()
  190. {
  191. NormalizedUserName = UserName.ToUpperInvariant();
  192. NormalizedEmailAddress = EmailAddress.ToUpperInvariant();
  193. }
  194. public User()
  195. {
  196. IsActive = true;
  197. SecurityStamp = SequentialGuidGenerator.Instance.Create().ToString();
  198. AccountType = VzDefinition.AccountType.Client;
  199. UserType = VzDefinition.UserType.Ordinary;
  200. Gender = VzDefinition.GenderType.Man;
  201. }
  202. public User(int? tenantId, string userName, string emailAddress = "", string phoneNumber = "") : this()
  203. {
  204. TenantId = tenantId;
  205. UserName = userName;
  206. EmailAddress = emailAddress;
  207. PhoneNumber = phoneNumber;
  208. SetNormalizedNames();
  209. }
  210. public virtual void SetNewPasswordResetCode()
  211. {
  212. PasswordResetCode = Guid.NewGuid().ToString("N").Truncate(MaxPasswordResetCodeLength);
  213. }
  214. public virtual void SetNewEmailConfirmationCode()
  215. {
  216. EmailConfirmationCode = Guid.NewGuid().ToString("N").Truncate(MaxEmailConfirmationCodeLength);
  217. }
  218. /// <summary>
  219. /// 用户创建 <see cref="UserIdentifier"/>。
  220. /// </summary>
  221. /// <returns></returns>
  222. public virtual UserIdentifier ToUserIdentifier()
  223. {
  224. return new UserIdentifier(TenantId, Id);
  225. }
  226. public void Unlock()
  227. {
  228. AccessFailedCount = 0;
  229. LockoutEndDateUtc = null;
  230. }
  231. public static User CreateTenantAdminUser(int tenantId, string emailAddress)
  232. {
  233. var user = new User
  234. {
  235. TenantId = tenantId,
  236. UserName = AdminUserName,
  237. Name = AdminUserName,
  238. Surname = AdminUserName,
  239. EmailAddress = emailAddress,
  240. AccountType = VzDefinition.AccountType.Client,
  241. UserType = VzDefinition.UserType.Ordinary,
  242. Roles = new List<UserRole>()
  243. };
  244. user.SetNormalizedNames();
  245. return user;
  246. }
  247. public static string CreateRandomPassword()
  248. {
  249. return Guid.NewGuid().ToString("N").Truncate(MaxPasswordLength);
  250. }
  251. public override string ToString()
  252. {
  253. return $"[User {Id}] {UserName}";
  254. }
  255. }