[ADD] Join tables. Added attributes to role.

This commit is contained in:
max 2025-02-10 02:11:35 +01:00
parent 2938e1311f
commit 65d625a30d
6 changed files with 52 additions and 11 deletions

View File

@ -1,3 +1,4 @@
using DotBased.AspNet.Authority.EFCore.Models;
using DotBased.AspNet.Authority.Models.Authority; using DotBased.AspNet.Authority.Models.Authority;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -10,6 +11,10 @@ public class AuthorityContext(DbContextOptions<AuthorityContext> options) : DbCo
public DbSet<AuthorityRole> Roles { get; set; } public DbSet<AuthorityRole> Roles { get; set; }
public DbSet<AuthorityUser> Users { get; set; } public DbSet<AuthorityUser> Users { get; set; }
public DbSet<RoleGroup> RoleGroup { get; set; }
public DbSet<RoleUser> RoleUser { get; set; }
public DbSet<UserGroup> UserGroup { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.Entity<AuthorityAttribute>(attributeEntity => modelBuilder.Entity<AuthorityAttribute>(attributeEntity =>
@ -29,6 +34,7 @@ public class AuthorityContext(DbContextOptions<AuthorityContext> options) : DbCo
{ {
roleEntity.ToTable("authority_roles"); roleEntity.ToTable("authority_roles");
roleEntity.HasKey(x => x.Id); roleEntity.HasKey(x => x.Id);
roleEntity.HasMany(r => r.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade);
}); });
modelBuilder.Entity<AuthorityUser>(userEntity => modelBuilder.Entity<AuthorityUser>(userEntity =>
@ -38,6 +44,24 @@ public class AuthorityContext(DbContextOptions<AuthorityContext> options) : DbCo
userEntity.HasMany(u => u.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade); userEntity.HasMany(u => u.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade);
}); });
modelBuilder.Entity<RoleGroup>(rgEntity =>
{
rgEntity.ToTable("role_group");
rgEntity.HasKey(rg => new { rg.RoleId, rg.GroupId });
});
modelBuilder.Entity<RoleUser>(ruEntity =>
{
ruEntity.ToTable("role_user");
ruEntity.HasKey(ru => new { ru.RoleId, ru.UserId });
});
modelBuilder.Entity<UserGroup>(ugEntity =>
{
ugEntity.ToTable("user_group");
ugEntity.HasKey(ug => new { ug.UserId, ug.GroupId });
});
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
} }
} }

View File

@ -0,0 +1,7 @@
namespace DotBased.AspNet.Authority.EFCore.Models;
public class RoleGroup
{
public Guid RoleId { get; set; }
public Guid GroupId { get; set; }
}

View File

@ -0,0 +1,7 @@
namespace DotBased.AspNet.Authority.EFCore.Models;
public class RoleUser
{
public Guid RoleId { get; set; }
public Guid UserId { get; set; }
}

View File

@ -0,0 +1,7 @@
namespace DotBased.AspNet.Authority.EFCore.Models;
public class UserGroup
{
public Guid UserId { get; set; }
public Guid GroupId { get; set; }
}

View File

@ -1,24 +1,18 @@
namespace DotBased.AspNet.Authority.Models.Authority; namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityGroup public class AuthorityGroup()
{ {
public AuthorityGroup(string name) : this() public AuthorityGroup(string name) : this()
{ {
Name = name; Name = name;
} }
public AuthorityGroup() public Guid Id { get; set; } = Guid.NewGuid();
{
Id = Guid.NewGuid();
CreatedDate = DateTime.Now;
}
public Guid Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public long Version { get; set; } public long Version { get; set; }
public DateTime CreatedDate { get; set; } public DateTime CreatedDate { get; set; } = DateTime.Now;
public ICollection<AuthorityAttribute> Attributes { get; set; } = []; public ICollection<AuthorityAttribute> Attributes { get; set; } = [];
} }

View File

@ -15,5 +15,7 @@ public class AuthorityRole()
public DateTime CreatedDate { get; set; } = DateTime.Now; public DateTime CreatedDate { get; set; } = DateTime.Now;
public IEnumerable<AuthorityAttribute> Attributes { get; set; } = [];
public override string ToString() => Name ?? string.Empty; public override string ToString() => Name ?? string.Empty;
} }