UserBase.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using Abp;
  6. using Abp.Domain.Entities;
  7. using Abp.Domain.Entities.Auditing;
  8. using Abp.Extensions;
  9. using IwbZero.Authorization.Base.SystemInfo;
  10. namespace IwbZero.Authorization.Base.Users
  11. {
  12. /// <summary>
  13. /// Base class for user.
  14. /// </summary>
  15. [Table("Sys_Users")]
  16. public abstract class UserBase : FullAuditedEntity<long>, IMayHaveTenant, IPassivable
  17. {
  18. public const string AdminUserName = "admin";
  19. public const string SystemUserName = "system";
  20. public const string HostAdminName = "administrator";
  21. public const string HostSystemName = "systemHost";
  22. /// <summary>
  23. /// Maximum length of the <see cref="UserName"/> property.
  24. /// </summary>
  25. public const int MaxUserNameLength = 256;
  26. /// <summary>
  27. /// Maximum length of the <see cref="AccountNo"/> property.
  28. /// </summary>
  29. public const int MaxAccountNoLength = 100;
  30. /// <summary>
  31. /// Maximum length of the <see cref="ImagePath"/> property.
  32. /// </summary>
  33. public const int MaxImagePathLength = 1000;
  34. /// <summary>
  35. /// Maximum length of the <see cref="EmailAddress"/> property.
  36. /// </summary>
  37. public const int MaxEmailAddressLength = 256;
  38. /// <summary>
  39. /// Maximum length of the <see cref="Name"/> property.
  40. /// </summary>
  41. public const int MaxNameLength = 64;
  42. /// <summary>
  43. /// Maximum length of the <see cref="Surname"/> property.
  44. /// </summary>
  45. public const int MaxSurnameLength = 64;
  46. public const int MaxAvatarImagePathLength = 500;
  47. /// <summary>
  48. /// Maximum length of the <see cref="AuthenticationSource"/> property.
  49. /// </summary>
  50. public const int MaxAuthenticationSourceLength = 64;
  51. /// <summary>
  52. /// Maximum length of the <see cref="Password"/> property.
  53. /// </summary>
  54. public const int MaxPasswordLength = 128;
  55. /// <summary>
  56. /// Maximum length of the <see cref="Password"/> without hashed.
  57. /// </summary>
  58. public const int MaxPlainPasswordLength = 32;
  59. /// <summary>
  60. /// Maximum length of the <see cref="EmailConfirmationCode"/> property.
  61. /// </summary>
  62. public const int MaxEmailConfirmationCodeLength = 328;
  63. /// <summary>
  64. /// Maximum length of the <see cref="PasswordResetCode"/> property.
  65. /// </summary>
  66. public const int MaxPasswordResetCodeLength = 328;
  67. /// <summary>
  68. /// Maximum length of the <see cref="PhoneNumber"/> property.
  69. /// </summary>
  70. public const int MaxPhoneNumberLength = 32;
  71. /// <summary>
  72. /// Maximum length of the <see cref="SecurityStamp"/> property.
  73. /// </summary>
  74. public const int MaxSecurityStampLength = 128;
  75. /// <summary>
  76. /// Authorization source name.
  77. /// It's set to external authentication source name if created by an external source.
  78. /// Default: null.
  79. /// </summary>
  80. [StringLength(MaxAuthenticationSourceLength)]
  81. public virtual string AuthenticationSource { get; set; }
  82. /// <summary>
  83. /// User name.
  84. /// User name must be unique for it's tenant.
  85. /// </summary>
  86. [Required]
  87. [StringLength(MaxUserNameLength)]
  88. [Index]
  89. public virtual string UserName { get; set; }
  90. [Index]
  91. public virtual int UserType { get; set; }
  92. [Index]
  93. public virtual int? AccountType { get; set; }
  94. [StringLength(MaxAccountNoLength)]
  95. public virtual string AccountNo { get; set; }
  96. [StringLength(MaxImagePathLength)]
  97. public virtual string ImagePath { get; set; }
  98. /// <summary>
  99. /// Tenant Id of this user.
  100. /// </summary>
  101. public virtual int? TenantId { get; set; }
  102. /// <summary>
  103. /// Email address of the user.
  104. /// Email address must be unique for it's tenant.
  105. /// </summary>
  106. [Required]
  107. [StringLength(MaxEmailAddressLength)]
  108. [Index]
  109. public virtual string EmailAddress { get; set; }
  110. /// <summary>
  111. /// Name of the user.
  112. /// </summary>
  113. [Required]
  114. [StringLength(MaxNameLength)]
  115. public virtual string Name { get; set; }
  116. /// <summary>
  117. /// Surname of the user.
  118. /// </summary>
  119. [StringLength(MaxSurnameLength)]
  120. public virtual string Surname { get; set; }
  121. /// <summary>
  122. /// Surname of the user.
  123. /// </summary>
  124. [StringLength(MaxAvatarImagePathLength)]
  125. public virtual string AvatarImagePath { get; set; }
  126. /// <summary>
  127. /// Return full name (Name Surname )
  128. /// </summary>
  129. [NotMapped]
  130. public virtual string FullName => Name + " " + Surname;
  131. /// <summary>
  132. /// Password of the user.
  133. /// </summary>
  134. [Required]
  135. [StringLength(MaxPasswordLength)]
  136. public virtual string Password { get; set; }
  137. /// <summary>
  138. /// Confirmation code for email.
  139. /// </summary>
  140. [StringLength(MaxEmailConfirmationCodeLength)]
  141. public virtual string EmailConfirmationCode { get; set; }
  142. /// <summary>
  143. /// Reset code for password.
  144. /// It's not valid if it's null.
  145. /// It's for one usage and must be set to null after reset.
  146. /// </summary>
  147. [StringLength(MaxPasswordResetCodeLength)]
  148. public virtual string PasswordResetCode { get; set; }
  149. /// <summary>
  150. /// Lockout end date.
  151. /// </summary>
  152. public virtual DateTime? LockoutEndDateUtc { get; set; }
  153. /// <summary>
  154. /// Gets or sets the access failed count.
  155. /// </summary>
  156. public virtual int AccessFailedCount { get; set; }
  157. /// <summary>
  158. /// Gets or sets the lockout enabled.
  159. /// </summary>
  160. public virtual bool IsLockoutEnabled { get; set; }
  161. /// <summary>
  162. /// Gets or sets the phone number.
  163. /// </summary>
  164. [StringLength(MaxPhoneNumberLength)]
  165. [Index]
  166. public virtual string PhoneNumber { get; set; }
  167. /// <summary>
  168. /// Is the <see cref="PhoneNumber"/> confirmed.
  169. /// </summary>
  170. public virtual bool IsPhoneNumberConfirmed { get; set; }
  171. /// <summary>
  172. /// Gets or sets the security stamp.
  173. /// </summary>
  174. [StringLength(MaxSecurityStampLength)]
  175. public virtual string SecurityStamp { get; set; }
  176. /// <summary>
  177. /// Is two factor auth enabled.
  178. /// </summary>
  179. public virtual bool IsTwoFactorEnabled { get; set; }
  180. /// <summary>
  181. /// Login definitions for this user.
  182. /// </summary>
  183. [ForeignKey("UserId")]
  184. public virtual ICollection<UserLogin> Logins { get; set; }
  185. /// <summary>
  186. /// Roles of this user.
  187. /// </summary>
  188. [ForeignKey("UserId")]
  189. public virtual ICollection<UserRole> Roles { get; set; }
  190. /// <summary>
  191. /// Claims of this user.
  192. /// </summary>
  193. [ForeignKey("UserId")]
  194. public virtual ICollection<UserClaim> Claims { get; set; }
  195. /// <summary>
  196. /// Settings for this user.
  197. /// </summary>
  198. [ForeignKey("UserId")]
  199. public virtual ICollection<SysSetting> Settings { get; set; }
  200. /// <summary>
  201. /// Is the <see cref="UserBase.EmailAddress"/> confirmed.
  202. /// </summary>
  203. public virtual bool IsEmailConfirmed { get; set; }
  204. /// <summary>
  205. /// Is this user active?
  206. /// If as user is not active, he/she can not use the application.
  207. /// </summary>
  208. public virtual bool IsActive { get; set; }
  209. protected UserBase()
  210. {
  211. // ReSharper disable VirtualMemberCallInConstructor
  212. IsActive = true;
  213. // ReSharper disable VirtualMemberCallInConstructor
  214. SecurityStamp = SequentialGuidGenerator.Instance.Create().ToString();
  215. }
  216. public virtual void SetNewPasswordResetCode()
  217. {
  218. PasswordResetCode = Guid.NewGuid().ToString("N").Truncate(MaxPasswordResetCodeLength);
  219. }
  220. public virtual void SetNewEmailConfirmationCode()
  221. {
  222. EmailConfirmationCode = Guid.NewGuid().ToString("N").Truncate(MaxEmailConfirmationCodeLength);
  223. }
  224. /// <summary>
  225. /// Creates <see cref="UserIdentifier"/> from this User.
  226. /// </summary>
  227. /// <returns></returns>
  228. public virtual UserIdentifier ToUserIdentifier()
  229. {
  230. return new UserIdentifier(TenantId, Id);
  231. }
  232. public override string ToString()
  233. {
  234. return $"[User {Id}] {UserName}";
  235. }
  236. }
  237. }