首页 > 其他分享 >ABP-VNext 用户权限管理系统实战02---用户权限表的创建与迁移

ABP-VNext 用户权限管理系统实战02---用户权限表的创建与迁移

时间:2023-11-22 21:33:05浏览次数:36  
标签:VNext COMMENT NULL get 用户 set 权限 public Comment

一、表实体建立

1、菜单表

 [Comment("菜单表")]
 [Table("t_identity_menu")]
 public class Menu : AuditedAggregateRoot<Guid>, ISoftDelete, IMultiTenant
 {
     [MaxLength(200)]
     [Comment("菜单名")]
     public string Name { get; set; }
 
     [MaxLength(50)]
     [Comment("菜单编码")]
     public int Code { get; set; }
    
     [MaxLength(36)]
     [Comment("父级菜单标识")]
     public Guid ParentId { get; set; }

     [Comment("层级")]
     public int Level { get; set; }

     [MaxLength(200)]
     [Comment("图标路径")]
     public string IconUrl { get; set; }

     [MaxLength(200)]
     [Comment("跳转路径")]
     public string RedirectUrl { get; set; }

     [Comment("菜单类型 0 APP 1 web")]
     public int MenuType { get; set; }


     [Comment("地址类型")]
     public int MenuInOut { get; set; }

  
     [Comment("排序")]
     public int Sort { get; set; }
   
     [Comment("是否启用")]
     public bool IsActive { get; set; }

     [Comment("是否删除")]
     public bool IsDeleted { get; set; }
  
     [Comment("租户Id")]
     public Guid? TenantId { set; get; }

    
     [Comment("用户归属系统Id")]
     [MaxLength(20)]
     public string SystemId { get; set; }
 
     [Comment("备注")]
     [MaxLength(500)]
     public string Remark { get; set; }
 }

2、角色权限中间表

  [Comment("角色权限中间表")]
  [Table("t_identity_role_authority")]
  public class RoleAuthority : AuditedAggregateRoot<Guid>, ISoftDelete, IMultiTenant
  {/// <summary>
   /// 角色id
   /// </summary>
      [MaxLength(36)]
      [Comment("角色标识")]
      public Guid RoleId { get; set; }

      /// <summary>
      /// 权限id
      /// </summary>
      [MaxLength(36)]
      [Comment("权限标识")]
      public Guid AuthId { get; set; }
     
      [Comment("是否删除")]
      public bool IsDeleted { get; set; }
      /// <summary>
      /// 租户Id
      /// </summary>
      public Guid? TenantId { get; set; }

      /// <summary>
      /// 构造函数
      /// </summary>
      /// <param name="id"></param>
      /// <param name="tenantId"></param>
      /// <param name="roleId"></param>
      /// <param name="authId"></param>
      public RoleAuthority(Guid id, Guid? tenantId, Guid roleId, Guid authId)
      {
          TenantId = tenantId;
          Id = id;
          RoleId = roleId;
          AuthId = authId;
      }
  }

3、角色扩展表

[Comment("角色扩展表")]
[Table("t_identity_role_expand")]
public class RoleExpand : AuditedAggregateRoot<Guid>
{

    public RoleExpand(Guid Id, int RoleType, Guid RoleId, int RoleStatus)
    {
        this.Id = Id;
        this.RoleId = RoleId;
        this.RoleType = RoleType;
        this.RoleStatus = RoleStatus;
    }

    [MaxLength(36)]
    [Comment("角色标识")]
    public Guid RoleId { get; set; }
   
    [Comment("角色类型")]
    public int RoleType { get; set; }
 
    [MaxLength(36)]
    [Comment("用户状态:0启用、1禁用")]
    public int RoleStatus { get; set; }
  
    [MaxLength(20)]
    [Comment("归属系统Id")]
    public string SystemId { get; set; }
}

4、角色菜单中间表

[Comment("角色菜单中间表")]
[Table("t_identity_role_menu")]
public class RoleMenu : AuditedAggregateRoot<Guid>, ISoftDelete
{
   
    [MaxLength(36)]
    [Comment("角色标识")]
    public Guid RoleId { get; set; }

   
    [MaxLength(36)]
    [Comment("菜单标识")]
    public Guid MenuId { get; set; }
    

    [Comment("是否删除")]
    public bool IsDeleted { get; set; }
}

