UserBase.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. [StringLength(MaxEmailAddressLength)]
  107. [Index]
  108. public virtual string EmailAddress { get; set; }
  109. /// <summary>
  110. /// Name of the user.
  111. /// </summary>
  112. [Required]
  113. [StringLength(MaxNameLength)]
  114. public virtual string Name { get; set; }
  115. /// <summary>
  116. /// Surname of the user.
  117. /// </summary>
  118. [StringLength(MaxSurnameLength)]
  119. public virtual string Surname { get; set; }
  120. /// <summary>
  121. /// Surname of the user.
  122. /// </summary>
  123. [StringLength(MaxAvatarImagePathLength)]
  124. public virtual string AvatarImagePath { get; set; }
  125. /// <summary>
  126. /// Return full name (Name Surname )
  127. /// </summary>
  128. [NotMapped]
  129. public virtual string FullName => Name + " " + Surname;
  130. /// <summary>
  131. /// Password of the user.
  132. /// </summary>
  133. [Required]
  134. [StringLength(MaxPasswordLength)]
  135. public virtual string Password { get; set; }
  136. /// <summary>
  137. /// Confirmation code for email.
  138. /// </summary>
  139. [StringLength(MaxEmailConfirmationCodeLength)]
  140. public virtual string EmailConfirmationCode { get; set; }
  141. /// <summary>
  142. /// Reset code for password.
  143. /// It's not valid if it's null.
  144. /// It's for one usage and must be set to null after reset.
  145. /// </summary>
  146. [StringLength(MaxPasswordResetCodeLength)]
  147. public virtual string PasswordResetCode { get; set; }
  148. /// <summary>
  149. /// Lockout end date.
  150. /// </summary>
  151. public virtual DateTime? LockoutEndDateUtc { get; set; }
  152. /// <summary>
  153. /// Gets or sets the access failed count.
  154. /// </summary>
  155. public virtual int AccessFailedCount { get; set; }
  156. /// <summary>
  157. /// Gets or sets the lockout enabled.
  158. /// </summary>
  159. public virtual bool IsLockoutEnabled { get; set; }
  160. /// <summary>
  161. /// Gets or sets the phone number.
  162. /// </summary>
  163. [StringLength(MaxPhoneNumberLength)]
  164. [Index]
  165. public virtual string PhoneNumber { get; set; }
  166. /// <summary>
  167. /// Is the <see cref="PhoneNumber"/> confirmed.
  168. /// </summary>
  169. public virtual bool IsPhoneNumberConfirmed { get; set; }
  170. /// <summary>
  171. /// Gets or sets the security stamp.
  172. /// </summary>
  173. [StringLength(MaxSecurityStampLength)]
  174. public virtual string SecurityStamp { get; set; }
  175. /// <summary>
  176. /// Is two factor auth enabled.
  177. /// </summary>
  178. public virtual bool IsTwoFactorEnabled { get; set; }
  179. /// <summary>
  180. /// Login definitions for this user.
  181. /// </summary>
  182. [ForeignKey("UserId")]
  183. public virtual ICollection<UserLogin> Logins { get; set; }
  184. /// <summary>
  185. /// Roles of this user.
  186. /// </summary>
  187. [ForeignKey("UserId")]
  188. public virtual ICollection<UserRole> Roles { get; set; }
  189. /// <summary>
  190. /// Claims of this user.
  191. /// </summary>
  192. [ForeignKey("UserId")]
  193. public virtual ICollection<UserClaim> Claims { get; set; }
  194. /// <summary>
  195. /// Settings for this user.
  196. /// </summary>
  197. [ForeignKey("UserId")]
  198. public virtual ICollection<SysSetting> Settings { get; set; }
  199. /// <summary>
  200. /// Is the <see cref="UserBase.EmailAddress"/> confirmed.
  201. /// </summary>
  202. public virtual bool IsEmailConfirmed { get; set; }
  203. /// <summary>
  204. /// Is this user active?
  205. /// If as user is not active, he/she can not use the application.
  206. /// </summary>
  207. public virtual bool IsActive { get; set; }
  208. protected UserBase()
  209. {
  210. // ReSharper disable VirtualMemberCallInConstructor
  211. IsActive = true;
  212. // ReSharper disable VirtualMemberCallInConstructor
  213. SecurityStamp = SequentialGuidGenerator.Instance.Create().ToString();
  214. }
  215. public virtual void SetNewPasswordResetCode()
  216. {
  217. PasswordResetCode = Guid.NewGuid().ToString("N").Truncate(MaxPasswordResetCodeLength);
  218. }
  219. public virtual void SetNewEmailConfirmationCode()
  220. {
  221. EmailConfirmationCode = Guid.NewGuid().ToString("N").Truncate(MaxEmailConfirmationCodeLength);
  222. }
  223. /// <summary>
  224. /// Creates <see cref="UserIdentifier"/> from this User.
  225. /// </summary>
  226. /// <returns></returns>
  227. public virtual UserIdentifier ToUserIdentifier()
  228. {
  229. return new UserIdentifier(TenantId, Id);
  230. }
  231. public override string ToString()
  232. {
  233. return $"[User {Id}] {UserName}";
  234. }
  235. }
  236. }