| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- 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<long>, IFullAudited<User>, 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; }
- /// <summary>
- /// 用户名。
- /// 用户名必须是唯一的。
- /// </summary>
- [Required]
- [StringLength(MaxUserNameLength)]
- public virtual string UserName { get; set; }
- /// <summary>
- /// 名字
- /// </summary>
- [Required]
- [StringLength(MaxNameLength)]
- public virtual string Name { get; set; }
- /// <summary>
- /// 姓氏
- /// </summary>
- [Required]
- [StringLength(MaxSurnameLength)]
- public virtual string Surname { get; set; }
- /// <summary>
- /// 返回全名 ( Surname Name)
- /// </summary>
- [NotMapped]
- //public virtual string FullName => Surname + " " + Name;
- public virtual string FullName => Surname;
- [Required]
- [StringLength(MaxPasswordLength)]
- public virtual string Password { get; set; }
- /// <summary>
- /// 性别
- /// </summary>
- public virtual VzDefinition.GenderType Gender { get; set; }
- ///
- /// <summary>
- /// 账户类型
- /// </summary>
- public virtual VzDefinition.AccountType AccountType { get; set; }
- /// <summary>
- /// 用户级别
- /// </summary>
- public virtual VzDefinition.UserType UserType { get; set; }
- /// <summary>
- /// 电话号码
- /// </summary>
- [StringLength(MaxPhoneNumberLength)]
- public virtual string PhoneNumber { get; set; }
- /// <summary>
- /// 电子邮件地址。
- /// 电子邮件地址必须是唯一的(可空)。
- /// </summary>
- [Required]
- [StringLength(MaxEmailAddressLength)]
- public virtual string EmailAddress { get; set; }
- /// <summary>
- /// 头像
- /// </summary>
- [StringLength(MaxAvatarPathLength)]
- public virtual string AvatarPath { get; set; }
- /// <summary>
- /// 关联账号
- /// </summary>
- [StringLength(MaxAccountNoLength)]
- public virtual string AccountNo { get; set; }
- /// <summary>
- /// 电子邮件的确认码
- /// </summary>
- [StringLength(MaxEmailConfirmationCodeLength)]
- public virtual string EmailConfirmationCode { get; set; }
- /// <summary>
- /// <see cref="EmailAddress"/> 是否已确认。
- /// </summary>
- public virtual bool IsEmailConfirmed { get; set; }
- /// <summary>
- /// <see cref="PhoneNumber"/> 是否已确认。
- /// </summary>
- public virtual bool IsPhoneNumberConfirmed { get; set; }
- /// <summary>
- /// 重置密码的代码。
- /// 如果为空则无效。
- /// 一次使用,复位后必须设置为null。
- /// </summary>
- [StringLength(MaxPasswordResetCodeLength)]
- public virtual string PasswordResetCode { get; set; }
- /// <summary>
- /// 锁定结束日期
- /// </summary>
- public virtual DateTime? LockoutEndDateUtc { get; set; }
- /// <summary>
- /// 访问失败计数
- /// </summary>
- public virtual int AccessFailedCount { get; set; }
- /// <summary>
- /// 是否启用锁定
- /// </summary>
- public virtual bool IsLockoutEnabled { get; set; }
- /// <summary>
- /// 授权源名称。
- /// 如果由外部源创建,则设置为外部身份验证源名称。
- /// Default: null.
- /// </summary>
- [StringLength(MaxAuthenticationSourceLength)]
- public virtual string AuthenticationSource { get; set; }
- /// <summary>
- /// 防伪印章
- /// </summary>
- [StringLength(MaxSecurityStampLength)]
- public virtual string SecurityStamp { get; set; }
- /// <summary>
- /// 是否启用双因素身份验证
- /// </summary>
- public virtual bool IsTwoFactorEnabled { get; set; }
- /// <summary>
- /// 用户登陆
- /// </summary>
- [ForeignKey("UserId")]
- public virtual ICollection<UserLogin> Logins { get; set; }
- /// <summary>
- /// 用户角色
- /// </summary>
- [ForeignKey("UserId")]
- public virtual ICollection<UserRole> Roles { get; set; }
- /// <summary>
- /// 用户声明
- /// </summary>
- [ForeignKey("UserId")]
- public virtual ICollection<UserClaim> Claims { get; set; }
- /// <summary>
- /// 用户权限
- /// </summary>
- [ForeignKey("UserId")]
- public virtual ICollection<UserPermissionSetting> Permissions { get; set; }
- /// <summary>
- /// 用户配置
- /// </summary>
- [ForeignKey("UserId")]
- public virtual ICollection<SysSetting> Settings { get; set; }
- /// <summary>
- /// 该用户是否活跃?
- /// 如果用户不活跃,他/她不能使用该应用程序。
- /// </summary>
- public virtual bool IsActive { get; set; }
- [Required]
- [StringLength(MaxUserNameLength)]
- public virtual string NormalizedUserName { get; set; }
- [Required]
- [StringLength(MaxEmailAddressLength)]
- public virtual string NormalizedEmailAddress { get; set; }
- /// <summary>
- /// 用户持久化到库时必须更改的随机值
- /// </summary>
- [StringLength(MaxConcurrencyStampLength)]
- public virtual string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString();
- public virtual ICollection<UserToken> 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);
- }
- /// <summary>
- /// 用户创建 <see cref="UserIdentifier"/>。
- /// </summary>
- /// <returns></returns>
- 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<UserRole>()
- };
- user.SetNormalizedNames();
- return user;
- }
- public static string CreateRandomPassword()
- {
- return Guid.NewGuid().ToString("N").Truncate(MaxPasswordLength);
- }
- public override string ToString()
- {
- return $"[User {Id}] {UserName}";
- }
- }
|