5、用户扩展表

 [Comment("用户扩展表")]
 [Table("t_identity_user_expand")]
 public  class UserExpand : AuditedAggregateRoot<Guid>
 {
     public UserExpand(Guid Id, int UserType, Guid UserId, UserStatusEnum userStatus = UserStatusEnum.Enable, int isConfirm = 0)
     {
         this.Id = Id;
         this.UserId = UserId;
         this.UserType = UserType;
         this.UserStatus = userStatus;
         this.IsConfirm = isConfirm;
     }
  
     [MaxLength(36)]
     [Comment("用户标识")]
     public Guid UserId { get; set; }
    

     [Comment("用户类型")]
     public int UserType { get; set; }


     [Comment("用户状态")]
     public UserStatusEnum UserStatus { get; set; }

  

     [Comment("是否已确认结果,0未确认,1已确认")]
     public int IsConfirm { get; set; }

  
     [MaxLength(200)]
     [Comment("用户图像")]
     public string HeadFileUrl { get; set; }

  
     [MaxLength(20)]
     [Comment("归属系统Id")]
     public string SystemId { get; set; }
     [Comment("锁定状态")]
     public bool? IsLock { get; set; }
 
     [Comment("登录失败次数")]
     public int? LoginFailedCount { get; set; }

     [Comment("锁定时间")]
     public DateTime? LockTime { get; set; }


     [Comment("锁定人")]
     public Guid? LockUserId { get; set; }

    
     [Comment("解锁时间")]
     public DateTime? UnLockTime { get; set; }

  
     [Comment("解锁人")]
     public Guid? UnLockUserId { get; set; }
 }

 

二、配置实体到EFCORE

1、IdentityManagementDbContext中增加实体集

 2、IdentityManagementDbContextModelCreatingExtensions中增加实体和表的映射

 

三、建立公共的数据迁移项目

1、新建一个web项目

项目中引用对应的程序集:Volo.Abp.EntityFrameworkCore.MySQL、Microsoft.EntityFrameworkCore,引入对应要迁移的项目:Bridge.IdentityManagement.EntityFrameworkCore

注意版本号要统一。

2、新增EFCoreDbContextFactory类和EFCoreMigrationDbContext类

EFCoreDbContextFactory类,在类中配置了数据库类型,要用的配置文件

 public class EFCoreDbContextFactory : IDesignTimeDbContextFactory<EFCoreMigrationDbContext>
 {

     public EFCoreMigrationDbContext CreateDbContext(string[] args)
     {

         var configuration = BuildConfiguration();

         var builder = new DbContextOptionsBuilder<EFCoreMigrationDbContext>()
             .UseMySql(configuration.GetConnectionString("Default"),
             ServerVersion.AutoDetect(configuration.GetConnectionString("Default")), o => o.SchemaBehavior(MySqlSchemaBehavior.Ignore));

         return new EFCoreMigrationDbContext(builder.Options);
     }

     private static IConfigurationRoot BuildConfiguration()
     {
         var builder = new ConfigurationBuilder()
             .SetBasePath(Directory.GetCurrentDirectory())
             .AddJsonFile("appsettings.json", optional: false);

         return builder.Build();
     }
 }

EFCoreMigrationDbContext类中加和了要迁移的实体,要实体和表的映射扩展方法

public class EFCoreMigrationDbContext : AbpDbContext<EFCoreMigrationDbContext>
{
    public EFCoreMigrationDbContext(
        DbContextOptions<EFCoreMigrationDbContext> options
        ) : base(options)
    {

    }
    #region 用户权限管理模块
    public DbSet<Menu> Menus { get; set; }
    public DbSet<RoleAuthority> RoleAuthoritys { get; set; }
    public DbSet<Authority> Authoritys { get; set; }
    public DbSet<RoleMenu> RoleMenus { get; set; }
    public DbSet<UserExpand> UserExpands { get; set; }
    #endregion


    protected override void OnModelCreating(ModelBuilder builder)
    {


        builder.ConfigureIdentityManagement();
        base.OnModelCreating(builder);

    }
}

3、修改配置文件,增加数据库连接

"ConnectionStrings": {
  "Default": "Server=123.249.14.34;Port=3306; Database=bridge; User=root; Password=xxxx;"
},

 

四、迁移数据表

1、生成迁移文件

