AbpVnext系列<四> 定义用户实体
一、一个系统中,最开始要设计的是用户表,先简单的设计如下图。
如上,用户表里有登录ID,密码,密码盐,用户类别等等。那么用户实体就需要带登录验证,密码设置,用户类别设置,状态设置等。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 设置密码 /// </summary> /// <param name="password"></param> public void SetPassword(string password) { PwdSalt = Guid.NewGuid().ToString(); Password = GetNewPassword(password, PwdSalt); } public string GetNewPassword(string password,string pwdSalt) { string pwd = (password + pwdSalt).ToMd5(); return pwd; } /// <summary> /// 更新密码 /// </summary> /// <param name="password"></param> public void UpdatePassword(string password) { Password = GetNewPassword(password, PwdSalt); } /// <summary> /// 设置状态 /// </summary> /// <param name="status"></param> public void SetStatus(NormalLockedStatus status) { Status = status; } /// <summary> /// 密码是否正确 /// </summary> /// <param name="password"></param> /// <returns></returns> public bool IsTruePassword(string password) { return Password == GetNewPassword(password, PwdSalt); } /// <summary> /// 登录 /// </summary> /// <param name="errorMessageService"></param> /// <param name="password"></param> public bool Login(string password) { if (Status.Equals(NormalLockedStatus.Locked)) { return false; } if (!IsTruePassword(password)) { return false; } return true; }
二、用户实体
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// /// </summary> /// <param name="id"></param> public User(long id) { Id = id; Status = NormalLockedStatus.Normal; } /// <summary> /// 租户Id /// </summary> public virtual Guid? TenantId { get; set; } /// <summary> /// 登录账号 /// </summary> public virtual string LoginName { get; set; } /// <summary> /// 昵称/姓名 /// </summary> public virtual string Nickname { get; set; } /// <summary> /// 密码 /// </summary> public virtual string Password { get; set; } /// <summary> /// 密码盐 /// </summary> public virtual string PwdSalt { get; set; } /// <summary> /// 员工号 /// </summary> public virtual string EmployeeID { get; set; } /// <summary> /// 账号类型: ( 1: 超级管理员 2:平台管理员,3:租户管理员) /// </summary> public virtual UserType UserType { get; set; } /// <summary> /// 备注 /// </summary> public virtual string Memo { get; set; } /// <summary> /// 状态(1-正常,2-锁定) /// </summary> public virtual NormalLockedStatus Status { get; set; }
三、在EFcore项目中的AidenAdminDbContextModelCreatingExtensions文件下添加表的属性
public static void ConfigureAidenAdmin( this ModelBuilder builder) { Check.NotNull(builder, nameof(builder)); builder.Entity<User>(b => { b.ConfigureFullAuditedAggregateRoot(); b.ToTable(AidenAdminDbProperties.DbTablePrefix + "User", AidenAdminDbProperties.DbSchema); b.ConfigureConcurrencyStamp(); b.ConfigureExtraProperties(); b.ConfigureFullAuditedAggregateRoot(); b.Property(x => x.Id).IsRequired(); b.Property(x => x.UserType).IsRequired().HasDefaultValue(UserType.Tenant); b.Property(x => x.Nickname).IsRequired().HasMaxLength(32); b.Property(x => x.Password).IsRequired().HasMaxLength(64); b.Property(x => x.LoginName).HasMaxLength(64); b.Property(x => x.PwdSalt).HasMaxLength(64); b.HasIndex(q => q.LoginName); b.HasIndex(q => q.EmployeeId); }); }
四、执行命令更新到数据库
add-migration 名称
update-database