IwbSysUser.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.Permissions;
  10. using Microsoft.AspNet.Identity;
  11. namespace IwbZero.Authorization.Users
  12. {
  13. /// <summary>
  14. /// Represents a user.
  15. /// </summary>
  16. public abstract class IwbSysUser<TUser> : UserBase, IUser<long>, IFullAudited<TUser>
  17. where TUser : IwbSysUser<TUser>
  18. {
  19. public virtual TUser DeleterUser { get; set; }
  20. public virtual TUser CreatorUser { get; set; }
  21. public virtual TUser LastModifierUser { get; set; }
  22. //public static IwbUser CreateAdminUser(string emailAddress, string password = null)
  23. //{
  24. // password = password ?? DefaultPassword;
  25. // var user = new IwbUser()
  26. // {
  27. // UserName = AdminUserName,
  28. // RealName = AdminUserName,
  29. // EmailAddress = emailAddress,
  30. // Password = new PasswordHasher().HashPassword(password)
  31. // };
  32. // return user;
  33. //}
  34. }
  35. [Table("Sys_Users")]
  36. public abstract class UserBase : FullAuditedEntity<long>, IPassivable
  37. {
  38. /// <summary>
  39. /// Maximum length of the <see cref="UserName"/> property.
  40. /// </summary>
  41. public const int MaxUserNameLength = 32;
  42. /// <summary>
  43. /// Maximum length of the <see cref="EmailAddress"/> property.
  44. /// </summary>
  45. public const int MaxEmailAddressLength = 256;
  46. /// <summary>
  47. /// Maximum length of the <see cref="RealName"/> property.
  48. /// </summary>
  49. public const int MaxNameLength = 32;
  50. ///// <summary>
  51. ///// Maximum length of the <see cref="Surname"/> property.
  52. ///// </summary>
  53. //public const int MaxSurnameLength = 32;
  54. /// <summary>
  55. /// Maximum length of the <see cref="AuthenticationSource"/> property.
  56. /// </summary>
  57. public const int MaxAuthenticationSourceLength = 64;
  58. /// <summary>
  59. /// UserName of the admin.
  60. /// admin can not be deleted and UserName of the admin can not be changed.
  61. /// </summary>
  62. public const string AdminUserName = "admin";
  63. public const string SystemUserName = "system";
  64. /// <summary>
  65. /// Maximum length of the <see cref="Password"/> property.
  66. /// </summary>
  67. public const int MaxPasswordLength = 128;
  68. /// <summary>
  69. /// Maximum length of the <see cref="Password"/> without hashed.
  70. /// </summary>
  71. public const int MaxPlainPasswordLength = 32;
  72. /// <summary>
  73. /// Maximum length of the <see cref="EmailConfirmationCode"/> property.
  74. /// </summary>
  75. public const int MaxEmailConfirmationCodeLength = 328;
  76. /// <summary>
  77. /// Maximum length of the <see cref="PasswordResetCode"/> property.
  78. /// </summary>
  79. public const int MaxPasswordResetCodeLength = 328;
  80. /// <summary>
  81. /// Maximum length of the <see cref="PhoneNumber"/> property.
  82. /// </summary>
  83. public const int MaxPhoneNumberLength = 32;
  84. /// <summary>
  85. /// Maximum length of the <see cref="SecurityStamp"/> property.
  86. /// </summary>
  87. public const int MaxSecurityStampLength = 128;
  88. /// <summary>
  89. /// User name.
  90. /// User name must be unique for it's tenant.
  91. /// </summary>
  92. [Required]
  93. [StringLength(MaxUserNameLength)]
  94. public virtual string UserName { get; set; }
  95. public virtual int UserType { get; set; }
  96. /// <summary>
  97. /// Authorization source name.
  98. /// It's set to external authentication source name if created by an external source.
  99. /// Default: null.
  100. /// </summary>
  101. [MaxLength(MaxAuthenticationSourceLength)]
  102. public virtual string AuthenticationSource { get; set; }
  103. /// <summary>
  104. /// Email address of the user.
  105. /// Email address must be unique for it's tenant.
  106. /// </summary>
  107. [Required]
  108. [StringLength(MaxEmailAddressLength)]
  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 RealName { get; set; }
  116. ///// <summary>
  117. ///// Surname of the user.
  118. ///// </summary>
  119. //[Required]
  120. //[StringLength(MaxSurnameLength)]
  121. //public virtual string Surname { get; set; }
  122. ///// <summary>
  123. ///// Return full name (Name Surname )
  124. ///// </summary>
  125. //[NotMapped]
  126. //public virtual string FullName { get { return this.RealName + " " + this.Surname; } }
  127. /// <summary>
  128. /// Password of the user.
  129. /// </summary>
  130. [Required]
  131. [StringLength(MaxPasswordLength)]
  132. public virtual string Password { get; set; }
  133. /// <summary>
  134. /// Confirmation code for email.
  135. /// </summary>
  136. [StringLength(MaxEmailConfirmationCodeLength)]
  137. public virtual string EmailConfirmationCode { get; set; }
  138. /// <summary>
  139. /// Reset code for password.
  140. /// It's not valid if it's null.
  141. /// It's for one usage and must be set to null after reset.
  142. /// </summary>
  143. [StringLength(MaxPasswordResetCodeLength)]
  144. public virtual string PasswordResetCode { get; set; }
  145. /// <summary>
  146. /// Lockout end date.
  147. /// </summary>
  148. public virtual DateTime? LockoutEndDateUtc { get; set; }
  149. /// <summary>
  150. /// Gets or sets the access failed count.
  151. /// </summary>
  152. public virtual int AccessFailedCount { get; set; }
  153. /// <summary>
  154. /// Gets or sets the lockout enabled.
  155. /// </summary>
  156. public virtual bool IsLockoutEnabled { get; set; }
  157. /// <summary>
  158. /// Gets or sets the phone number.
  159. /// </summary>
  160. [StringLength(MaxPhoneNumberLength)]
  161. public virtual string PhoneNumber { get; set; }
  162. /// <summary>
  163. /// Is the <see cref="PhoneNumber"/> confirmed.
  164. /// </summary>
  165. public virtual bool IsPhoneNumberConfirmed { get; set; }
  166. /// <summary>
  167. /// Gets or sets the security stamp.
  168. /// </summary>
  169. [StringLength(MaxSecurityStampLength)]
  170. public virtual string SecurityStamp { get; set; }
  171. /// <summary>
  172. /// Is two factor auth enabled.
  173. /// </summary>
  174. public virtual bool IsTwoFactorEnabled { get; set; }
  175. /// <summary>
  176. /// Roles of this user.
  177. /// </summary>
  178. [ForeignKey("UserId")]
  179. public virtual ICollection<SysUserRole> Roles { get; set; }
  180. /// <summary>
  181. /// Is the <see cref="AbpUserBase.EmailAddress"/> confirmed.
  182. /// </summary>
  183. public virtual bool IsEmailConfirmed { get; set; }
  184. /// <summary>
  185. /// Is this user active?
  186. /// If as user is not active, he/she can not use the application.
  187. /// </summary>
  188. public virtual bool IsActive { get; set; }
  189. /// <summary>
  190. /// The last time this user entered to the system.
  191. /// </summary>
  192. public virtual DateTime? LastLoginTime { get; set; }
  193. protected UserBase()
  194. {
  195. IsActive = true;
  196. IsLockoutEnabled = true;
  197. SecurityStamp = SequentialGuidGenerator.Instance.Create().ToString();
  198. }
  199. public virtual void SetNewPasswordResetCode()
  200. {
  201. PasswordResetCode = Guid.NewGuid().ToString("N").Truncate(MaxPasswordResetCodeLength);
  202. }
  203. public virtual void SetNewEmailConfirmationCode()
  204. {
  205. EmailConfirmationCode = Guid.NewGuid().ToString("N").Truncate(MaxEmailConfirmationCodeLength);
  206. }
  207. /// <summary>
  208. /// Creates <see cref="UserIdentifier"/> from this User.
  209. /// </summary>
  210. /// <returns></returns>
  211. public virtual UserIdentifier ToUserIdentifier()
  212. {
  213. return new UserIdentifier(null, Id);
  214. }
  215. public override string ToString()
  216. {
  217. return $"[User {Id}] {UserName}";
  218. }
  219. }
  220. }