Add-Migration -Context EFCoreMigrationDbContext -Name bridge_identity_20231022 -OutputDir Migrations/EFCoreMigrationDb

 

  2、迁移数据库 方法一:生成sql脚本 如果你不想直接迁移到数据库,只生成sql脚本请用如下的命令 Script-Migration -Context EFCoreMigrationDbContext   如果想迁移指定文件的内容请用如下的命令,增加了from 和 to参数 Script-Migration -From 20231023025116_bridge_identity_20231022 -To 20231023025116_bridge_identity_20231022 -Context EFCoreMigrationDbContext 生成的sql脚本如下,可直接在数据库中执行:
CREATE TABLE IF NOT EXISTS `__EFMigrationsHistory` (
    `MigrationId` varchar(150) CHARACTER SET utf8mb4 NOT NULL,
    `ProductVersion` varchar(32) CHARACTER SET utf8mb4 NOT NULL,
    CONSTRAINT `PK___EFMigrationsHistory` PRIMARY KEY (`MigrationId`)
) CHARACTER SET=utf8mb4;

START TRANSACTION;

ALTER DATABASE CHARACTER SET utf8mb4;

CREATE TABLE `Authoritys` (
    `Id` char(36) COLLATE ascii_general_ci NOT NULL,
    `PageCode` varchar(64) CHARACTER SET utf8mb4 NULL,
    `PageName` varchar(64) CHARACTER SET utf8mb4 NULL,
    `MenuId` char(36) COLLATE ascii_general_ci NULL,
    `Operate` varchar(64) CHARACTER SET utf8mb4 NULL,
    `OperateName` varchar(64) CHARACTER SET utf8mb4 NULL,
    `IsDeleted` tinyint(1) NOT NULL DEFAULT FALSE,
    `TenantId` char(36) COLLATE ascii_general_ci NULL,
    `ExtraProperties` longtext CHARACTER SET utf8mb4 NULL,
    `ConcurrencyStamp` varchar(40) CHARACTER SET utf8mb4 NULL,
    `CreationTime` datetime(6) NOT NULL,
    `CreatorId` char(36) COLLATE ascii_general_ci NULL,
    `LastModificationTime` datetime(6) NULL,
    `LastModifierId` char(36) COLLATE ascii_general_ci NULL,
    CONSTRAINT `PK_Authoritys` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4;

CREATE TABLE `t_identity_menu` (
    `Id` char(36) COLLATE ascii_general_ci NOT NULL,
    `Name` varchar(200) CHARACTER SET utf8mb4 NULL COMMENT '菜单名',
    `Code` int NOT NULL COMMENT '菜单编码',
    `ParentId` char(36) COLLATE ascii_general_ci NOT NULL COMMENT '父级菜单标识',
    `Level` int NOT NULL COMMENT '层级',
    `IconUrl` varchar(200) CHARACTER SET utf8mb4 NULL COMMENT '图标路径',
    `RedirectUrl` varchar(200) CHARACTER SET utf8mb4 NULL COMMENT '跳转路径',
    `MenuType` int NOT NULL COMMENT '菜单类型 0 APP 1 web',
    `MenuInOut` int NOT NULL COMMENT '地址类型',
    `Sort` int NOT NULL COMMENT '排序',
    `IsActive` tinyint(1) NOT NULL COMMENT '是否启用',
    `IsDeleted` tinyint(1) NOT NULL DEFAULT FALSE COMMENT '是否删除',
    `TenantId` char(36) COLLATE ascii_general_ci NULL COMMENT '租户Id',
    `SystemId` varchar(20) CHARACTER SET utf8mb4 NULL COMMENT '用户归属系统Id',
    `Remark` varchar(500) CHARACTER SET utf8mb4 NULL COMMENT '备注',
    `ExtraProperties` longtext CHARACTER SET utf8mb4 NULL,
    `ConcurrencyStamp` varchar(40) CHARACTER SET utf8mb4 NULL,
    `CreationTime` datetime(6) NOT NULL,
    `CreatorId` char(36) COLLATE ascii_general_ci NULL,
    `LastModificationTime` datetime(6) NULL,
    `LastModifierId` char(36) COLLATE ascii_general_ci NULL,
    CONSTRAINT `PK_t_identity_menu` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4 COMMENT='菜单表';

CREATE TABLE `t_identity_role_authority` (
    `Id` char(36) COLLATE ascii_general_ci NOT NULL,
    `RoleId` char(36) COLLATE ascii_general_ci NOT NULL COMMENT '角色标识',
    `AuthId` char(36) COLLATE ascii_general_ci NOT NULL COMMENT '权限标识',
    `IsDeleted` tinyint(1) NOT NULL DEFAULT FALSE COMMENT '是否删除',
    `TenantId` char(36) COLLATE ascii_general_ci NULL,
    `ExtraProperties` longtext CHARACTER SET utf8mb4 NULL,
    `ConcurrencyStamp` varchar(40) CHARACTER SET utf8mb4 NULL,
    `CreationTime` datetime(6) NOT NULL,
    `CreatorId` char(36) COLLATE ascii_general_ci NULL,
    `LastModificationTime` datetime(6) NULL,
    `LastModifierId` char(36) COLLATE ascii_general_ci NULL,
    CONSTRAINT `PK_t_identity_role_authority` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4 COMMENT='角色权限中间表';

CREATE TABLE `t_identity_role_expand` (
    `Id` char(36) COLLATE ascii_general_ci NOT NULL,
    `RoleId` char(36) COLLATE ascii_general_ci NOT NULL COMMENT '角色标识',
    `RoleType` int NOT NULL COMMENT '角色类型',
    `RoleStatus` int NOT NULL COMMENT '用户状态:0启用、1禁用',
    `SystemId` varchar(20) CHARACTER SET utf8mb4 NULL COMMENT '归属系统Id',
    `ExtraProperties` longtext CHARACTER SET utf8mb4 NULL,
    `ConcurrencyStamp` varchar(40) CHARACTER SET utf8mb4 NULL,
    `CreationTime` datetime(6) NOT NULL,
    `CreatorId` char(36) COLLATE ascii_general_ci NULL,
    `LastModificationTime` datetime(6) NULL,
    `LastModifierId` char(36) COLLATE ascii_general_ci NULL,
    CONSTRAINT `PK_t_identity_role_expand` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4 COMMENT='角色扩展表';

CREATE TABLE `t_identity_role_menu` (
    `Id` char(36) COLLATE ascii_general_ci NOT NULL,
    `RoleId` char(36) COLLATE ascii_general_ci NOT NULL COMMENT '角色标识',
    `MenuId` char(36) COLLATE ascii_general_ci NOT NULL COMMENT '菜单标识',
    `IsDeleted` tinyint(1) NOT NULL DEFAULT FALSE COMMENT '是否删除',
    `ExtraProperties` longtext CHARACTER SET utf8mb4 NULL,
    `ConcurrencyStamp` varchar(40) CHARACTER SET utf8mb4 NULL,
    `CreationTime` datetime(6) NOT NULL,
    `CreatorId` char(36) COLLATE ascii_general_ci NULL,
    `LastModificationTime` datetime(6) NULL,
    `LastModifierId` char(36) COLLATE ascii_general_ci NULL,
    CONSTRAINT `PK_t_identity_role_menu` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4 COMMENT='角色菜单中间表';

CREATE TABLE `t_identity_user_expand` (
    `Id` char(36) COLLATE ascii_general_ci NOT NULL,
    `UserId` char(36) COLLATE ascii_general_ci NOT NULL COMMENT '用户标识',
    `UserType` int NOT NULL COMMENT '用户类型',
    `UserStatus` int NOT NULL COMMENT '用户状态',
    `IsConfirm` int NOT NULL COMMENT '是否已确认结果,0未确认,1已确认',
    `HeadFileUrl` varchar(200) CHARACTER SET utf8mb4 NULL COMMENT '用户图像',
    `SystemId` varchar(20) CHARACTER SET utf8mb4 NULL COMMENT '归属系统Id',
    `IsLock` tinyint(1) NULL COMMENT '锁定状态',
    `LoginFailedCount` int NULL COMMENT '登录失败次数',
    `LockTime` datetime(6) NULL COMMENT '锁定时间',
    `LockUserId` char(36) COLLATE ascii_general_ci NULL COMMENT '锁定人',
    `UnLockTime` datetime(6) NULL COMMENT '解锁时间',
    `UnLockUserId` char(36) COLLATE ascii_general_ci NULL COMMENT '解锁人',
    `ExtraProperties` longtext CHARACTER SET utf8mb4 NULL,
    `ConcurrencyStamp` varchar(40) CHARACTER SET utf8mb4 NULL,
    `CreationTime` datetime(6) NOT NULL,
    `CreatorId` char(36) COLLATE ascii_general_ci NULL,
    `LastModificationTime` datetime(6) NULL,
    `LastModifierId` char(36) COLLATE ascii_general_ci NULL,
    CONSTRAINT `PK_t_identity_user_expand` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4 COMMENT='用户扩展表';

INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20231023025116_bridge_identity_20231022', '7.0.1');

COMMIT;

 

方法二:直接迁移 Update-DataBase 20231023025116_bridge_identity_20231022 完成后查看数据库表:

 

 

 

标签:VNext,COMMENT,NULL,get,用户,set,权限,public,Comment
From: https://www.cnblogs.com/zhengwei-cq/p/17781950.html

相关文章

  • JuiceFS 用户必备的 6 个技巧
    随着大数据、AI技术的发展,越来越多的企业、团队和个人开始使用JuiceFS,本文整理了6个超实用的JuiceFS技巧,帮助大家提升JuiceFS的管理效率。一、查看已挂载的文件系统有时候你可能在一台机器上挂载了多个JuiceFS文件系统,或是在多台机器上使用不同的选项挂载了同一个文件......
  • 队列存放用户请求,执行耗时操作的解决方案
    队列存放用户请求的实现方案直接上图待补充……......
  • Oracle创建查询账户,并授予查询其他用户表权限
    要在Oracle数据库中创建新的查询用户,以便可以访问表和视图等信息一、登录DBA权限的账号1、可以使用命令窗口登录,进行数据库管理connsys/sys123assysdba;2、也可以使用plsql登录以上两种方式自选其一登录。二、新建表空间、用户此部分操作由于在另一篇随笔中有详细记录......
  • 快手视频评论区提取工具,抓取评论内容UID用户信息,采集精准关键词,开源版!
    快手评论区里面其实有很多精准的关键词用户,我们可以提取这些关键词用户来实现企业的营销和提取,那么今天我就给大家推荐这么一个软件源码,易语言开发的,用的鱼刺_http模块,和gzip.dll,提取的时候需要用到COOKIE,这个在浏览器开f12刷新,然后选择快手主页,抓包复制cookie就可以了。软件截图......
  • 快手视频评论区用户ID提取工具,可采集UID快手号,评论内容时间,易语言开源版!
    这是之前给某个客户开发的,定制的,可以输入用户的作品连接,然后导入COOKIE后就会自动提取评论区下载的所有用户评论内容用户di,快手号这些信息,用的是鱼刺_http模块,和“gzip.dll”组件,具体源码和界面我下面会开源出来,仅供大家学习。软件界面: 采集效果: 源码分享:==============......
  • Linux系统用户如何添加到用户组
    新增一个用户并添加到指定用户组#检查用户组是否存在,如果组存在则会输出组信息,否则没有任何输出grep<用户组名称>/etc/group#如果用户组不存在则使用如下命令新建用户组:groupadd<用户组名称>#新建用户并将其加入指定用户组,作为其主用户组(每个用户有且只有一个主用户组)useradd......
  • 用户管理
    Linux系统是多用户,多任务的分时操作系统,系统上每一个进程都有一个特定的文件,每个文件都被特定的用户所拥有。用户标识:UID与GID 因为Linux系统并不能识别用户名信息,所以每个用户都有唯一的系统可识别的UID,它类似于居民身份证编号。id命令可以查看当前用户登录信息,UID为用户的ID......
  • linux文件权限1
    基本权限UGO文件和文件目录的所有者:u——User文件和文件目录的所属组:g——Group其他用户:o——Other文件类型d:文件夹-:普通文件l:软链接(类似Windows的快捷方式)b:块设备文件(例如硬盘、光驱等)文件基本权限r:读权限——对于文件指可以查看文件内容;对于目录指可以浏览该目录......
  • 用户管理
    管理用户/组1.创建用户/组首先使用useradd命令创建用户qf01,不指定任何选项,然后使用grep命令过滤出该用户信息,具体如下: 其中,/etc/shadow的行中显示“!!”,由此可以看出密码未设置。在/home目录下可查看新创建的用户,系统还会为用户创建一个邮箱。在Linux下创建一个用户时,若未指......
  • 文件权限
    基本权限UGO 首先介绍U,G,O这三个字母所代表的含义 U:owner,属主 G:group,属组 O:other,其他用户 r:read(读取),数字设定为4w:write(写入),数字设定为2x:execute(执行),数字设定为1  chown:修改文件属主,属组chgrp:修改文件属组chmod:修改文件权限chomod:修改文件权限使用......