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.Base.SystemInfo;
namespace IwbZero.Authorization.Base.Users
{
///
/// Base class for user.
///
[Table("Sys_Users")]
public abstract class UserBase : FullAuditedEntity, IMayHaveTenant, IPassivable
{
public const string AdminUserName = "admin";
public const string SystemUserName = "system";
public const string HostAdminName = "administrator";
public const string HostSystemName = "systemHost";
///
/// Maximum length of the property.
///
public const int MaxUserNameLength = 256;
///
/// Maximum length of the property.
///
public const int MaxAccountNoLength = 100;
///
/// Maximum length of the property.
///
public const int MaxImagePathLength = 1000;
///
/// Maximum length of the property.
///
public const int MaxEmailAddressLength = 256;
///
/// Maximum length of the property.
///
public const int MaxNameLength = 64;
///
/// Maximum length of the property.
///
public const int MaxSurnameLength = 64;
public const int MaxAvatarImagePathLength = 500;
///
/// Maximum length of the property.
///
public const int MaxAuthenticationSourceLength = 64;
///
/// 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;
///
/// Authorization source name.
/// It's set to external authentication source name if created by an external source.
/// Default: null.
///
[StringLength(MaxAuthenticationSourceLength)]
public virtual string AuthenticationSource { get; set; }
///
/// User name.
/// User name must be unique for it's tenant.
///
[Required]
[StringLength(MaxUserNameLength)]
[Index]
public virtual string UserName { get; set; }
[Index]
public virtual int UserType { get; set; }
[Index]
public virtual int? AccountType { get; set; }
[StringLength(MaxAccountNoLength)]
public virtual string AccountNo { get; set; }
[StringLength(MaxImagePathLength)]
public virtual string ImagePath { get; set; }
///
/// Tenant Id of this user.
///
public virtual int? TenantId { get; set; }
///
/// Email address of the user.
/// Email address must be unique for it's tenant.
///
[Required]
[StringLength(MaxEmailAddressLength)]
[Index]
public virtual string EmailAddress { get; set; }
///
/// Name of the user.
///
[Required]
[StringLength(MaxNameLength)]
public virtual string Name { get; set; }
///
/// Surname of the user.
///
[StringLength(MaxSurnameLength)]
public virtual string Surname { get; set; }
///
/// Surname of the user.
///
[StringLength(MaxAvatarImagePathLength)]
public virtual string AvatarImagePath { get; set; }
///
/// Return full name (Name Surname )
///
[NotMapped]
public virtual string FullName => Name + " " + 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)]
[Index]
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; }
///
/// Login definitions for this user.
///
[ForeignKey("UserId")]
public virtual ICollection Logins { get; set; }
///
/// Roles of this user.
///
[ForeignKey("UserId")]
public virtual ICollection Roles { get; set; }
///
/// Claims of this user.
///
[ForeignKey("UserId")]
public virtual ICollection Claims { get; set; }
///
/// Settings for this user.
///
[ForeignKey("UserId")]
public virtual ICollection Settings { 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; }
protected UserBase()
{
// ReSharper disable VirtualMemberCallInConstructor
IsActive = true;
// ReSharper disable VirtualMemberCallInConstructor
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(TenantId, Id);
}
public override string ToString()
{
return $"[User {Id}] {UserName}";
}
}
}