using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using Abp; using Abp.Domain.Entities; using Abp.Domain.Entities.Auditing; using Abp.Extensions; using IwbZero.Authorization.Permissions; using Microsoft.AspNet.Identity; namespace IwbZero.Authorization.Users { /// /// Represents a user. /// public abstract class IwbSysUser : UserBase, IUser, IFullAudited where TUser : IwbSysUser { public virtual TUser DeleterUser { get; set; } public virtual TUser CreatorUser { get; set; } public virtual TUser LastModifierUser { get; set; } //public static IwbUser CreateAdminUser(string emailAddress, string password = null) //{ // password = password ?? DefaultPassword; // var user = new IwbUser() // { // UserName = AdminUserName, // RealName = AdminUserName, // EmailAddress = emailAddress, // Password = new PasswordHasher().HashPassword(password) // }; // return user; //} } [Table("Sys_Users")] public abstract class UserBase : FullAuditedEntity, IPassivable { /// /// Maximum length of the property. /// public const int MaxUserNameLength = 32; /// /// Maximum length of the property. /// public const int MaxEmailAddressLength = 256; /// /// Maximum length of the property. /// public const int MaxNameLength = 32; ///// ///// Maximum length of the property. ///// //public const int MaxSurnameLength = 32; /// /// Maximum length of the property. /// public const int MaxAuthenticationSourceLength = 64; /// /// UserName of the admin. /// admin can not be deleted and UserName of the admin can not be changed. /// public const string AdminUserName = "admin"; public const string SystemUserName = "system"; /// /// Maximum length of the property. /// public const int MaxPasswordLength = 128; /// /// Maximum length of the without hashed. /// public const int MaxPlainPasswordLength = 32; /// /// Maximum length of the property. /// public const int MaxEmailConfirmationCodeLength = 328; /// /// Maximum length of the property. /// public const int MaxPasswordResetCodeLength = 328; /// /// Maximum length of the property. /// public const int MaxPhoneNumberLength = 32; /// /// Maximum length of the property. /// public const int MaxSecurityStampLength = 128; /// /// User name. /// User name must be unique for it's tenant. /// [Required] [StringLength(MaxUserNameLength)] public virtual string UserName { get; set; } public virtual int UserType { get; set; } /// /// Authorization source name. /// It's set to external authentication source name if created by an external source. /// Default: null. /// [MaxLength(MaxAuthenticationSourceLength)] public virtual string AuthenticationSource { get; set; } /// /// Email address of the user. /// Email address must be unique for it's tenant. /// [Required] [StringLength(MaxEmailAddressLength)] public virtual string EmailAddress { get; set; } /// /// Name of the user. /// [Required] [StringLength(MaxNameLength)] public virtual string RealName { get; set; } ///// ///// Surname of the user. ///// //[Required] //[StringLength(MaxSurnameLength)] //public virtual string Surname { get; set; } ///// ///// Return full name (Name Surname ) ///// //[NotMapped] //public virtual string FullName { get { return this.RealName + " " + this.Surname; } } /// /// Password of the user. /// [Required] [StringLength(MaxPasswordLength)] public virtual string Password { get; set; } /// /// Confirmation code for email. /// [StringLength(MaxEmailConfirmationCodeLength)] public virtual string EmailConfirmationCode { get; set; } /// /// Reset code for password. /// It's not valid if it's null. /// It's for one usage and must be set to null after reset. /// [StringLength(MaxPasswordResetCodeLength)] public virtual string PasswordResetCode { get; set; } /// /// Lockout end date. /// public virtual DateTime? LockoutEndDateUtc { get; set; } /// /// Gets or sets the access failed count. /// public virtual int AccessFailedCount { get; set; } /// /// Gets or sets the lockout enabled. /// public virtual bool IsLockoutEnabled { get; set; } /// /// Gets or sets the phone number. /// [StringLength(MaxPhoneNumberLength)] public virtual string PhoneNumber { get; set; } /// /// Is the confirmed. /// public virtual bool IsPhoneNumberConfirmed { get; set; } /// /// Gets or sets the security stamp. /// [StringLength(MaxSecurityStampLength)] public virtual string SecurityStamp { get; set; } /// /// Is two factor auth enabled. /// public virtual bool IsTwoFactorEnabled { get; set; } /// /// Roles of this user. /// [ForeignKey("UserId")] public virtual ICollection Roles { get; set; } /// /// Is the confirmed. /// public virtual bool IsEmailConfirmed { get; set; } /// /// Is this user active? /// If as user is not active, he/she can not use the application. /// public virtual bool IsActive { get; set; } /// /// The last time this user entered to the system. /// public virtual DateTime? LastLoginTime { get; set; } protected UserBase() { IsActive = true; IsLockoutEnabled = true; SecurityStamp = SequentialGuidGenerator.Instance.Create().ToString(); } public virtual void SetNewPasswordResetCode() { PasswordResetCode = Guid.NewGuid().ToString("N").Truncate(MaxPasswordResetCodeLength); } public virtual void SetNewEmailConfirmationCode() { EmailConfirmationCode = Guid.NewGuid().ToString("N").Truncate(MaxEmailConfirmationCodeLength); } /// /// Creates from this User. /// /// public virtual UserIdentifier ToUserIdentifier() { return new UserIdentifier(null, Id); } public override string ToString() { return $"[User {Id}] {UserName}"; } } }