diff --git a/DotBased.AspNet.Authority.EFCore/AuthorityContext.cs b/DotBased.AspNet.Authority.EFCore/AuthorityContext.cs index 5463940..6a0f813 100644 --- a/DotBased.AspNet.Authority.EFCore/AuthorityContext.cs +++ b/DotBased.AspNet.Authority.EFCore/AuthorityContext.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; namespace DotBased.AspNet.Authority.EFCore; -public class AuthorityContext : DbContext +public class AuthorityContext(DbContextOptions options) : DbContext(options) { public DbSet Attributes { get; set; } public DbSet Groups { get; set; } @@ -12,10 +12,32 @@ public class AuthorityContext : DbContext protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity().ToTable("authority_attributes"); - modelBuilder.Entity().ToTable("authority_groups"); - modelBuilder.Entity().ToTable("authority_roles"); - modelBuilder.Entity().ToTable("authority_users"); + modelBuilder.Entity(attributeEntity => + { + attributeEntity.ToTable("authority_attributes"); + attributeEntity.HasKey(a => new { a.BoundId, a.AttributeKey }); + }); + + modelBuilder.Entity(groupEntity => + { + groupEntity.ToTable("authority_groups"); + groupEntity.HasKey(x => x.Id); + groupEntity.HasMany(g => g.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity(roleEntity => + { + roleEntity.ToTable("authority_roles"); + roleEntity.HasKey(x => x.Id); + }); + + modelBuilder.Entity(userEntity => + { + userEntity.ToTable("authority_users"); + userEntity.HasKey(x => x.Id); + userEntity.HasMany(u => u.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade); + }); + base.OnModelCreating(modelBuilder); } } \ No newline at end of file diff --git a/DotBased.AspNet.Authority.EFCore/DotBased.AspNet.Authority.EFCore.csproj b/DotBased.AspNet.Authority.EFCore/DotBased.AspNet.Authority.EFCore.csproj index 64df6fe..ec7e8bd 100644 --- a/DotBased.AspNet.Authority.EFCore/DotBased.AspNet.Authority.EFCore.csproj +++ b/DotBased.AspNet.Authority.EFCore/DotBased.AspNet.Authority.EFCore.csproj @@ -12,6 +12,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DotBased.AspNet.Authority.EFCore/README.md b/DotBased.AspNet.Authority.EFCore/README.md new file mode 100644 index 0000000..f15acc9 --- /dev/null +++ b/DotBased.AspNet.Authority.EFCore/README.md @@ -0,0 +1,23 @@ +# EF Core database + +## Add migration project +```csharp +options.UseSqlite("Data Source=dev-dotbased.db", c => c.MigrationsAssembly("PROJECT-NAME")); +``` + +## EF Tool + +Add migration +```shell +dotnet ef migrations add MIGRATION-NAME --project PROJECT-NAME +``` + +Remove migrations +```shell +dotnet ef migrations remove --project PROJECT-NAME +``` + +Update database +```shell +dotnet ef database update --project PROJECT-NAME +``` diff --git a/DotBased.AspNet.Authority/DotBased.AspNet.Authority.csproj b/DotBased.AspNet.Authority/DotBased.AspNet.Authority.csproj index 53d8b22..b0a8901 100755 --- a/DotBased.AspNet.Authority/DotBased.AspNet.Authority.csproj +++ b/DotBased.AspNet.Authority/DotBased.AspNet.Authority.csproj @@ -17,6 +17,7 @@ + diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityAttribute.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityAttribute.cs index 5bc4d6e..27c08ca 100755 --- a/DotBased.AspNet.Authority/Models/Authority/AuthorityAttribute.cs +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityAttribute.cs @@ -1,26 +1,18 @@ namespace DotBased.AspNet.Authority.Models.Authority; -public class AuthorityAttribute +public class AuthorityAttribute(string attributeKey, Guid bound) { - public AuthorityAttribute(string attributeKey, string bound) + public AuthorityAttribute() : this(string.Empty, Guid.NewGuid()) { - AttributeKey = attributeKey; - BoundId = bound; } - public AuthorityAttribute() - { - AttributeKey = string.Empty; - BoundId = string.Empty; - } - - public string AttributeKey { get; set; } // ClaimType/Authority.attribute.enabled + public Guid BoundId { get; set; } = bound; - public string BoundId { get; set; } // Bound to User, Group, Role id + public string AttributeKey { get; set; } = attributeKey; - public object? AttributeValue { get; set; } + public string AttributeValue { get; set; } = string.Empty; - public string? Type { get; set; } // AspNet.Claim.Role/Property/Data.JSON, Data.Raw, Data.Base64 etc. + public string? Type { get; set; } public long Version { get; set; } } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityGroup.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityGroup.cs index c5474df..05f85c9 100755 --- a/DotBased.AspNet.Authority/Models/Authority/AuthorityGroup.cs +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityGroup.cs @@ -20,4 +20,5 @@ public class AuthorityGroup public long Version { get; set; } public DateTime CreatedDate { get; set; } + public ICollection Attributes { get; set; } = []; } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityRole.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityRole.cs index f1ef51a..1e215aa 100755 --- a/DotBased.AspNet.Authority/Models/Authority/AuthorityRole.cs +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityRole.cs @@ -1,6 +1,6 @@ namespace DotBased.AspNet.Authority.Models.Authority; -public abstract class AuthorityRole() +public class AuthorityRole() { public AuthorityRole(string name) : this() { diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityUser.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityUser.cs index bc90356..03214ca 100755 --- a/DotBased.AspNet.Authority/Models/Authority/AuthorityUser.cs +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityUser.cs @@ -41,5 +41,7 @@ public class AuthorityUser() public bool PhoneNumberConfirmed { get; set; } + public ICollection Attributes { get; set; } = []; + public override string ToString() => UserName ?? EmailAddress ?? string.Empty; } \ No newline at end of file diff --git a/TestWebApi/Program.cs b/TestWebApi/Program.cs index 1c24460..4c03d50 100755 --- a/TestWebApi/Program.cs +++ b/TestWebApi/Program.cs @@ -23,7 +23,7 @@ builder.Logging.ClearProviders(); builder.Logging.AddDotBasedLoggerProvider(LogService.Options); builder.Services.AddAuthorityContext(options => { - options.UseSqlite("Data Source=dev-dotbased.db"); + options.UseSqlite("Data Source=dev-dotbased.db", c => c.MigrationsAssembly("TestWebApi")); }); builder.Services.AddAuthority(options => { diff --git a/TestWebApi/TestWebApi.csproj b/TestWebApi/TestWebApi.csproj index 6340445..673fe48 100755 --- a/TestWebApi/TestWebApi.csproj +++ b/TestWebApi/TestWebApi.csproj @@ -8,7 +8,11 @@ - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + +